使用Google+ API for PHP - 需要获取用户电子邮件 [英] Using Google+ API for PHP -- Need to get the users email

查看:200
本文介绍了使用Google+ API for PHP - 需要获取用户电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google PHP API。文档相当乏味......我想让用户连接他们的Google+信息,并使用它来为我的数据库中的用户签名。为了做到这一点,我需要获得他们用于他们的谷歌帐户的电子邮件。我似乎无法弄清楚如何。当用户连接到我的应用程序时,通过强制许可,Facebook很容易。任何人有任何想法?这是我用来抓取用户google +个人资料的代码,并且工作正常,除非用户可能没有在那里列出他们的电子邮件。

I'm using the Google PHP API. The documentation is rather lackluster... I want to allow users to connect their Google+ information and also use it to sign the users up in my database. In order to do that I need to get the email they use for their google account. I can't seem to figure out how to. It's easy enough in Facebook by forcing the permission when the users connect to my app. Anyone have any idea? This is the code I'm using to grab the users google+ profile and it works fine, except users may not have their email listed there.

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知道如何获得它?

Without the users email address, it sort of defeats the purpose of allowing users to signup with Google+ Anyone more familiar with the Google API know how I can get it?

推荐答案

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

Here's a small sample application: http://code.google.com/p/google-api-php-client/source/browse/trunk/examples/userinfo/index.php

示例的重要部分在这里:

The important bit of the sample is here:

  $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;

我假设这段代码是用包含授权码的GET请求调用的。请记住包含或需要apiOauth2Service.php才能使其工作。在我的情况是这样的:

I'm assuming this snippet was called with a GET request including the authorization code. Remember to include or require apiOauth2Service.php in order for this to work. In my case is something like this:

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

祝您好运。

这篇关于使用Google+ API for PHP - 需要获取用户电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆