Apache Oltu unsupported_response_type

时间:2017-04-23 17:54:18

标签: authentication exception github access-token oltu

当我尝试从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={}}

我希望尽可能保持一般,所以我不想使用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);

3 个答案:

答案 0 :(得分:2)

它也适用于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");

希望得到这个帮助。

答案 1 :(得分:1)

我最后的相同问题由以下修复 -

  1. 添加了缺少的 org.json Jar
  2. 删除了请求中重复的标头“content-type”

答案 2 :(得分:0)

在我的情况下,我已经设置了这些标题,但仍然有这个例外。引自1.0.2标签的来源。

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

它隐藏了Throwable异常e。在调试时我发现它是org.json.JSONTokener的类找不到错误。所以将org.json jar添加到tomcat lib我可以防止这种异常。