Secure AuthSub using the Zend PHP library 1.6+


After uploading a public certificate to https://www.google.com/accounts/ManageDomains, here's how to use the Zend PHP 1.6+ library to work with secure AuthSub. This example uses the Google Health Data API
<?
function setupClient($singleUseToken = null) { 
  $client = null;  

  // Fetch a new AuthSub token?
  if (!$singleUseToken) {
    $next = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    $scope = 'https://www.google.com/health/feeds';
    $authSubHandler = 'https://www.google.com/health/authsub';    
    $secure = 1;
    $session = 1;
    $permission = 1;  // 1 - allows posting notices && allows reading profile data
    $authSubURL =  Zend_Gdata_AuthSub::getAuthSubTokenUri($next, $scope, $secure, $session, $authSubHandler);
    
    $authSubURL .= '&permission=' . $permission;
    
    echo '<a href="' . $authSubURL . '">Link your Google Health Account</a>';
  } else {
    $client = new Zend_Gdata_HttpClient();
    
    // This sets your private key to be used to sign subsequent requests
    $client->setAuthSubPrivateKeyFile('/path/to/myrsakey.pem', null, true);

    $sessionToken = Zend_Gdata_AuthSub::getAuthSubSessionToken(trim($singleUseToken), $client);
    // Set the long-lived session token for subsequent requests
    $client->setAuthSubToken($sessionToken);
  }
  return $client;
}
?>
Use this function like this:
$client = setupClient(@$_GET['token']);
if ($client) {
  // Query a feed
} else {
  exit(); // Just display the AuthSub link
}

Update:Read the new documentation on using AuthSub for PHP as well as all the other client libraries.

17 comments:

Niv Singer said...

thanks, this was really helpful!

nobutaka said...

I love you dude, very usefull post.

Unknown said...

Do I need an $authSubHandler for youtube scope? My code works fine with $secure=0, but when $secure=1, I get an exception in getAuthSubSessionToken() -- "Invalid AuthSub header" 401.

This follows the "Allow Access" page with "Registered, secure. This website is registered with Google ..."

My code is taken from code.google.com/apis/gdata/authsub.html (PHP). Thanks!

Eric (Google) said...

Not for YouTube. You should be able to get away with:
Zend_Gdata_AuthSub::getAuthSubTokenUri($next, $scope, $secure, $session)

for requesting the token and:
$client = new Zend_Gdata_HttpClient();
$client->setAuthSubPrivateKeyFile('/path/to/myrsakey.pem', null, true);
$sessionToken =
Zend_Gdata_AuthSub::getAuthSubSessionToken($singleUseToken, $client);

...for uprading the token. Check that you're setting
your private key (and the file is readable) before making the exchange.

Feel free to post in the Accounts APIs forum if you have questions:
http://groups.google.com/group/Google-Accounts-API

Unknown said...

Thanks Eric! All looks well in my code, but it fails. I'll post a follow-up on the Accounts-API group.

Yonas said...

I finally solved my "401 Unknown authorization header" problem:

function getAuthSubHttpClient($err_func)
{
$sToken = HTTP_Session2::get('sessionToken');

if (!isset($sToken) && !isset($_GET['token']) ){
call_user_func($err_func, "redirect");
return;
} else if (!isset($sToken) && isset($_GET['token'])) {
$client = new Zend_Gdata_HttpClient();
$client->setAuthSubPrivateKeyFile('/path/to/myrsakey.pem', null, true);

$sToken = Zend_Gdata_AuthSub::getAuthSubSessionToken(trim($_GET['token']), $client);
}

HTTP_Session2::set('sessionToken', $sToken);
$httpClient = Zend_Gdata_AuthSub::getHttpClient($sToken);
$httpClient->setAuthSubPrivateKeyFile('/path/to/myrsakey.pem', null, true);

return $httpClient;
}

function getAuthSubRequestUrl()
{
$next = 'http://example.com/login.php';
$scope = 'http://gdata.youtube.com';
$secure = true;
$session = true;
return Zend_Gdata_AuthSub::getAuthSubTokenUri($next, $scope, $secure, $session);
}


The fix was to add the second call to $httpClient->setAuthSubPrivateKeyFile before return $httpClient. This allows every call to getAuthSubHttpClient() to know the private key's location, not just the first call.

Yonas said...

Also:

If your private key is password protected, you need to pass the password as the second argument to setAuthSubPrivateKeyFile().

ClearTours said...

Yonas,

Thanks for posting that update! my brain was burning up trying to figure that same problem out.

Unknown said...

ok

How to get username when you use youtube api to get authentication with authsub youtube api

Zend gdata library

Kruspex said...

To get the usernamne you have to do this:

This is for Symfony2 but you can change it for your project:

$yt = new \Zend_Gdata_YouTube($httpClient,"anonymous","",$developer_key);

$username = $yt->getUserProfile("default")->username->text;

if you put "default" it returns the username that have accepted permissions.

(soyy about my english)

kosovohp03 said...

healthtips
healthtips
healthtips
healthtips
healthtips




socks proxy
socks proxy
socks proxy
socks proxy
socks proxy
socks proxy
socks proxy
socks proxy
socks proxy
socks proxy
check socks proxy
socks proxy
socks proxy






funnystory
funnystory
funnystory
funnystory
funnystory




funny pictures
funny pictures
funny pictures
funny pictures
funny pictures
funny pictures






Girl Xinh
Girl Xinh
Girl Xinh
Girl Xinh
Girl Xinh
Girl Xinh
Girl Xinh





cute animal
food news
internet
football news
bank news
marketing
science news
travel vietnamt
real estate vietnam
business news
music news
health news
software
seo news

krishna said...

flipkart big billion day offers,
flipkart big shopping days,
flipkart big shopping days sale,
flipkart big shopping days 2018,
flipkart big shopping days offers,
flipkart big shopping days sale 2018,
flipkart next big shopping days,
big shopping days in flipkart,
flipkart online shopping big billion day,
flipkart big apple shopping day,
flipkart big shopping days 2017,
flipkart big shopping days terms and conditions,
flipkart big shopping days sbi offer,
flipkart big shopping days hdfc offer,
flipkart big shopping days mobiles,
flipkart big shopping days 2018 redmi note 5 pro,
flipkart big shopping days 2018 offers,
flipkart big shopping days date,
flipkart big shopping days hdfc,
flipkart big shopping days oppo f7,
flipkart big billion day online shopping,
big app shopping days in flipkart,
flipkart big shopping days 2016,
flipkart big shopping days 2017 sbi offer,
flipkart big shopping days july 2018,
flipkart big shopping days may,
flipkart big shopping days may 2018

krishna said...


flipkart coupons,
flipkart coupons code,
flipkart coupons discount,
flipkart coupons code for new user,
flipkart coupons mobile,
flipkart coupons on mobile,
flipkart coupons for today,
flipkart coupons free,
flipkart coupons today,
flipkart coupons for mobiles,
flipkart coupons code generator,
flipkart gift coupons,
flipkart coupons on books,
flipkart coupons shoes,
flipkart coupons books,
flipkart coupons for new users,
flipkart coupons for books,
flipkart laptops coupons,
flipkart coupons electronics,
flipkart coupons for watches,
flipkart coupons on watches,
flipkart coupons watches,
flipkart coupons for hdfc,
flipkart coupons for laptops,
flipkart coupons hdfc,
flipkart coupons for mobiles today,
flipkart mobile coupons today,
flipkart coupons 2018,
flipkart coupons on footwear,
get flipkart coupons,
flipkart gift coupons free,
flipkart coupons mobile phones,
flipkart coupons and offers,
flipkart coupons grabon,
flipkart refrigerator coupons,
flipkart coupons 100 rs off,
earn flipkart coupons,
flipkart discount coupons for led tv,
flipkart diaper coupons,
flipkart coupons for furniture,
flipkart coupons for fastrack watches,
flipkart coupons for beauty products,
flipkart discount coupons 2018,
flipkart phone coupons,
flipkart coupons coupondunia,
flipkart coupons for men's wallets,
flipkart coupons for kitchen appliances,
flipkart coupons for first time user,
flipkart coupons today in india,
flipkart coupons for washing machine,
flipkart gift voucher coupons,
flipkart coupons iphone,
flipkart discount coupons for laptops,
flipkart discount coupons code for shoes,
flipkart tablet coupons,
flipkart helmet coupons,
flipkart coupons on mobile accessories,
flipkart coupons above 500,
flipkart discount coupons on headphones,
flipkart saree coupons,
flipkart sign up coupons,
flipkart coupons for electronics today,
flipkart coupons promo code,
flipkart trimmer coupons,
flipkart bags coupons,
flipkart coupons on earphones,
flipkart coupons for trimmers,
flipkart coupons for home appliances,
flipkart coupons on tv,
flipkart coupons codes for external hard disk,
flipkart coupons balance,
flipkart printer coupons,
flipkart coupons for sbi credit card,
flipkart coupons for led tv,
flipkart coupons on toys,
flipkart coupons latest,
flipkart coupons tricks,
flipkart promotional coupons
flipkart working coupons
flipkart coupons hdfc bank
flipkart coupons for headphones
flipkart headphones coupons
win flipkart coupons
flipkart free coupons hack
flipkart secret coupons
flipkart coupons for women's clothing,
flipkart coupons how to use,
flipkart promo coupons,
flipkart coupons axis bank,
flipkart coupons for games,
flipkart coupons for wallets,
flipkart coupons hack,
flipkart coupons icici,
flipkart coupons new user,
flipkart coupons purchase,
flipkart bank coupons,
flipkart coupons affiliate,
flipkart coupons august 2017,
flipkart coupons for icici credit card,
flipkart coupons for iphone 6,
flipkart coupons for jackets,
flipkart coupons for nike shoes,
flipkart coupons for redmi note 4,
flipkart coupons for wifi routers,
flipkart coupons groupon,
flipkart coupons july 2017,
flipkart coupons june 2017,
flipkart coupons june 2018,
flipkart coupons on power banks,
flipkart coupons sbi,
flipkart coupons september 2017,
flipkart discount coupons on kitchen a,ppliances,
flipkart e coupons,
flipkart coupons api

krishna said...


flipkart fashion sale,
flipkart fashion sale 70 off,
flipkart fashion sale 2018,
flipkart fashion sale gowns,
flipkart fashion sale kurtis,
flipkart fashion sale ad girl,
flipkart fashion sale date,
flipkart fashion sale tops,
flipkart fashion sale shoes,
flipkart fashion sale 2017,
flipkart summer sale 2018,
flipkart summer sale,
flipkart summer sale offers,
flipkart summer sale 2018,
flipkart summer sale,
flipkart summer internship 2018,
flipkart summer internship 2019,
flipkart summer collection



flipkart freedom sale 2018,
flipkart the freedom sale,
flipkart big freedom sale 2018,
flipkart freedom sale 2018 dates,
flipkart big freedom sale 2018 date,
flipkart freedom sale offer,
freedom sale in flipkart,
flipkart freedom sale 2017,
flipkart freedom sale 2019,
flipkart freedom sale date,
best deals on flipkart freedom sale,
flipkart the big freedom sale 2018,
flipkart the big freedom sale 2017,
flipkart next freedom sale,
flipkart big freedom sale 2018 offers,
flipkart freedom sale hdfc offer,
flipkart great freedom sale,
what is flipkart freedom sale



flipkart hdfc,
flipkart hdfc offer,
flipkart hdfc credit card offer,
flipkart offers for hdfc credit card,
flipkart hdfc offer terms and conditions,
flipkart hdfc debit card emi,
flipkart hdfc card offers,
flipkart hdfc debit card offer,
flipkart coupons for hdfc,
flipkart hdfc offer 2018,
flipkart offer with hdfc credit card,
flipkart hdfc smart buy,
flipkart hdfc offer august 2017,
flipkart emi hdfc debit card,
flipkart hdfc debit card emi offer,
flipkart hdfc emi offer,
flipkart loan from hdfc,
flipkart hdfc promo code,
flipkart sale hdfc offer,
flipkart hdfc emi,
flipkart and hdfc offer,
flipkart hdfc terms and conditions,
flipkart hdfc no cost emi,
flipkart hdfc bank,
flipkart hdfc credit card emi,
flipkart hdfc sale,
flipkart and hdfc,
flipkart and hdfc credit card offer,
flipkart discount hdfc credit card,
flipkart freedom sale hdfc offer,
flipkart hdfc 10 instant ,
flipkart hdfc bank offer 2017,
flipkart hdfc offer 2017,
flipkart hdfc offer july 2017,
flipkart hdfc offer may 2017

digitals said...

new social bookmarking sites list
free social bookmarking sites list with high pr
high pr social bookmarking sites list
top social bookmarking sites for seo
bookmarking sites
free social bookmarking sites list
social bookmarking list
social bookmarking sites list
best social bookmarking sites
sbm sites

Geremy said...

dynamo who is widely recognized as dynamo l is one of India's top professional PUBG Mobile players. He is the head of team SouL, a PUBG Mobile Esports team based in India. dynamo gaming net worth It is personally my favorite one, have spent hours on it,

Unknown said...

Graphology is the study of handwriting as a means of analyzing character. Also called handwriting analysis. Graphology in https://www.nirvaanalife.com/this sense is not a branch of linguistics. The term graphology is derived from the Greek words for "writing" and "study." Read More about nirvaanalife Courses