Google api 如何使用Google访问令牌获取用户配置文件

Google api 如何使用Google访问令牌获取用户配置文件,google-api,signing,google-oauth,Google Api,Signing,Google Oauth,我正在测试通过谷歌访问令牌获取用户信息 单击“登录”按钮后,我将被重定向到 然后获取一些类似这样的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

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

单击“登录”按钮后,我将被重定向到

然后获取一些类似这样的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"
}
现在我需要知道如何提取用户名,地址和电子邮件任何帮助请


提前感谢

您需要使用访问令牌(在重定向url中获得)来访问Google的People API。检查规格

您可能会发现,了解如何使用访问令牌访问Google的API非常有用

祝你好运

检查。演示中有一个示例

代码片段:

$accountObj = call_api($_SESSION['accessToken'],"https://www.googleapis.com/oauth2/v1/userinfo");
call\u api
调用api并获取数据:

function call_api($accessToken,$url){
    $curl = curl_init($url);

    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    $curlheader[0] = "Authorization: Bearer " . $accessToken;
    curl_setopt($curl, CURLOPT_HTTPHEADER, $curlheader);

    $json_response = curl_exec($curl);
    curl_close($curl);

    $responseObj = json_decode($json_response);

    return $responseObj;       
}
从account对象中,可以访问名称:

$your_name =  $accountObj->name;

您可以使用快速入门,并在此处查看示例:
下面是对

的描述,试试这个:

 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');

    }
  });

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

请求

https://www.googleapis.com/oauth2/v3/tokeninfo?id_token={accces_token}
回应

{

  "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",
  ...
  ...
}
{
  "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"
}

这是服务器端的一个简单nodejs代码

 var express = require('express');
 var appln = express();
 var google = require('googleapis');
 var plus = google.plus('v1');
 var OAuth = google.auth.OAuth2;
 var oauth2client = new OAuth(YOUR_CLIENT_ID  , YOUR_SECRET_ID ,  CALLBACK_REDIRECT_URI );

 appln.get("/tokens" , function(req , res ) {
         var code = req.query.code;
       oauth2client.getToken( code , function( err , tokens ){
                         if(err){
                            console.log(err);
                            res.send(err);
                            return;
                            }
                 oauth2client.setCredentials(tokens);
                 actoken = tokens.access_token;
                 reftoken = tokens.refresh_token;

       plus.people.get({  
                  userId: 'me',
                  auth: oauth2client
                  }, function (err, response) {
               // handle err and response
               var name = ""+response.displayName;
               var id =  ""+response.id;
               var age = ""+response.ageRange.min;
               if(err) console.log(err);
               console.log("Name : ", name," ",id,"",age);    
               res.send(response);

           });




        });
  });                       

如果您使用的是PHP

你可以查一下。简而言之,您要查找的代码如下

$payload = $client->verifyIdToken($id_token);

这里有效负载有您需要的信息,只要您添加所需的作用域。

我也有同样的问题。我想提取用户信息。但无法获得准确的链接。然后我检查了at
第54行的代码

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

获取请求

https://www.googleapis.com/oauth2/v3/userinfo?access_token={access_token}
回应

{

  "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",
  ...
  ...
}
{
  "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"
}
{
“sub”:“23423…”,
“姓名”:“约翰·多伊”,
“名字”:“约翰”,
“家族名称”:“Doe”,
“图片”:“,
“电子邮件”:“约翰。doe@gmail.com",
“电子邮件已验证”:正确,
“区域设置”:“en”
}

如果需要令牌信息,请将令牌信息作为参数传递

https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=ya29.a0AfH6SMArZZITzn-...
https://www.googleapis.com/oauth2/v3/userinfo?access_token=ya29.a0AfH6SMArZZITzn-...
如果需要所有用户信息,请将userinfo作为参数传递

https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=ya29.a0AfH6SMArZZITzn-...
https://www.googleapis.com/oauth2/v3/userinfo?access_token=ya29.a0AfH6SMArZZITzn-...
令牌信息响应

{
    "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": ""
}
{
    "sub": "",
    "name": "",
    "given_name": "",
    "family_name": "",
    "picture": "",
    "email": "",
    "email_verified": ,
    "locale": ""
}
用户信息响应

{
    "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": ""
}
{
    "sub": "",
    "name": "",
    "given_name": "",
    "family_name": "",
    "picture": "",
    "email": "",
    "email_verified": ,
    "locale": ""
}
不要忘记将作为范围传递

var params = {
  'client_id': '',
  'redirect_uri': '',
  'response_type': 'token',
  'scope': 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile',
};

我收到这个错误{“error”:{“errors”:[{“domain”:“usageLimits”,“reason”:“dailylimitedededunreg”,“message”:“超出未经验证使用的每日限制。继续使用需要注册。”,“extendedHelp”:“}],“code”:403,“message”:“超出未经验证使用的每日限制。继续使用需要注册。”}当我创建http访问此url时$response=file_get_contents(“);echo$response;当我使用此代码时$response=file_get_contents(“此处的用户ID?字段=生日%2CcurrentLocation%2CdisplayName%2Cemails%2Cgender%2Cname&key={MY API key HERE}”);echo$response;我收到此错误{“错误”:{“错误”:[{“域”:“usageLimits”,”原因“:“密钥无效”,“消息“:“错误请求”}],“代码”:400,“消息“:“错误请求”}”我刚刚复制并粘贴了我更新的API密钥,所以我确定它是ActiveThank,这很有效为什么Google文档没有显示access_令牌应该在查询字符串中?是的,这也让我有点迷茫。他们必须在rest API的概述中提到它,但我在Google plus部分中找不到。我没有找到他的想法:,其中提到可以将其放在url或授权头中。但它们并不是真正相关的文章。为什么访问令牌位于get url参数中。这是否会造成在中间跃点中获取日志的安全风险?过时的版本,V3已过时。上面的请求是针对id_令牌,而不是针对access_令牌{access_令牌}@RajatAggarwal我在谷歌文档中找不到这个URL。所以花了我很多时间。最终Passport JS代码库帮了我一些忙。