authenticationexceptiongithubaccess-tokenoltu

Apache Oltu unsupported_response_type


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);

Solution

  • 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.