从Oauth2 Google Contacts API获取用户信息 [英] Get Userinfo from Oauth2 Google Contacts API

查看:1112
本文介绍了从Oauth2 Google Contacts API获取用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的错误:

  com.google.api.client.googleapis.json.GoogleJsonResponseException:401未授权
{
code:401,
errors:[{
domain:global,
location:Authorization,
locationType:header,
message:证书无效,
reason:authError
}],
message:无效凭证
}

以下代码中,我使用:

  GoogleCredential凭证=新GoogleCredential.Builder()
.setTransport(this.TRANSPORT).setJsonFactory(this.JSON_FACTORY)
。 setClientSecrets(Constants.CLIENT_ID,Constants.CLIENT_SECRET).build();
credential.setAccessToken(tokenResponse.getAccessToken());
credential.setAccessToken(tokenResponse.getRefreshToken());

直到这里,我得到刷新令牌,访问令牌等

'pre> 的oauth2 userInfoService =新Oauth2.Builder(this.TRANSPORT,
this.JSON_FACTORY,credential.getRequestInitializer())
.setApplicationName(常数。 APPLICATION_NAME).build();

它在下线失败:(不知道,为什么?)

  Userinfo userInfo = userInfoService.userinfo()。get()。execute(); 

我在网上搜索过,而且我收到了很少的例子和稀有材料。
任何机构都有任何想法吗?



我做错了什么?

解决方案

我在猜测 credential.getRequestInitializer()为null。



我已经通过设置自定义请求初始化程序到这样的凭证对象

  GoogleCredential凭证=新的GoogleCredential.Builder()
.setTransport .TRANSPORT).setJsonFactory(this.JSON_FACTORY)
.setClientSecrets(Constants.CLIENT_ID,Constants.CLIENT_SECRET).setRequestInitializer((新HttpRequestInitializer(){
@覆盖
公共无效初始化(HttpRequest的请求)
抛出IOException {
request.getHeaders()。put(Authorization,Bearer+ accessToken);
}
}))。build()

Google的文档指定以下内容:

**
例如,使用access_token查询字符串参数调用UserInfo API如下所示:



GET https://www.googleapis.com/oauth2/v1/userinfo?access_token= {accessToken}
使用HTTP标头中的访问令牌调用相同的API如下所示:



GET / oauth2 / v1 / userinfo HTTP / 1.1
授权:持有者{accessToken}
主持人:googleapis.com **



希望这会帮助你

Error which i am getting:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
{
  "code" : 401,
  "errors" : [ {
    "domain" : "global",
    "location" : "Authorization",
    "locationType" : "header",
    "message" : "Invalid Credentials",
    "reason" : "authError"
  } ],
  "message" : "Invalid Credentials"
}

Below code, i am using:

GoogleCredential credential = new GoogleCredential.Builder()
    .setTransport(this.TRANSPORT).setJsonFactory(this.JSON_FACTORY)
    .setClientSecrets(Constants.CLIENT_ID, Constants.CLIENT_SECRET).build();
credential.setAccessToken(tokenResponse.getAccessToken());
credential.setAccessToken(tokenResponse.getRefreshToken());

Till here, i get Refresh token, Access Token, etc

Oauth2 userInfoService = new Oauth2.Builder(this.TRANSPORT,
        this.JSON_FACTORY, credential.getRequestInitializer())
        .setApplicationName(Constants.APPLICATION_NAME).build();

It fails at below line: (Dont know, Why?)

Userinfo userInfo = userInfoService.userinfo().get().execute();

I searched on web, and i get very less examples of it and rare materials. Any body has any idea on it?

What am i doing wrong?

解决方案

I'm guessing credential.getRequestInitializer() is null.

I've solved this by setting an custom request initializer to the credential object like this

GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(this.TRANSPORT).setJsonFactory(this.JSON_FACTORY)
.setClientSecrets(Constants.CLIENT_ID, Constants.CLIENT_SECRET).setRequestInitializer((new HttpRequestInitializer(){
                @Override
                public void initialize(HttpRequest request)
                        throws IOException {
                    request.getHeaders().put("Authorization", "Bearer " + accessToken);
                }
            })).build()

Google's documentation especifies the following:

** For example, a call to the UserInfo API using the access_token query string parameter looks like the following:

GET https://www.googleapis.com/oauth2/v1/userinfo?access_token={accessToken} A call to the same API using the access token in the HTTP header looks like the following:

GET /oauth2/v1/userinfo HTTP/1.1 Authorization: Bearer {accessToken} Host: googleapis.com**

Hope this will help you

这篇关于从Oauth2 Google Contacts API获取用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆