我已经用PHP Oauth 2.0成功地生成了一个访问令牌和刷新令牌,使用以下代码,我想将刷新令牌和用户电子邮件存储在我的数据库中。我有刷新令牌,但不确定如何获取用户电子邮件。
<?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代码
<?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
发布于 2020-12-09 23:15:54
我不使用Google APK,但使用普通的API请求,您可以这样做:
GET
:https://www.googleapis.com/oauth2/v3/userinfo?access_token={access_token}
响应:
{
"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"
}
https://stackoverflow.com/questions/65215105
复制相似问题