【问题标题】:Using Google+ API for PHP -- Need to get the users email使用 Google+ API for PHP -- 需要获取用户的电子邮件
【发布时间】:2014-05-04 14:20:13
【问题描述】:

我正在使用 Google PHP API。文档相当乏味......我希望允许用户连接他们的 Google+ 信息并使用它来在我的数据库中注册用户。为此,我需要获取他们用于 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>";
}

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

【问题讨论】:

    标签: php google-api


    【解决方案1】:

    Google API PHP 客户端现在支持 UserInfo API。

    这是一个小示例应用程序: http://code.google.com/p/google-api-php-client/source/browse/trunk/examples/userinfo/index.php

    示例的重要部分在这里:

      $client = new apiClient();
      $client->setApplicationName(APP_NAME);
      $client->setClientId(CLIENT_ID);
      $client->setClientSecret(CLIENT_SECRET);
      $client->setRedirectUri(REDIRECT_URI);
      $client->setDeveloperKey(DEVELOPER_KEY);
      $client->setScopes(array('https://www.googleapis.com/auth/userinfo.email',
            'https://www.googleapis.com/auth/plus.me'));      // Important!
    
      $oauth2 = new apiOauth2Service($client);
    
      // Authenticate the user, $_GET['code'] is used internally:
      $client->authenticate();
    
      // Will get id (number), email (string) and verified_email (boolean):
      $user = $oauth2->userinfo->get();
      $email = filter_var($user['email'], FILTER_SANITIZE_EMAIL);
      print $email;
    

    我假设这个 sn-p 是通过包含授权代码的 GET 请求调用的。请记住包含或要求 apiOauth2Service.php 以使其正常工作。在我的情况下是这样的:

      require_once 'google-api-php-client/apiClient.php';
      require_once 'google-api-php-client/contrib/apiOauth2Service.php';
    

    祝你好运。

    【讨论】:

    【解决方案2】:

    使用今天的 API 更新:

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

    这假设如下:

    1. 您已使用适当的范围对当前用户进行了身份验证。
    2. 您已经使用您的 client_id 和 client_secret 设置了 $client。

    【讨论】:

    • 这对我有用:$google_plus = new Google_Service_Oauth2($client); $google_plus->userinfo->get()
    【解决方案3】:

    如果我遗漏了什么,请原谅我,但是如果您没有他们的电子邮件地址,您怎么知道要将他们与哪个 Google 帐户关联?通常,您会提示用户输入其 Google 帐户的电子邮件,以便首先找到他们的个人资料。

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

    那么,在您获得访问令牌后,get the profile info 就很简单了。 (我假设您已经能够将返回的授权代码兑换为访问令牌?)只需向 https://www.googleapis.com/oauth2/v1/userinfo?access_token={accessToken} 发出 get 请求,它会返回一个 JSON 数组的配置文件数据,包括电子邮件:

    {
     "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"
    }
    

    PHP 库中必须有一个方法来发出该请求,但我找不到它。 不能保证,但试试这个:

    $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'];
            }
          }
     }
    

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

    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>";
    }
    

    【讨论】:

    • 我正在使用 Google+ 服务,他们像在 Facebook 中一样对我的应用程序进行 OAuth。唯一的问题是我不知道如何确保获得电子邮件地址,以便我可以将它们输入到我的数据库中。我将更新完整脚本的代码,但省略包含所有敏感代码的“$client”变量。
    • 似乎没有简单的方法来解决这个问题,因为没有功能。通过在 Google Group 中四处挖掘,我可能找到了解决方法。如果我得到这个工作,我会发布答案,这不应该太难。看来我只需要使用 HTTP 请求。 groups.google.com/forum/#!topic/google-api-php-client/…
    • 哦不,肯定有。给我一两分钟。
    • @Jacob,不能保证所有的语法都是正确的,但是你去吧。
    猜你喜欢
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 2019-12-22
    • 2013-09-05
    • 1970-01-01
    • 2014-05-05
    相关资源
    最近更新 更多