使用谷歌&x2B;PHP的API——需要获得用户的电子邮件

使用谷歌&x2B;PHP的API——需要获得用户的电子邮件,php,google-api,Php,Google Api,我正在使用Google PHP API。文件相当平淡。。。我想允许用户连接他们的Google+信息,并使用它在我的数据库中注册用户。为了做到这一点,我需要得到他们的谷歌帐户使用的电子邮件。我似乎不知道该怎么做。在Facebook上,当用户连接到我的应用程序时,通过强制获得许可就足够简单了。有人知道吗?这是我用来抓取用户google+个人资料的代码,它工作正常,但用户可能没有在那里列出他们的电子邮件 include_once("$DOCUMENT_ROOT/../qwiku_src/php/goo

我正在使用Google PHP API。文件相当平淡。。。我想允许用户连接他们的Google+信息,并使用它在我的数据库中注册用户。为了做到这一点,我需要得到他们的谷歌帐户使用的电子邮件。我似乎不知道该怎么做。在Facebook上,当用户连接到我的应用程序时,通过强制获得许可就足够简单了。有人知道吗?这是我用来抓取用户google+个人资料的代码,它工作正常,但用户可能没有在那里列出他们的电子邮件

include_once("$DOCUMENT_ROOT/../qwiku_src/php/google/initialize.php");

$plus = new apiPlusService($client);
if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()) {
    $me = $plus->people->get('me');
    print "Your Profile: <pre>" . print_r($me, true) . "</pre>";
    // The access token may have been updated lazily.
    $_SESSION['token'] = $client->getAccessToken();
} else {
    $authUrl = $client->createAuthUrl();
    print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
include_once($DOCUMENT_ROOT/./qwiku_src/php/google/initialize.php);
$plus=新的apiPlusService($client);
如果(isset($_GET['code'])){
$client->authenticate();
$\会话['token']=$client->getAccessToken();
标题('Location:http://'.$\u SERVER['http\u HOST'].$\u SERVER['PHP\u SELF']);
}
如果(isset($\u会话['token'])){
$client->setAccessToken($_会话['token']);
}
如果($client->getAccessToken()){
$me=$plus->people->get('me');
打印“您的个人资料:”。打印($me,true)。”;
{
 "id": "00000000000000",
 "email": "fred.example@gmail.com",
 "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"
}
//访问令牌可能已被延迟更新。 $\会话['token']=$client->getAccessToken(); }否则{ $authUrl=$client->createAuthUrl(); 打印“”; }

如果没有用户的电子邮件地址,这有点违背了允许用户注册Google+的目的。任何更熟悉Google API的人都知道我是如何获得它的吗?

如果我遗漏了一些东西,请原谅,但是如果你没有他们的电子邮件地址,你怎么知道与他们链接的Google帐户?通常,你会提示用户输入他们谷歌账户的电子邮件,以便首先找到他们的个人资料

使用OAuth2,您可以通过
scope
参数请求权限。()我想你想要的范围是
https://www.googleapis.com/auth/userinfo.email
https://www.googleapis.com/auth/userinfo.profile

那么,一旦您获得了访问令牌,就很简单了。(我假设您能够将返回的授权码兑换为访问令牌?)只需向
https://www.googleapis.com/oauth2/v1/userinfo?access_token={accessToken}
,返回配置文件数据的JSON数组,包括电子邮件:

$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'];
        }
      }
 }
PHP库中必须有一个方法来发出该请求,但我找不到它。没有保证,但请尝试以下方法:

else {
    $authUrl = $client->createAuthUrl("https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile");
    print "<a class='login' href='$authUrl'>Connect Me!</a>";
}

无论如何,在您发布的代码中,只需将所需的范围传递给
createAuthUrl()

else{
$authUrl=$client->createAuthUrl(“https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile");
打印“”;
}

谷歌API PHP客户端现在支持UserInfo API

下面是一个小示例应用程序:

示例的重要部分如下所示:

  require_once 'google-api-php-client/apiClient.php';
  require_once 'google-api-php-client/contrib/apiOauth2Service.php';
我假设此代码段是通过包含授权代码的GET请求调用的。记住,要使其正常工作,必须包含或要求apiouth2service.php。就我而言,是这样的:

$googlePlus = new Google_Service_Plus($client);
$userProfile = $googlePlus->people->get('me');
$emails = $userProfile->getEmails();

祝你好运。

用今天的API更新:

这假设如下:

  • 您已使用正确的作用域对当前用户进行了身份验证
  • 您已使用客户端id和客户端密码设置$client

  • 我正在使用Google+服务,他们像在Facebook上一样使用我的应用程序。唯一的问题是,我无法确定如何确保获得电子邮件地址,以便将其输入数据库。我将更新完整脚本的代码,但省略包含所有敏感代码的“$client”变量。由于没有函数,似乎没有简单的方法来实现这一点。我可能通过在谷歌集团中挖掘找到了一个解决办法。如果我能让它工作,我会发布答案,这应该不会太难。看来我只能使用HTTP请求了。哦,不,肯定有。给我一两分钟。@Jacob,不保证所有语法都是正确的,但你可以评论一下:这对我有用:$google\u plus=new google\u Service\u Oauth2($client)$谷歌+>用户信息->获取()
    $googlePlus = new Google_Service_Plus($client);
    $userProfile = $googlePlus->people->get('me');
    $emails = $userProfile->getEmails();