javascript 如何使用谷歌访问令牌获取用户电子邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15104682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get user email with google access token?
提问by Konga Raju
I followed all these steps.
我遵循了所有这些步骤。
https://developers.google.com/+/web/signin/
https://developers.google.com/+/web/signin/
I have client id and client secret.
我有客户 ID 和客户机密。
I got access token now, how can I get user profile and email with access token? And how to check whether user logged in or not?
我现在获得了访问令牌,如何使用访问令牌获取用户个人资料和电子邮件?以及如何检查用户是否登录?
回答by Vineet1982
Using OAuth2, you can request permissions through the scope parameter. (Documentation.) I imagine the scopes you want are https://www.googleapis.com/auth/userinfo.emailand https://www.googleapis.com/auth/userinfo.profile.
使用 OAuth2,您可以通过 scope 参数请求权限。(文档。)我想你想要的范围是https://www.googleapis.com/auth/userinfo.email和https://www.googleapis.com/auth/userinfo.profile。
Then, it's a simple matter to get the profile info once you've obtained your access token. (I assume you've been able to redeem the returned authorization code for an access token?) Just make a get request to https://www.googleapis.com/oauth2/v1/userinfo?access_token={accessToken}, which returns a JSON array of profile data, including email:
然后,一旦获得访问令牌,获取配置文件信息就很简单了。(我假设您已经能够将返回的授权码兑换为访问令牌?)只需向https://www.googleapis.com/oauth2/v1/userinfo?access_token={accessToken}发出获取请求,它会返回配置文件数据的 JSON 数组,包括电子邮件:
{
"id": "00000000000000",
"email": "[email protected]",
"verified_email": true,
"name": "Fred Example",
"given_name": "Fred",
"family_name": "Example",
"picture": "https://lh5.googleusercontent.com/-2Sv-4bBMLLA/AAAAAAAAAAI/AAAAAAAAABo/bEG4kI2mG0I/photo.jpg",
"gender": "male",
"locale": "en-US"
}
No guarantees, but try this:
没有保证,但试试这个:
$url = "https://www.googleapis.com/oauth2/v1/userinfo";
$request = apiClient::$io->makeRequest($client->sign(new apiHttpRequest($url, 'GET')));
if ((int)$request->getResponseHttpCode() == 200) {
$response = $request->getResponseBody();
$decodedResponse = json_decode($response, true);
//process user info
} else {
$response = $request->getResponseBody();
$decodedResponse = json_decode($response, true);
if ($decodedResponse != $response && $decodedResponse != null && $decodedResponse['error']) {
$response = $decodedResponse['error'];
}
}
}
回答by Dino
try this
试试这个
$accessToken = 'access token';
$userDetails = file_get_contents('https://www.googleapis.com/oauth2/v1/userinfo?access_token=' . $accessToken);
$userData = json_decode($userDetails);
if (!empty($userData)) {
$googleUserId = '';
$googleEmail = '';
$googleVerified = '';
$googleName = '';
$googleUserName = '';
if (isset($userData->id)) {
$googleUserId = $userData->id;
}
if (isset($userData->email)) {
$googleEmail = $userData->email;
$googleEmailParts = explode("@", $googleEmail);
$googleUserName = $googleEmailParts[0];
}
if (isset($userData->verified_email)) {
$googleVerified = $userData->verified_email;
}
if (isset($userData->name)) {
$googleName = $userData->name;
}
} else {
echo "Not logged In";
}
回答by Azam Alvi
You just add this line into your scope
Open your Application.cfc
and then add this code
您只需将此行添加到您的scope
Open yourApplication.cfc
然后添加此代码
<cfset request.oauthSettings =
{scope = "https://www.googleapis.com/auth/userinfo.email+https://www.googleapis.com/auth/userinfo.profile",
client_id = "Your-id",
client_secret = "your-secret",
redirect_uri = "redirect-page",
state = "optional"} />
Now you can get email from function that you can call like this
现在您可以从可以像这样调用的函数中获取电子邮件
<cfscript>
public function getProfile(accesstoken) {
var h = new com.adobe.coldfusion.http();
h.setURL("https://www.googleapis.com/oauth2/v1/userinfo");
h.setMethod("get");
h.addParam(type="header",name="Authorization",value="OAuth #accesstoken#");
h.addParam(type="header",name="GData-Version",value="3");
h.setResolveURL(true);
var result = h.send().getPrefix();
return deserializeJSON(result.filecontent.toString());
}
</cfscript>
<cfoutput>
<cfset show = getProfile(session.ga_accessToken)>
<cfdump var="#show#">
</cfoutput>
Hope this can Help many of people to solve this . :)
希望这可以帮助很多人解决这个问题。:)