Google AuthSub Authentication + jQuery + Ajax

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Marknut
    New Member
    • Apr 2010
    • 42

    Google AuthSub Authentication + jQuery + Ajax

    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.MyTestSi te.com%2F&scope =https%3A%2F%2F gdata.youtube.c om&session=1&se cure=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:

    Code:
    $.ajax({
    	   	url: "https://www.google.com/accounts/AuthSubSessionToken"
    		,headers: { 	
    			"Host": "www.google.com"
    			,"Content-Type": "application/x-www-form-urlencoded"
    			,"Authorization": authSub
    			,"User-Agent": "Java/1.5.0_06"
    			,"Accept": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
    			,"Connection": "keep-alive"}
    		,success: 
    			function(data) {
    				alert('Data Loaded: ' + data);}
    		,type: 'GET'
    		,dataType: "jsonp"
    		,crossDomain: true
    		});
    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?
  • Marknut
    New Member
    • Apr 2010
    • 42

    #2
    Solution - PHP cURL

    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:
    Code:
    	function sendGetMethod($requestURL, $token) {   
    		//Secure example = http://gdatatips.blogspot.com/2008/07/secure-authsub-in-php.html
    	  
    	  	$curl = curl_init();  
    		curl_setopt($curl, CURLOPT_URL, $requestURL);
    	  	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  
    	  	curl_setopt($curl, CURLOPT_FAILONERROR, true);  
    
    	  	curl_setopt($curl, CURLOPT_HTTPHEADER, array(  
    		  	"Authorization: AuthSub token=\"$token\"")  
    	  	);   
    		
    	  	$result = curl_exec($curl);
    	  	curl_close($curl);
    	  
    	  	return $result;  
    	}
    
    	function upgradeToken($token) {
    		$response = $this->sendGetMethod("https://www.google.com/accounts/AuthSubSessionToken", $token);
    		
    		return $response;
    	}
    Also, to post a comment:
    Code:
    function postComment($comment, $id, $token) {
    		$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>";
    		$contentLength = strlen($comment);
    							
    		$curl = curl_init();  
    		curl_setopt($curl, CURLOPT_URL, "http://gdata.youtube.com/feeds/api/videos/$id/comments");
    	  	curl_setopt($curl, CURLOPT_POST, true);  
    	  	curl_setopt($curl, CURLOPT_FAILONERROR, true);
    		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);		
    
    	  	curl_setopt($curl, CURLOPT_HTTPHEADER, array(  
    		  	"Authorization: AuthSub token=\"$token\""
    			,"Content-Type: application/atom+xml"
    			,"Content-Length: $contentLength"
    			,"GData-Version: 2"
    			,"X-GData-Key: key={my_personal_dev_key}" )
    	  	);   		
    		
    		curl_setopt($curl, CURLOPT_POSTFIELDS, "$comment");		
    		
    	  	$result = curl_exec($curl);
    		$info = curl_getinfo($curl);
    		
    		if(curl_errno($curl)) {
    			$error_code = curl_errno($curl);
    			$http_code = (string)$info['http_code'];
    			...}
    		
    		curl_close($curl);
    
    	  	return $result;
    }
    Last edited by Marknut; Dec 23 '11, 05:15 PM. Reason: Posted the wrong code, but will leave that code and add the valid code

    Comment

    Working...