简体   繁体   English

OAuth2 Java - 如何设置Accept标头

[英]OAuth2 Java - How to set Accept header

I'm trying to implement a "sign in with Github" feature for my site with a hybrid solution - the site's javascript does the initial login/credentials, and the redirect url is for a tomcat servlet continuing the access token request. 我正在尝试使用混合解决方案为我的网站实现“使用Github登录”功能 - 网站的javascript执行初始登录/凭据,重定向url用于继续访问令牌请求的tomcat servlet。

I'm using Google API oauth2 library for Java. 我正在使用Google API oauth2库来实现Java。

My code currently looks like this: 我的代码目前看起来像这样:

AuthorizationCodeTokenRequest tokenRequest =
    new AuthorizationCodeTokenRequest(
            new NetHttpTransport(),
            new JacksonFactory(),
            new GenericUrl("https://github.com/login/oauth/access_token"),
            code)
            .setClientAuthentication(
                new ClientParametersAuthentication(
                    GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET));

TokenResponse tokenResponse = tokenRequest.execute();

This doesn't work, and I'm getting the following exception: 这不起作用,我得到以下异常:

com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'access_token': was expecting ('true', 'false' or 'null')

I've looked into it, and found the response from Github is not in a json format (which the JacksonFactory expects), but actually looks like this: 我调查了一下,发现Github的响应不是json格式(JacksonFactory期望的),但实际上看起来像这样:

access_token=xxxxxxxxxxx&scope=user%3Aemail&token_type=bearer

I've found in https://developer.github.com/v3/media/#request-specific-version that I need to set the Accept http header of the request to be application/vnd.github.v3+json for it to return something in json format. 我在https://developer.github.com/v3/media/#request-specific-version中找到了我需要将请求的Accept http标头设置为application / vnd.github.v3 + json for it以json格式返回一些东西。

How can I do this with Google's oauth2 api? 如何使用Google的oauth2 api执行此操作?

EDIT: Based on tinker 's response, I solved with the following addition: 编辑:基于修补匠的反应,我解决了以下补充:

tokenRequest.setRequestInitializer(new HttpRequestInitializer()
{
    @Override
    public void initialize(HttpRequest request) throws IOException
    {
        request.getHeaders().setAccept("application/json");
    }
});

You have to create an HttpRequest and add the headers to that. 您必须创建一个HttpRequest并添加标头。 Look at javadoc for setting the request headers here. 查看javadoc以在此处设置请求标头。

From the AuthorizationCodeTokenRequest you can get the HttpRequestInitializer using getRequestInitializer() . AuthorizationCodeTokenRequest您可以使用getRequestInitializer()获取HttpRequestInitializer If this is null then you can create an initalizer and use setRequestInitializer() with the newly created object. 如果这是null,那么你可以创建一个initalizer并使用setRequestInitializer()和新创建的对象。

With the initializer you can now initialize the request that has the desired headers. 使用初始化程序,您现在可以initialize具有所需标头的请求。

I haven't tested this so I'm not absolutely sure, but from the docs this looks like the only way to do it. 我没有测试过这个,所以我不是很确定,但从文档中看,这看起来是唯一的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM