开发者

Using PHP and OAuth, I'm trying to authenticate and retrieve the email address and name of a Google account

I don't really know where to go from here. I'm using the "oauth-php" library from Google Code found here: http://code.google.com/p/oauth-php/

I'm able to go as far as receiving an HTTP response with the "oauth_verifier" and "oauth_token" variables. What do I do from here? How do I access the authenticated user's email address and possibly their name? I really JUST want to know their email address like "user@example.com".

Thanks a million! Here's my code so far:

<?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_H开发者_Python百科OST . '/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);
}


Here's a sample application demonstrating how you can use the userinfo API (which provides the user's email address) with the Google API PHP Client:

http://code.google.com/p/google-api-php-client/source/browse/trunk/examples/userinfo/index.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜