473,386 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Google AuthSub Authentication + jQuery + Ajax

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. }

1 2731
Marknut
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

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

Similar topics

9
by: Dominic Myers | last post by:
At the risk of inflaming passions best left alone I'd like to ask a favour of the members of this esteemed news-group: I'm currently engaged on a dissertation which is investigating AJAX...
4
by: VK | last post by:
Google Trends is an all new service (started May 10) and I have not responsability for proper query or data accuracy. Overall seems pretty close to what could be observed by the post history in...
5
by: ziobudda | last post by:
Hi, I want ask you if, for a web portal/application, is better prototype or Jquery? I don't want to innesc some type of flame, but after the announce that drupal use JQuery and that the new...
83
by: liketofindoutwhy | last post by:
I am learning more and more Prototype and Script.aculo.us and got the Bungee book... and wonder if I should get some books on jQuery (jQuery in Action, and Learning jQuery) and start learning about...
9
by: Jon Paal [MSMD] | last post by:
using json like ( {"Records": , "RecordCount":"1" } ) and jquery like: $.ajax({ .... success: function(json, status) {
11
by: kj | last post by:
I would like to convert a form that currently uses the traditional CGI sequence (i.e. the action associated with the form sends a POST request to a server-side CGI script) to one that uses AJAX to...
10
dmjpro
by: dmjpro | last post by:
function synchronous_ajax(){ var ajax = null; if(typeof ActiveXObject!='undefined') ajax = new ActiveXObject("Microsoft.XMLHTTP"); else if(typeof XMLHttpRequest!='undefined') ajax = new...
20
by: Aaron Gray | last post by:
There does not seem too be anyway to test if two jQuery references are the same element. Given :- ... <div id="1"></div .... Then :- alert( $("#1") == $("#1"))
6
by: ghjk | last post by:
I have a php page which is having "sumbit" and "cancel" buttion in default. but when user click submit button I want to hide"submit" button and display "ok" button. How can I do that?
3
by: neovantage | last post by:
Hi Experts, I have create a jQuery ui function which delete product from the shopping list. After receiving a jQuery Ajax response i want to bind the same function which load on page load. How can i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.