首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在PHP中生成访问令牌后获取用户邮件

如何在PHP中生成访问令牌后获取用户邮件
EN

Stack Overflow用户
提问于 2020-12-09 18:36:59
回答 1查看 36关注 0票数 1

我已经用PHP Oauth 2.0成功地生成了一个访问令牌和刷新令牌,使用以下代码,我想将刷新令牌和用户电子邮件存储在我的数据库中。我有刷新令牌,但不确定如何获取用户电子邮件。

代码语言:javascript
运行
复制
<?php 
require  '../vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfig('client_secret_234rcontent.com.json');
$client->addScope('https://www.googleapis.com/auth/calendar');
$client->setRedirectUri('https://' . $_SERVER['HTTP_HOST'] . 
 '/google_calendar2/oauth2callback.php');
 // offline access will give you both an access and refresh token so that
 // your app can refresh the access token without user interaction.
 $client->setAccessType('offline');
 // Using "consent" ensures that your application always receives a refresh token.
// If you are not using offline access, you can omit this.
$client->setPrompt('consent');
$client->setApprovalPrompt("consent");
$client->setIncludeGrantedScopes(true);   // incremental auth
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));

重定向uri代码

代码语言:javascript
运行
复制
<?php
require  'vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfigFile('client_secret_2344056t4.apps.googleusercontent.com.json');
$client->setRedirectUri('https://' . $_SERVER['HTTP_HOST'] . 
'/google_calendar2/oauth2callback.php');
$client->addScope('https://www.googleapis.com/auth/calendar');
$credentials=$client->authenticate($_GET['code']);
$access_token = $client->getAccessToken();
// $refresh_token = $credentials['refresh_token'];
print_r($credentials);

resposne enter image description here

EN

回答 1

Stack Overflow用户

发布于 2020-12-09 23:15:54

我不使用Google APK,但使用普通的API请求,您可以这样做:

GEThttps://www.googleapis.com/oauth2/v3/userinfo?access_token={access_token}

响应:

代码语言:javascript
运行
复制
{
  "sub": "116289810291163096502",
  "name": "John Smith",
  "given_name": "John",
  "family_name": "Smith",
  "picture": "https://lh3.googleusercontent.com/a-/AOh10GhaIqIxRM0PbSwNMA7hFltGP7P1CzvUbCtcpyDHqg=s96-c",
  "email": "john.smith@example.com",
  "email_verified": true,
  "locale": "en",
  "hd": "example.com"
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65215105

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档