Authentication Apache Oltu不支持的\u响应\u类型

Authentication Apache Oltu不支持的\u响应\u类型,authentication,exception,github,access-token,oltu,Authentication,Exception,Github,Access Token,Oltu,这就是我试图从github接收访问令牌时的问题 OAuthProblemException{error='unsupported'u response\u type',description='Invalid response!响应正文不是应用程序/json编码的,'uri='null',state='null',scope='null',redirectUri='null',responseStatus=0,parameters={} 我希望它尽可能通用,因此我不想使用GithubTokenR

这就是我试图从github接收访问令牌时的问题

OAuthProblemException{error='unsupported'u response\u type',description='Invalid response!响应正文不是应用程序/json编码的,'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);

它还与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");

希望有此帮助。

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

它隐藏了可丢弃的异常e。调试时,我发现org.json.JSONTokener的类未找到错误。因此,将org.json jar添加到tomcat库中可以防止此异常。

在我的例子中,我已经设置了这些头,但仍然得到了此异常。从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");
        }

它隐藏了可丢弃的异常e。调试时,我发现org.json.JSONTokener的类未找到错误。因此,将org.json jar添加到tomcat库中可以防止此异常-

  • 添加了缺少的org.json Jar
  • 已删除请求中的重复标头“内容类型”

  • 同样的问题在我这边是固定的如下-

  • 添加了缺少的org.json Jar
  • 已删除请求中的重复标头“内容类型”