Monday, April 04, 2011

Google Spreadsheets を Androidから扱う (2)

OAuthの続きです。

Implict intent

androidには,intentでactivity, service, broadcastに応答させる昨日があります。この機能を利用して,Browser上でリンクをクリックして,インストールされているactivityを起動する方法があります。
とくに,
Consider, for example, what the browser application does when the user follows a link on a web page. It first tries to display the data (as it could if the link was to an HTML page). If it can't display the data, it puts together an implicit intent with the scheme and data type and tries to start an activity that can do the job. If there are no takers, it asks the download manager to download the data. That puts it under the control of a content provider, so a potentially larger pool of activities (those with filters that just name a data type) can respond.
とありますように,ブラウザでWebページ上のリンクをクリックして, データが表示されない場合,該当する暗黙のintentを発行するようになっています。 OAuth認証では,この機能を利用して,callback urlに自分のアプリを起動できるようなurlを指定します。
Google I/O 2010 Buzz Android Sample for Java Client Library for Google API's version 2.2.0-alphaの実装を参考にして,話を進めていきます。

Request Token (Temporary credentials token) の取得

前回,GoogleOAuthAuthorizeTemporaryTokenUrlを作成いたしましたが,その補足です。
Request Tokenを取得するために,GoogleOAuthGetTemporaryTokenを利用します。
    GoogleOAuthGetTemporaryToken temporaryToken = new GoogleOAuthGetTemporaryToken();
    OAuthHmacsinger signer = new OAuthHmacSigner();
    signer.clientSharedSecret = "anonymous";
    temporaryToken.signer = signer;
    temporaryToken.displayName = appname;
    temporaryToken.consumerKey = "anonymous";
    temporaryToken.scope = "https://spreadsheets.google.com/feeds/";
    temporaryToken.transport = transport;
    temporaryToken.callback = "x-myspreadsheet://com.example.abekatsu/";
前回との違いは,callbackの指定です。custom schemeを使うなという意見がありますが,custom schemeを用いたアプリの起動が広まっている現状を踏まえ,今回は利用します。
競合を避けるために,host部分をjavaのpackage名のように,自分自身のFQDNをTop levelから書いてみました。

GoogleOAuthAuthorizeTemporaryTokenUrlの作成

    OAuthCredentialsResponse response = temporaryToken.execute();
    GoogleOAuthAuthorizeTemporaryTokenUrl authorizeUrl = new GoogleOAuthAuthorizeTemporaryTokenUrl();
    authorizeUrl.template = "mobile";
    authorizeUrl.set("scope", scope);
    authorizeUrl.set("domain", "anonymous");
    authorizeUrl.set("xoauth_displayname", appname);
    authorizeUrl.temporaryToken = response.token;
この authorizeUrl で得られるURLを引数にしてブラウザを起動します。
     Intent intent = new Intent(Intent.ACTION_VIEW);
     intent.setData(Uri.parse(temporaryTokenUrl.build()));
     startActivity(intent);


実際に実行しますと,GoogleのAccountページが表示されます。ここで,「Grant access」をクリックすると,上記のcallbackで指定したURLを含む暗黙のIntentが発行されます。
















暗黙のIntentの取得

先程のステップで得られた,暗黙のIntentを取得してみましょう。
AndroidManifest.xml
まずは,AndroidManifest.xmlで,このIntentを補足するように指定します。
    
        
            
                
                
            
            
            
                
                
                
                
            
        
    
暗黙のIntentでは,カテゴリーが "android.intent.category.DEFAULT" となります。
Activityの編集
ここでは,単純に Intent に含まれているデータを表示させてみます。
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        boolean isViewAction = Intent.ACTION_VIEW.equals(getIntent().getAction());
        if (isViewAction) {
            Uri uri = this.getIntent().getData();
            if (uri != null) {
                Log.d(TAG, "uri: " + uri.toString());
            }
        }
    }
実際に,アプリケーションを実行して Grant access すると次のデータを含む 暗黙のIntentが発行されます。
D/OkodukaiNoteActivity(  235): uri: x-okozukai:///?oauth_verifier=ここにoauth_verifierの文字列&oauth_token=ここにoauth_tokenの文字列
次回はこのauth_tokenをGoogleにauthorizeします。

No comments: