阿帕奇奥尔图 unsupported_response_type

Apache Oltu unsupported_response_type

这是我尝试从 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 class。我还有哪些选择可以避免异常?

            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" 解决方案:

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

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

我认为我们需要在 header 中设置 content 类型。这两行解决了这个问题。

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

希望对您有所帮助。

在我的例子中,我已经设置了这些 headers 但仍然有这个异常。从 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。在调试时我发现它是 class not found error for org.json.JSONTokener。所以将 org.json jar 添加到 tomcat 库中我可以防止这个异常。

我这边的相同问题已通过以下解决 -

  1. 添加了缺少的 org.json Jar
  2. 删除了请求中的重复 header 'content-type'