当我试图从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);发布于 2017-06-12 06:08:48
它也适用于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");希望能帮上忙。
发布于 2021-02-11 07:38:44
同样的问题在我的结尾是固定的,下面-
发布于 2018-03-19 13:48:43
在我的例子中,我已经设置了这些头,但是仍然得到了这个异常。引用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中可以防止此异常。
https://stackoverflow.com/questions/43574573
复制相似问题