Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

sample code for fetching yahoo profile data asynchronously using stor…

…ed oauth token
  • Loading branch information...
commit 2a3fd51ee092e71f71a64e1c2e6df9a143c28ae3 0 parents
erik authored
106 CustomSessionStore.php
@@ -0,0 +1,106 @@
+<?php
+
+require '../../yosdk/yahoo-yos-social-php-1fe1b43/lib/YahooSessionStore.inc';
+
+class CustomSessionStore implements YahooSessionStore {
+
+ function __construct($guid=NULL, $storage='yahooTokenStorage.php'){
+ $this->guid = $guid;
+ $this->storage = $storage;
+
+ //init w/ empty array
+ if (!is_file($this->storage)) {
+ file_put_contents($this->storage, serialize(array()));
+ }
+ }
+
+ /**
+ * Indicates if the session store has a request token.
+ *
+ * @return True if a request token is present, false otherwise.
+ */
+ function hasRequestToken() {
+ $token = $this->fetchRequestToken();
+ return $token ? true : false;
+ }
+
+ /**
+ * Indicates if the session store has an access token.
+ *
+ * @return True if an access token is present, false otherwise.
+ */
+ function hasAccessToken() {
+ $token = $this->fetchAccessToken();
+ return $token ? true : false;
+ }
+
+ /**
+ * Stores the given request token in the session store.
+ *
+ * @param $token A PHP stdclass object containing the components of
+ * the OAuth request token.
+ * @return True on success, false otherwise.
+ */
+ function storeRequestToken($token){
+ $storage = unserialize(file_get_contents($this->storage));
+ $storage[$token->key] = $token;
+ file_put_contents($this->storage, serialize($storage));
+ return true;
+ }
+
+ /**
+ * Fetches and returns the request token from the session store.
+ *
+ * @return The request token.
+ */
+ function fetchRequestToken($oauth_token=NULL){
+ $storage = unserialize(file_get_contents($this->storage));
+ return $storage[$oauth_token];
+ }
+
+ /**
+ * Clears the request token from the session store.
+ *
+ * @return True on success, false otherwise.
+ */
+ function clearRequestToken(){
+ $storage = unserialize(file_get_contents($this->storage));
+ unset($storage[$this->request_token]);
+ file_put_contents($this->storage, serialize($storage));
+ }
+
+ /**
+ * Stores the given access token in the session store.
+ *
+ * @param $token A PHP stdclass object containing the components of
+ * the OAuth access token.
+ * @return True on success, false otherwise.
+ */
+ function storeAccessToken($token){
+ $storage = unserialize(file_get_contents($this->storage));
+ $storage[$token->guid] = $token;
+ file_put_contents($this->storage, serialize($storage));
+ return true;
+ }
+
+ /**
+ * Fetches and returns the access token from the session store.
+ *
+ * @return The access token.
+ */
+ function fetchAccessToken(){
+ $storage = unserialize(file_get_contents($this->storage));
+ return $storage[$this->guid];
+ }
+
+ /**
+ * Clears the access token from the session store.
+ *
+ * @return True on success, false otherwise.
+ */
+ function clearAccessToken(){
+ $storage = unserialize(file_get_contents($this->storage));
+ unset($storage[$this->guid]);
+ file_put_contents($this->storage, serialize($storage));
+ }
+}
162 comments.php
@@ -0,0 +1,162 @@
+<?php
+
+//comments from some db query
+$comments = array(
+ array(
+ 'provider' => 'yahoo',
+
+ //yahoo user 1
+ 'id' => 'BG5BMUK24OOYGHWKTJBCX2TN5E',
+
+ 'text' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
+ 'date' => '1264631647'
+ ),
+ array(
+ 'provider' => 'twitter',
+ 'id' => '2',
+ 'text' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
+ 'date' => '1264631647'
+ ),
+ array(
+ 'provider' => 'facebook',
+ 'id' => '3',
+ 'text' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
+ 'date' => '1264631647'
+ ),
+ array(
+ 'provider' => 'facebook',
+ 'id' => '4',
+ 'text' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
+ 'date' => '1264631647'
+ ),
+ array(
+ 'provider' => 'yahoo',
+
+ //yahoo user 2
+ 'id' => 'VEGCGDXJ7FSR2PHSWPIR7EXMAQ',
+
+ 'text' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
+ 'date' => '1264631647'
+ ),
+ array(
+ 'provider' => 'facebook',
+ 'id' => '6',
+ 'text' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
+ 'date' => '1264631647'
+ ),
+ array(
+ 'provider' => 'google',
+ 'id' => '7',
+ 'text' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
+ 'date' => '1264631647'
+ ),
+ array(
+ 'provider' => 'twitter',
+ 'id' => '8',
+ 'text' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
+ 'date' => '1264631647'
+ ),
+ array(
+ 'provider' => 'yahoo',
+
+ //yahoo user 1
+ 'id' => 'BG5BMUK24OOYGHWKTJBCX2TN5E',
+
+ 'text' => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
+ 'date' => '1264631647'
+ ),
+);
+
+?>
+
+<ul id="comments">
+
+ <? // use index to disambiguate two comments from same user ?>
+ <? foreach($comments as $index => $comment): ?>
+
+ <li id="comment-<?= $index ?>-<?= $comment['provider'] ?>-<?= $comment['id'] ?>">
+
+ <!-- reveal profile markup id data avail -->
+ <div class="profile" style="display:none">
+ <div class="name"></div>
+ <img/>
+ </div>
+
+ <?= $comment['text'] ?><br/>
+ <?= $comment['date'] ?>
+ </li>
+ <? endforeach ?>
+</ul>
+
+<script type="text/javascript" src="http://yui.yahooapis.com/3.0.0b1/build/yui/yui-min.js"></script>
+<script>
+var Y = YUI();
+Y.use('node', 'io-base', 'json-parse', 'css3-selector', function (Y) {
+
+ //loop through all the comments (ref: http://developer.yahoo.com/yui/3/api/NodeList.html#method_each)
+ Y.all('#comments li').each(function (node, index, list) {
+
+ //comment id
+ var id = node.get('id');
+
+ //to keep demo simple, profile.php only handles yahoo profiles
+ if (-1 === id.indexOf('yahoo')) {
+ return;
+ }
+
+ //proxy url + yahoo guid
+ var uri = 'profile.php?guid=' + id.split('-')[3],
+
+ //xhr req callback
+ handleComplete = function (reqId, o, args) {
+
+ //extract req data
+ var json = Y.JSON.parse(o.responseText),
+ liNodeId = args.complete.split('=')[1];
+
+ //bail if error
+ if (json.error) {
+ Y.log(json);
+ return;
+ }
+
+ //get profile data
+ var familyName = json.success.query.results.profile.familyName,
+ givenName = json.success.query.results.profile.givenName,
+ picUrl = json.success.query.results.profile.image.imageUrl,
+
+ //get dom nodes
+ profileNode = Y.Node.get('#' + liNodeId + ' .profile'),
+ nameNode = Y.Node.get('#' + liNodeId + ' .name'),
+ imgNode = Y.Node.get('#' + liNodeId + ' img');
+
+ //show the name and/or pic if avail
+ if (familyName) {
+ nameNode.append(givenName + ' ' + familyName);
+ profileNode.setStyle('display', 'block');
+ }
+
+ if (picUrl) {
+ imgNode.set('src', picUrl);
+ profileNode.setStyle('display', 'block');
+ }
+ },
+ config = {
+
+ //define fn to call when req completes
+ 'on' : {
+ 'complete' : handleComplete
+ },
+
+ //pass the comment node's id to the callback so it can add data to it
+ 'arguments' : {
+ 'complete' : 'id=' + id
+ },
+
+ //bind callback to each request not to window, which would only call fn for last req
+ 'context' : this
+ };
+ Y.io(uri, config);
+ });
+});
+</script>
6 config.php
@@ -0,0 +1,6 @@
+<?php
+define('KEY', '');
+define('SECRET', '');
+define('APPID', '');
+define('CALLBACK', '');
+?>
23 init.php
@@ -0,0 +1,23 @@
+<?php
+
+require_once("../../yosdk/yahoo-yos-social-php-1fe1b43/lib/Yahoo.inc");
+require_once("CustomSessionStore.php");
+
+//define constants
+require 'config.php';
+
+$store = new CustomSessionStore();
+
+if ($_GET['oauth_token']) {
+ $request_token = $store->fetchRequestToken($_GET['oauth_token']);
+ $store->clearRequestToken($_GET['oauth_token']);
+ $access_token = YahooAuthorization::getAccessToken(KEY, SECRET, $request_token, $_GET['oauth_verifier']);
+ $store->storeAccessToken($access_token);
+ header('location: profile.php?guid='.$access_token->guid);
+} else {
+ $request_token = YahooAuthorization::getRequestToken(KEY, SECRET, CALLBACK);
+ $store->storeRequestToken($request_token);
+ $auth_url = YahooAuthorization::createAuthorizationUrl($request_token);
+ header('location: '.$auth_url);
+}
+?>
41 profile.php
@@ -0,0 +1,41 @@
+<?php
+
+require_once("../../yosdk/yahoo-yos-social-php-1fe1b43/lib/Yahoo.inc");
+require_once("CustomSessionStore.php");
+
+//define constants
+require 'config.php';
+
+function respond ($response)
+{
+ header('content-type: application/json');
+ echo json_encode($response);
+ die;
+}
+
+if (!$_GET['guid']) {
+ respond(array('error' => 'guid is required'));
+}
+
+$store = new CustomSessionStore($_GET['guid']);
+$access_token = $store->fetchAccessToken();
+
+$consumer = (object) array(
+ 'key' => KEY,
+ 'secret' => SECRET
+);
+
+//token will expire in < 30 sec, so try to refresh
+if (($access_token->tokenExpires >= 0) && ($access_token->tokenExpires - time()) < 30) {
+ YahooSession::accessTokenExpired($access_token, $consumer, APPID, $store);
+}
+
+//use yahoosession for convenient yql oauth requests
+$session = new YahooSession($consumer, $access_token, APPID);
+
+//more info: http://developer.yahoo.com/yql/console/?q=select%20image.imageUrl%2C%20familyName%2C%20givenName%20from%20social.profile%20where%20guid%3Dme
+$yql = sprintf('select image.imageUrl, familyName, givenName from social.profile where guid="%s"', $_GET['guid']);
+
+respond(array('success' => $session->query($yql)));
+
+?>
Please sign in to comment.
Something went wrong with that request. Please try again.