Google AuthSub a Picasa Web Data API crossdomain.xml

19. June 2009

“Je viac ako rok potom, kedy do gdata-issues pribudla žiadosť o pridanie crossdomain.xml pre Google Accounts (http://code.google.com/p/gdata-issues/issues/detail?id=406).”

Od apríla tohto roku som mal možnosť ako člen http://groups.google.com/group/authsub-trusted-testers skupiny testovať a ladiť AuthSub autentifikáciu cez Flash Player. Mesiac sme komunikovali, testovali a reportovali problémy. Výsledkom je crossdomain.xml pre Google Accounts http://accounts.googleapis.com/crossdomain.xml

Juch. Zhrniem výsledky do krátkej ukážky Google Accounts AuthSub autentifikácie:

1. Načítať crossdomain.xml

Explicitne musíme povoliť odosielanie requestov na Google Accounts API

Security.loadPolicyFile("https://accounts.googleapis.com/crossdomain.xml"); 

2. Prejsť na stránku “Žiadosť o prístup”

Nasleduje autentifikácia cez prehliadač na strane Google

// create a request to get the token
var variables URLVariables = new URLVariables();
    
variables["scope""http://photos.googleapis.com/data";
    
variables["session""1";
    
variables["secure""0";
    
variables["hd""default";
    
variables["next""http://www.prasa.sk/authsub/index.html";
    
var 
requestURLRequest = new URLRequest("https://www.google.com/accounts/AuthSubRequest");
    
request.data =  variables;
 
navigateToURL(request"_top"); 

3. Google vráti prehliadač späť …

… na našu aplikáciu spolu aj s tokenom. Token sa dá z adresy načítať napríklad cez ExternalInterface.call()

http://www.prasa.sk/authsub/index.html?token=CK-...-ezGBg 

4. Načítať long-lived session token

Nasledovným volaním vymeníme single-use token za long-lived session token, ktorý možeme použiť na authorizovaný request služieb Google (napr. Picasa Web Data API):

private function getAuthSubSessionToken() : void
{
    
// create request to get the session token
    
var request URLRequest = new URLRequest();
        
request.url "https://accounts.googleapis.com/accounts/AuthSubSessionToken";
        
request.method URLRequestMethod.POST;            // we need to use POST method because the headers are NOT sent
        
request.data = new URLVariables("test=variable");    // some test varialbe to get the headers working
        // create request headers
        
request.requestHeaders.push(new URLRequestHeader("Authorization","AuthSub token=\""+token+"\""));
        
request.requestHeaders.push(new URLRequestHeader("Content-type","application/x-www-form-urlencoded"));
        
    var 
loader URLLoader = new URLLoader();
        
loader.addEventListener(Event.COMPLETEloader_result);
        
    try
    
{
        loader
.load(request);
    

    
catch(Error)
    
{
        
throw new Error(e);
    
}
}

public function loader_result(evt Event) : void
{
    
var loader URLLoader URLLoader(evt.target);
    
sessionToken = new URLVariables(loader.data).Token;    // store our session token in a instance variable

Novinkou v crossdomain.xml aktualizáciách od Google je aj Picasa Web Data API. Do crossdomain.xml súboru pribudla Authorization hlavička a keďže vlastné hlavičky sa nedajú z Flash Player-a poslať ináč ako POST requestom, pribudla aj X-HTTP-Method-Override hlavička pre simuláciu napr. GET requestov. S takýmto crossdomain.xml súborom a long-lived session tokenom už konečne vieme načítať napríklad zoznam privátnych Picasa Web albumov.

1. Explicitne načítať crossdomain.xml

Explicitne musíme povoliť odosielanie requestov na Picasa Web Data API

Security.loadPolicyFile("http://photos.googleapis.com/data/crossdomain.xml"); 

2. Odoslať žiadosť …

… spolu s s autorizačným tokenom v hlavičke

private function getPrivateAlbums() : void
{
    
// create request to get the session token
    
var request URLRequest = new URLRequest();
        
request.url "http://photos.googleapis.com/data/feed/api/user/default?access=private";
        
request.method URLRequestMethod.POST;            // we need to use POST method because the headers are NOT sent
        
request.data = new URLVariables("test=variable");    // some test varialbe to get the headers working
        // create request headers
        
request.requestHeaders.push(new URLRequestHeader("Authorization","AuthSub token=\"" + sessionToken + "\""));
        
request.requestHeaders.push(new URLRequestHeader("X-HTTP-Method-Override""GET"));
        
request.requestHeaders.push(new URLRequestHeader("Content-type","application/atom+xml"));
        
    var 
loader URLLoader = new URLLoader();
        
loader.addEventListener(Event.COMPLETEprivateLoader_result);
        
    try
    
{
        loader
.load(request);
    

    
catch(Error)
    
{
        
throw new Error(e);
    
}
}

public function privateLoader_result(evt Event) : void
{
    
var atom_ns Namespace = new Namespace("http://www.w3.org/2005/Atom");
    var 
loader URLLoader URLLoader(evt.target); 
    var 
data XML = new XML(loader.data);
    
    for 
each(var album XML in data.atom_ns::entry)
    
{
        trace
(album.atom_ns::title.text(), album.atom_ns::summary.text());
    
}        

Funkčnú aplikáciu hladajte na http://www.prasa.sk/authsub (view-source enabled).

Po dlhom čase sme sa nakoniec dočkali. Som zvedavý na nové aplikácie a možnosti.
Má niekto chuť napísať gdata client library v as3?

Komentujte
Políčka označené * sú povinné.