我正在测试通过Google访问令牌获取用户信息
点击登录按钮后,我会被重定向到
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"
}
现在我需要知道如何提取用户名,地址并通过电子邮件发送任何帮助吗?
提前致谢
答案 0 :(得分:16)
试试这个:
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');
}
});
答案 1 :(得分:12)
您可以使用此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",
...
...
}
答案 2 :(得分:9)
我有同样的问题。我想提取用户信息。但是无法获得确切的链接。然后,我在Line number 54
浏览了Passport Google Strategy的代码。
我的范围是['profile', 'email']
获取请求
https://www.googleapis.com/oauth2/v3/userinfo?access_token={access_token}
响应
{
"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"
}
答案 3 :(得分:3)
您需要使用访问令牌(您在重定向网址中获取)才能访问Google的People API。查看规范here。
您可能会发现Google's OAuth2 playground非常有用,可以了解如何使用访问权限来访问Google的API。
祝你好运!答案 4 :(得分:1)
检查this。有演示
代码段:
$accountObj = call_api($_SESSION['accessToken'],"https://www.googleapis.com/oauth2/v1/userinfo");
call_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;
}
从帐户对象中,可以访问该名称:
$your_name = $accountObj->name;
答案 5 :(得分:1)
您可以使用快速入门并在此处查看示例:sample
以下是对People:get
答案 6 :(得分:1)
这是服务器端的一个简单的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);
});
});
});
答案 7 :(得分:1)
如果需要token_info,请将 tokeninfo 作为参数传递
https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=ya29.a0AfH6SMArZZITzn-...
如果要获取所有用户信息,请将 userinfo 作为参数传递
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": ""
}
不要将 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',
};
答案 8 :(得分:0)
如果您正在使用PHP
您可以选中here。简而言之,您正在寻找的代码就是这个
$payload = $client->verifyIdToken($id_token);
只要您添加所需的作用域,有效载荷就会具有您所需的信息。