1

This is the problem when I try to receive an access token from github.

OAuthProblemException{error='unsupported_response_type', description='Invalid response! Response body is not application/json encoded', uri='null', state='null', scope='null', redirectUri='null', responseStatus=0, parameters={}}

I want to keep it as general as possible so I don't want to use the GithubTokenResponse class. What options do I have left to avoid the exception ?

            OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
            OAuthClientRequest request = OAuthClientRequest
                    .tokenLocation("https://github.com/login/oauth/access_token")
                    .setCode(code)
                    .setGrantType(GrantType.AUTHORIZATION_CODE)
                    .setClientId(id)
                    .setClientSecret(secret)
                    .buildQueryMessage();

            OAuthAccessTokenResponse  tk = oAuthClient.accessToken(request); //this causes the problem

"Ugly" solution :

GitHubTokenResponse  tk = oAuthClient.accessToken(request, GitHubTokenResponse.class);
Oleg
  • 1,479
  • 3
  • 21
  • 42

3 Answers3

4

It also works with OAuthJSONAccessTokenResponse. Here is the following code what I try and it is working fine.

OAuthClient client = new OAuthClient(new URLConnectionClient());

            OAuthClientRequest request =
                    OAuthClientRequest.tokenLocation(TOKEN_REQUEST_URL)
                    .setGrantType(GrantType.CLIENT_CREDENTIALS)
                    .setClientId(CLIENT_ID)
                    .setClientSecret(CLIENT_SECRET)
                    // .setScope() here if you want to set the token scope
                    .buildQueryMessage();
            request.addHeader("Accept", "application/json");
            request.addHeader("Content-Type", "application/json");
            request.addHeader("Authorization", base64EncodedBasicAuthentication());

            String token = client.accessToken(request, OAuth.HttpMethod.POST, OAuthJSONAccessTokenResponse.class).getAccessToken();

I think we need to set the content type in the header. These two lines which fix the issue.

request.addHeader("Accept", "application/json");
request.addHeader("Content-Type", "application/json");

Hope this help.

Anshu Kumar
  • 633
  • 7
  • 12
1

Same issue at my end is fixed by below -

  1. Added missing org.json Jar
  2. Removed duplicate header 'content-type' in request
0

In my case I have already set these headers but still got this exception. Quoting from source from 1.0.2 tag.

try {
            this.body = body;
            parameters = JSONUtils.parseJSON(body);
        } catch (Throwable e) {
            throw OAuthProblemException.error(OAuthError.CodeResponse.UNSUPPORTED_RESPONSE_TYPE,
                "Invalid response! Response body is not " + OAuth.ContentType.JSON + " encoded");
        }

It hides the Throwable exception e. While debugging I found that it was class not found error for org.json.JSONTokener. So adding org.json jar to the tomcat lib I could prevent this exception.

lakshman
  • 2,641
  • 6
  • 37
  • 63