Obtaining an Access Token for Java using OAuth 2.0

The user wants to obtain an OAuth 2.0 access token from a REST API using Java code. While they have been able to successfully retrieve the token using a Bash script (curl command), their attempts to do so with Java code using Unirest have been unsuccessful. Despite researching and experimenting, the user is still unsure of what is missing from their Java code request.

Question:

I aim to retrieve the OAuth 2.0 access token from
REST API
using Java code. Although, I have already obtained it from the server using Bash (
script (curl
command).

A functional Bash script.

#!/usr/bin/env bash
       # Base URL of TeamForge site.
       site_url="https://teamforge.example.com"
       # TeamForge authentication credentials.
       username="foo"
       password="bar"
       # Requested scope (all)
       scope="urn:ctf:services:ctf
       curl -d "grant_type=password&client_id=api-client&scope=$scope&username=$username&password=$password" $site_url/sf/auth/token

Upon entering the code snippet, the resulting reply was as follows:

  {
         "access_token": "eyJraWQiOiIxIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiJhZG1pbiIsImF1ZCI...",
         "token_type": "Bearer"
       }

While attempting to convert it into Java code with the use of Unirest:

  HttpResponse jsonResponse = Unirest.post(""https://teamforge.example.com/sf/auth/token"")

Frequently Asked Questions