首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Apache Oltu unsupported_response_type

Apache Oltu unsupported_response_type
EN

Stack Overflow用户
提问于 2017-04-23 17:54:18
回答 3查看 4K关注 0票数 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类。我还有什么选择可以避免这个例外呢?

代码语言:javascript
复制
            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

“丑”解决办法:

代码语言:javascript
复制
GitHubTokenResponse  tk = oAuthClient.accessToken(request, GitHubTokenResponse.class);
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-06-12 06:08:48

它也适用于OAuthJSONAccessTokenResponse。下面是我尝试的代码,它运行得很好。

代码语言:javascript
复制
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();

我认为我们需要在标题中设置内容类型。解决问题的这两行。

代码语言:javascript
复制
request.addHeader("Accept", "application/json");
request.addHeader("Content-Type", "application/json");

希望能帮上忙。

票数 4
EN

Stack Overflow用户

发布于 2021-02-11 07:38:44

同样的问题在我的结尾是固定的,下面-

  1. 添加了丢失的org.json罐
  2. 删除请求中的重复标题“内容-类型”
票数 1
EN

Stack Overflow用户

发布于 2018-03-19 13:48:43

在我的例子中,我已经设置了这些头,但是仍然得到了这个异常。引用1.0.2标签中的源。

代码语言:javascript
复制
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中可以防止此异常。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43574573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档