1

这是我尝试从 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);
4

3 回答 3

3

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

希望这有帮助。

于 2017-06-12T06:08:48.710 回答
1

我最后的相同问题由以下解决 -

  1. 添加了缺少的 org.json Jar
  2. 删除了请求中的重复标头“内容类型”
于 2021-02-11T07:38:44.280 回答
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 我可以防止此异常。

于 2018-03-19T13:48:43.987 回答