Note that there are some explanatory texts on larger screens.

plurals
  1. POAfter successfully connecting to Google Contacts API via php, how/what do I store in database to reconnect later?
    primarykey
    data
    text
    <p>So here's the deal.</p> <p>I've connected to Google contact's api via php, stored everything in sessions, and retrieved a list of contacts. </p> <p>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. </p> <p>I imagine the answer is simple to someone who understands OAuth well - it's really just a question of what do I store.</p> <p>Code below:</p> <pre><code>&lt;?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' =&gt; GOOGLE_CONSUMER_KEY, 'consumer_secret' =&gt; GOOGLE_CONSUMER_SECRET, 'server_uri' =&gt; GOOGLE_OAUTH_HOST, 'request_token_uri' =&gt; GOOGLE_REQUEST_TOKEN_URL, 'authorize_uri' =&gt; GOOGLE_AUTHORIZE_URL, 'access_token_uri' =&gt; 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' =&gt; 'https://www.google.com/m8/feeds/', 'xoauth_displayname' =&gt; 'My web app', 'oauth_callback' =&gt; '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&amp;oauth_token=" . $tokenResultParams['token']); } else { // STEP 2: Get an access token $oauthToken = $_GET["oauth_token"]; $oauthVerifier = $_GET["oauth_verifier"]; $tokenResultParams = $_GET; //$db-&gt;query("UPDATE gmkeys SET token='$oauthToken', secrettoken='$oauthVerifier'"); try { OAuthRequester::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&amp;group=http%3A%2F%2Fwww.google.com%2Fm8%2Ffeeds%2Fgroups%2Fusernamehere%40gmail.com%2Fbase%2F6", 'GET', $tokenResultParams); $result = $request-&gt;doRequest(0); if ($result['code'] == 200) { $xml = new SimpleXMLElement($result['body']); ... } else { echo 'Error'; } } } catch(OAuthException2 $e) { echo "OAuthException: " . $e-&gt;getMessage(); var_dump($e); } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload