Php 需要来自谷歌的电子邮件地址以及我们从Alexa Skill收到的访问令牌吗?

Php 需要来自谷歌的电子邮件地址以及我们从Alexa Skill收到的访问令牌吗?,php,google-oauth,alexa,alexa-skill,Php,Google Oauth,Alexa,Alexa Skill,我能够在Alexa Skill中使用Google Auth v2链接帐户。我可以得到访问令牌。现在我想获得用户用来链接帐户的谷歌电子邮件地址。那么我怎样才能获得谷歌的电子邮件地址呢?使用Oauth2范围https://www.googleapis.com/auth/userinfo.email. 使用Google Library for PHP将范围添加到您的Google客户端示例中: $googleClient = new Google_Client(); // Your code here

我能够在Alexa Skill中使用Google Auth v2链接帐户。我可以得到访问令牌。现在我想获得用户用来链接帐户的谷歌电子邮件地址。那么我怎样才能获得谷歌的电子邮件地址呢?

使用Oauth2范围https://www.googleapis.com/auth/userinfo.email.

使用Google Library for PHP将范围添加到您的Google客户端示例中:

$googleClient = new Google_Client();
// Your code here with Client ID, Secret, ...
$googleClient->addScope('https://www.googleapis.com/auth/userinfo.email');
这将为您提供用户的电子邮件

if(isset($_GET['code'])) {
// Your code here, the login code is in $_GET['code']

/**
* Get access token
*/
$token = $googleClient->fetchAccessTokenWithAuthCode($_GET['code']);
$googleClient->setAccessToken($token['access_token']);

// Create Google Auth Service with the Google Client
$google_auth = new Google_Service_Oauth2($googleClient);

// Get the userinfo 
$google_auth_info = $google_auth->userinfo->get();

// Get the E-Mail:
$email = $google_auth_info->email;

}
我想你要找的部分是authinfo->get->email,但我添加了更多的上下文,让你看看如何使用它

不要忘记在您的Google开发者控制台中启用此范围:
->编辑->步骤2:Scopes

我建议通过Google People api,尝试person.get方法,它将返回完整的配置文件信息。userinfo终结点不可靠,只能返回有限的信息量。注意:userinfo终结点返回有限的信息量,但并不总是返回全部信息。更好的选择是使用People api,您可以将其用于相同的电子邮件、配置文件范围