如何使用Google Access Token获取用户个人资料

jq *_*ner 16 signing google-api google-oauth

我正在测试通过谷歌访问令牌获取用户信息

http://www.mawk3y.net/glogin

点击登录按钮后,我被重定向到

https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=access_token_here

并获得这样的一些JSON数据

{
"issued_to": "my client id.apps.googleusercontent.com",
"audience": "my client id.apps.googleusercontent.com",
"user_id": "user id here",
"scope": "https://www.googleapis.com/auth/plus.login",
"expires_in": 3596,
"access_type": "online"
}
Run Code Online (Sandbox Code Playgroud)

现在我需要知道如何提取用户名,地址和电子邮件任何帮助吗?

提前致谢

bet*_*akh 17

试试这个:

 var url = 'https://www.googleapis.com/plus/v1/people/me?access_token={access_token}';

  $.ajax({
    type: 'GET',
    url: url,
    async: false,
    success: function(userInfo) {
      //info about user
      console.log(userInfo);
      console.log('test');
    },
    error: function(e) {
      console.log('error');

    }
  });
Run Code Online (Sandbox Code Playgroud)


Him*_*der 16

您可以使用此API验证google在您的服务器上登录后收到的身份验证令牌

请求

https://www.googleapis.com/oauth2/v3/tokeninfo?id_token={accces_token}
Run Code Online (Sandbox Code Playgroud)

响应

{

  "email_verified": "true",
  "email": "abhinav.xxx@gmail.com",
  "name": "abhinav srivastava",
  "picture": "https://lh3.googleusercontent.com/-xgD_zFj1EgY/AAAAAAAAAAI/AAAAAAAACZ0/fnecSQ03o0Y/s96-c/photo.jpg",
  "given_name": "abhinav",
  "family_name": "srivastava",
  "locale": "en",
  ...
  ...
}
Run Code Online (Sandbox Code Playgroud)

资源

  • 上面的请求是针对id_token的,不是针对access_token的 (7认同)

Asu*_*nda 13

我遇到过同样的问题。我想提取用户信息。但是无法获得要点击的确切链接。然后,我经历的代码去护照谷歌战略的Line number 54。

我的范围是 ['profile', 'email']

获取请求

https://www.googleapis.com/oauth2/v3/userinfo?access_token={access_token}
Run Code Online (Sandbox Code Playgroud)

回复

{
  "sub": "23423....",
  "name": "John Doe",
  "given_name": "John",
  "family_name": "Doe",
  "picture": "<Profile picture URL>",
  "email": "john.doe@gmail.com",
  "email_verified": true,
  "locale": "en"
}
Run Code Online (Sandbox Code Playgroud)


Aru*_*R S 9

如果您想要 token_info,请将tokeninfo作为参数传递

https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=ya29.a0AfH6SMArZZITzn-...
Run Code Online (Sandbox Code Playgroud)

如果您想要所有用户信息,请将userinfo作为参数传递

https://www.googleapis.com/oauth2/v3/userinfo?access_token=ya29.a0AfH6SMArZZITzn-...
Run Code Online (Sandbox Code Playgroud)

令牌信息响应

{
    "azp": "",
    "aud": "",
    "sub": "",
    "scope": "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile openid",
    "exp": "",
    "expires_in": "",
    "email": "",
    "email_verified": "",
    "access_type": ""
}
Run Code Online (Sandbox Code Playgroud)

用户信息响应

{
    "sub": "",
    "name": "",
    "given_name": "",
    "family_name": "",
    "picture": "",
    "email": "",
    "email_verified": ,
    "locale": ""
}
Run Code Online (Sandbox Code Playgroud)

不要忘记传递https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile作为范围

var params = {
  'client_id': '',
  'redirect_uri': '',
  'response_type': 'token',
  'scope': 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile',
};
Run Code Online (Sandbox Code Playgroud)


Mig*_*res 4

您需要使用访问令牌(在重定向网址中获取)来访问 Google 的 People API。请在此处查看规格。

您可能会发现Google 的 OAuth2 游乐场对于了解如何使用访问令牌访问 Google 的 API 非常有用。

祝你好运!