使用PHP和OAuth,I';我正在尝试验证和检索谷歌帐户的电子邮件地址和名称

使用PHP和OAuth,I';我正在尝试验证和检索谷歌帐户的电子邮件地址和名称,php,oauth,Php,Oauth,我真的不知道从这里到哪里去。我使用的是谷歌代码中的“oauth php”库,可在此处找到: 我甚至可以用“oauth_校验器”和“oauth_令牌”变量接收HTTP响应。我该怎么办?如何访问经过身份验证的用户的电子邮件地址以及他们的姓名?我真的只想知道他们的电子邮件地址,比如“user@example.com“ 万分感谢!以下是我目前的代码: <?php // SETTINGS error_reporting(E_ALL); ini_set('display_errors', 1);

我真的不知道从这里到哪里去。我使用的是谷歌代码中的“oauth php”库,可在此处找到:

我甚至可以用“oauth_校验器”和“oauth_令牌”变量接收HTTP响应。我该怎么办?如何访问经过身份验证的用户的电子邮件地址以及他们的姓名?我真的只想知道他们的电子邮件地址,比如“user@example.com“

万分感谢!以下是我目前的代码:

<?php

// SETTINGS
error_reporting(E_ALL);
ini_set('display_errors', 1);

// INCLUDES
include_once './php/OAuth/OAuthStore.php';
include_once './php/OAuth/OAuthRequester.php';

// CONSTANTS
define('CONSUMER_KEY',      '...');
define('CONSUMER_SECRET',   '...');
define('OAUTH_HOST',        'https://www.google.com');
define('REQUEST_TOKEN_URL', OAUTH_HOST . '/accounts/OAuthGetRequestToken');
define('AUTHORIZE_URL',     OAUTH_HOST . '/accounts/OAuthAuthorizeToken');
define('ACCESS_TOKEN_URL',  OAUTH_HOST . '/accounts/OAuthGetAccessToken');
define('OAUTH_TMP_DIR',     function_exists('sys_get_temp_dir') ? sys_get_temp_dir() : realpath($_ENV['TMP']));

//  Init the OAuthStore
$options = array(
    'consumer_key'      => CONSUMER_KEY,
    'consumer_secret'   => CONSUMER_SECRET,
    'server_uri'        => OAUTH_HOST,
    'request_token_uri' => REQUEST_TOKEN_URL,
    'authorize_uri'     => AUTHORIZE_URL,
    'access_token_uri'  => ACCESS_TOKEN_URL
);
// Note: do not use "Session" storage in production. Prefer a database
// storage, such as MySQL.
OAuthStore::instance('Session', $options);

// OAUTH
try
{
    // STEP 1:  If we do not have an OAuth token yet, go get one
    if (empty($_GET['oauth_token']))
    {
        $getAuthTokenParams = array(
            'scope'                 => 'http://docs.google.com/feeds/',
            'xoauth_displayname'    => 'OAuth test',
            'oauth_callback'        => '...'
        );
        $tokenResult = OAuthRequester::requestRequestToken(CONSUMER_KEY, 0, $getAuthTokenParams);

        //  redirect to the google authorization page, they will redirect back
        header('Location: ' . AUTHORIZE_URL . '?btmpl=mobile&oauth_token=' . $tokenResult['token']);
    }
    else
    {
        //  STEP 2:  Get an access token
        $oauthToken     = $_GET['oauth_token'];
        $tokenResult    = $_GET;
        try {
            OAuthRequester::requestAccessToken(CONSUMER_KEY, $oauthToken, 0, 'POST', $_GET);
        } catch (OAuthException2 $e) {
            return;
        }
    }
}
catch (OAuthException2 $e)
{
    echo 'OAuthException: ' . $e->getMessage();
    var_dump($e);
}

下面是一个示例应用程序,演示如何将userinfo API(提供用户的电子邮件地址)用于: