2017-04-23 289 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(); 

我認爲,我們需要設置在標題中內容類型。這兩行解決了這個問題。

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

希望得到這個幫助。