By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,317 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,317 IT Pros & Developers. It's quick & easy.

Google AuthSub Authentication + jQuery + Ajax

P: 42
I've been trying to get Google's AuthSub authentication to work for me, but to no avail. I can get the token (see below) but I cannot use the token in any way.

-To get the token, the user clicks a link where href="https://www.google.com/accounts/AuthSubRequest?next=http%3A%2F%2Fdev.MyTestSite.co m%2F&scope=https%3A%2F%2Fgdata.youtube.com&session =1&secure=0"

That redirects correctly with a token (which has more characters than any of their examples).

-HERE'S THE ISSUE: To turn the single use token into a session token, I've tried this:

Expand|Select|Wrap|Line Numbers
  1. $.ajax({
  2.            url: "https://www.google.com/accounts/AuthSubSessionToken"
  3.         ,headers: {     
  4.             "Host": "www.google.com"
  5.             ,"Content-Type": "application/x-www-form-urlencoded"
  6.             ,"Authorization": authSub
  7.             ,"User-Agent": "Java/1.5.0_06"
  8.             ,"Accept": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
  9.             ,"Connection": "keep-alive"}
  10.         ,success: 
  11.             function(data) {
  12.                 alert('Data Loaded: ' + data);}
  13.         ,type: 'GET'
  14.         ,dataType: "jsonp"
  15.         ,crossDomain: true
  16.         });
  17.  
This is returning a 401 Unauthorized error. When I look at the request in firebug, I don't see the correct request headers. Is there something that I'm doing wrong?
Dec 2 '11 #1

✓ answered by Marknut

I just wanted to share the solution to my problem. I still couldn't get AJAX to work for this GET method, so I used cURL in PHP:

To upgrade the token to a session token:
Expand|Select|Wrap|Line Numbers
  1.     function sendGetMethod($requestURL, $token) {   
  2.         //Secure example = http://gdatatips.blogspot.com/2008/07/secure-authsub-in-php.html
  3.  
  4.           $curl = curl_init();  
  5.         curl_setopt($curl, CURLOPT_URL, $requestURL);
  6.           curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  
  7.           curl_setopt($curl, CURLOPT_FAILONERROR, true);  
  8.  
  9.           curl_setopt($curl, CURLOPT_HTTPHEADER, array(  
  10.               "Authorization: AuthSub token=\"$token\"")  
  11.           );   
  12.  
  13.           $result = curl_exec($curl);
  14.           curl_close($curl);
  15.  
  16.           return $result;  
  17.     }
  18.  
  19.     function upgradeToken($token) {
  20.         $response = $this->sendGetMethod("https://www.google.com/accounts/AuthSubSessionToken", $token);
  21.  
  22.         return $response;
  23.     }
Also, to post a comment:
Expand|Select|Wrap|Line Numbers
  1. function postComment($comment, $id, $token) {
  2.         $comment = "<?xml version='1.0' encoding='UTF-8'?><entry xmlns='http://www.w3.org/2005/Atom' xmlns:yt='http://gdata.youtube.com/schemas/2007'><content>$comment</content></entry>";
  3.         $contentLength = strlen($comment);
  4.  
  5.         $curl = curl_init();  
  6.         curl_setopt($curl, CURLOPT_URL, "http://gdata.youtube.com/feeds/api/videos/$id/comments");
  7.           curl_setopt($curl, CURLOPT_POST, true);  
  8.           curl_setopt($curl, CURLOPT_FAILONERROR, true);
  9.         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);        
  10.  
  11.           curl_setopt($curl, CURLOPT_HTTPHEADER, array(  
  12.               "Authorization: AuthSub token=\"$token\""
  13.             ,"Content-Type: application/atom+xml"
  14.             ,"Content-Length: $contentLength"
  15.             ,"GData-Version: 2"
  16.             ,"X-GData-Key: key={my_personal_dev_key}" )
  17.           );           
  18.  
  19.         curl_setopt($curl, CURLOPT_POSTFIELDS, "$comment");        
  20.  
  21.           $result = curl_exec($curl);
  22.         $info = curl_getinfo($curl);
  23.  
  24.         if(curl_errno($curl)) {
  25.             $error_code = curl_errno($curl);
  26.             $http_code = (string)$info['http_code'];
  27.             ...}
  28.  
  29.         curl_close($curl);
  30.  
  31.           return $result;
  32. }

Share this Question
Share on Google+
1 Reply


P: 42
I just wanted to share the solution to my problem. I still couldn't get AJAX to work for this GET method, so I used cURL in PHP:

To upgrade the token to a session token:
Expand|Select|Wrap|Line Numbers
  1.     function sendGetMethod($requestURL, $token) {   
  2.         //Secure example = http://gdatatips.blogspot.com/2008/07/secure-authsub-in-php.html
  3.  
  4.           $curl = curl_init();  
  5.         curl_setopt($curl, CURLOPT_URL, $requestURL);
  6.           curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  
  7.           curl_setopt($curl, CURLOPT_FAILONERROR, true);  
  8.  
  9.           curl_setopt($curl, CURLOPT_HTTPHEADER, array(  
  10.               "Authorization: AuthSub token=\"$token\"")  
  11.           );   
  12.  
  13.           $result = curl_exec($curl);
  14.           curl_close($curl);
  15.  
  16.           return $result;  
  17.     }
  18.  
  19.     function upgradeToken($token) {
  20.         $response = $this->sendGetMethod("https://www.google.com/accounts/AuthSubSessionToken", $token);
  21.  
  22.         return $response;
  23.     }
Also, to post a comment:
Expand|Select|Wrap|Line Numbers
  1. function postComment($comment, $id, $token) {
  2.         $comment = "<?xml version='1.0' encoding='UTF-8'?><entry xmlns='http://www.w3.org/2005/Atom' xmlns:yt='http://gdata.youtube.com/schemas/2007'><content>$comment</content></entry>";
  3.         $contentLength = strlen($comment);
  4.  
  5.         $curl = curl_init();  
  6.         curl_setopt($curl, CURLOPT_URL, "http://gdata.youtube.com/feeds/api/videos/$id/comments");
  7.           curl_setopt($curl, CURLOPT_POST, true);  
  8.           curl_setopt($curl, CURLOPT_FAILONERROR, true);
  9.         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);        
  10.  
  11.           curl_setopt($curl, CURLOPT_HTTPHEADER, array(  
  12.               "Authorization: AuthSub token=\"$token\""
  13.             ,"Content-Type: application/atom+xml"
  14.             ,"Content-Length: $contentLength"
  15.             ,"GData-Version: 2"
  16.             ,"X-GData-Key: key={my_personal_dev_key}" )
  17.           );           
  18.  
  19.         curl_setopt($curl, CURLOPT_POSTFIELDS, "$comment");        
  20.  
  21.           $result = curl_exec($curl);
  22.         $info = curl_getinfo($curl);
  23.  
  24.         if(curl_errno($curl)) {
  25.             $error_code = curl_errno($curl);
  26.             $http_code = (string)$info['http_code'];
  27.             ...}
  28.  
  29.         curl_close($curl);
  30.  
  31.           return $result;
  32. }
Dec 23 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.