After successfully connecting to Google Contacts API via php, how/what do I store in database to reconnect later?
So here's the deal.
I've connected to Google contact's api via php, stored everything in sessions, and retrieved a list of contacts.
What I want is to store all the necessary tokens in a database, and retrieve them at a later time to re-use them on the same user. I can't figure out what information to store... I've attempted storing every little item from the session into a database and reloading it when I try to reconnect to the api, but always get one error or another because my tokens aren't correct.
I imagine the answer is simple to someone who understands OAuth well - it's really just a question of what do I store.
Code below:
<?php
session_start();
include_once "../oauth-php/library/OAuthStore.php";
include_once "../oauth-php/library/OAuthRequester.php";
global $db;
$userid=$_SESSION['userid'];
define("GOOGLE_CONSUMER_KEY", "website.com"); //
define("GOOGLE_CONSUMER_SECRET", "----------------------"); //
define("GOOGLE_OAUTH_HOST", "https://www.google.com");
define("GOOGLE_REQUEST_TOKEN_URL", GOOGLE_OAUTH_HOST . "/accounts/OAuthGetRequestToken");
define("GOOGLE_AUTHORIZE_URL", GOOGLE_OAUTH_HOST . "/accounts/OAuthAuthorizeToken");
define("GOOGLE_ACCESS_TOKEN_URL", GOOGLE_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' => GOOGLE_CONSUMER_KEY,
'consumer_secret' => GOOGLE_CONSUMER_SECRET,
'server_uri' => GOOGLE_OAUTH_HOST,
'request_token_uri' => GOOGLE_REQUEST_TOKEN_URL,
'authorize_uri' => GOOGLE_AUTHORIZE_URL,
'access_token_uri' => GOOGLE_ACCESS_TOKEN_URL
);
OAuthStore::instance("Session", $options);
try
{
// STEP 1: If we do not have an OAuth token yet, go get one
if (empty($_GET["oauth_token"]))
{
$getAuthTokenParams = array('scope' =>
'https://www.google.com/m8/feeds/',
'xoauth_displayname' => 'My web app',
'oauth_callback' => 'http://website.com/google.php');
// get a request token
$tokenResultParams = OAuthRequester::requestRequestToken(GOOGLE_CONSUMER_KEY, 0, $getAuthTokenParams);
// redirect to the google authorization page, they will redirect back
header("Location: " . GOOGLE_AUTHORIZE_URL . "?btmpl=mobile&oauth_token=" . $tokenResultParams['token']);
}
else {
// STEP 2: Get an access token
$oauthToken = $_GET["oauth_token"];
$oauthVerifier = $_GET["oauth_verifier"];
$tokenResultParams = $_GET;
//$db->query("UPDATE gmkeys SET token='$oauthToken', secrettoken='$oauthVerifier'");
try {
OAuthReques开发者_运维技巧ter::requestAccessToken(GOOGLE_CONSUMER_KEY, $oauthToken, 0, 'POST', $_GET);
}
catch (OAuthException2 $e)
{
var_dump($e);
// Something wrong with the oauth_token.
// Could be:
// 1. Was already ok
// 2. We were not authorized
return;
}
// make the request.
$request = new OAuthRequester("https://www.google.com/m8/feeds/contacts/default/full?max-results=1000&group=http%3A%2F%2Fwww.google.com%2Fm8%2Ffeeds%2Fgroups%2Fusernamehere%40gmail.com%2Fbase%2F6", 'GET', $tokenResultParams);
$result = $request->doRequest(0);
if ($result['code'] == 200)
{
$xml = new SimpleXMLElement($result['body']);
...
}
else
{
echo 'Error';
}
}
}
catch(OAuthException2 $e) {
echo "OAuthException: " . $e->getMessage();
var_dump($e);
}
?>
The access_token and the request_token strings
精彩评论