2017-04-23 11 views
1

これはgithubからアクセストークンを受け取ろうとすると問題になります。Apache Oltu unsupported_response_type

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={}}

私はGithubTokenResponseクラスを使用したくないので、可能な限り一般として、それを維持したいです。例外を避けるために残した選択肢は何ですか?

  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 

"醜い" 解決策:

GitHubTokenResponse tk = oAuthClient.accessToken(request, GitHubTokenResponse.class); 

答えて

1

またOAuthJSONAccessTokenResponseで動作します。ここでは私が試してみて、うまく働いている次のコードです。

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

私たちは、ヘッダにコンテンツタイプを設定する必要があると思います。問題を解決するこれらの2つの行。

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

このヘルプが必要です。