メモ

いくつか調査中のソレについて控え。

android.content.SharedPreferences

  • GAE とのやりとりにおいて、ユーザ id やらパスワードの保持に使える模様
  • google アカウントな情報とかってどうやって持ってるんだろ

GAE

  • MacBook な職場端末には GoogleAppEngineLauncher なるものがある模様
  • Preference な画面にて python な PATH を入れた
  • 試しにプロジェクト作成してみた

Android から GAE へのログイン

  • 材料以下
  • preference に Email および Passwd を保持しといて最初にログインするとか?
    • なんかテキトーな Activity を表示しといて裏 (な thread) でログイン処理
      • 失敗したときどうするか、は一旦スルー?
    • とりあえずチュートリアルの Using the Users Service なソレを流用
      • 成功したら適当な json オブジェクト戻すか (を
      • 以下なカンジをでっち上げてみた
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from django.utils import simplejson

class MainHandler(webapp.RequestHandler):

  def get(self):
    user = users.get_current_user()
    if user:
      d = dict([("result", "Login Success")])
    else:
      d = dict([("result", "Login Unsuccessful")])

    self.response.out.write(simplejson.dumps(d, enable_ascii=False))

def main():
  application = webapp.WSGIApplication([('/', MainHandler)],
                                       debug=True)
  util.run_wsgi_app(application)


if __name__ == '__main__':
  main()

で GoogleAppEngineLauncher で起動してみると起動に失敗している。

Unable to assign value 'testGAE' to attribute 'application':
Value 'testGAE' does not match expression '^(?!-)[a-z\d\-]{1,100}$'

どうやら app.yaml の以下なナニがダウトらしい

application: testGAE

大文字は許容されていない模様。GAE を削除したら正常動作しましたが、ログインしてても users.get_current_user は None を戻している (んだっけ?
これはおそらく_ココ_に記述されているログイン処理を経て auth な token と共に get なリクエストを投げないと駄目と見た。
ちなみにブラウザ上では

{"result":"Login Unsuccessful"}

という表示になってます。

Android 側の実装

では、って事でこちらの実装試験をば。とりあえず新規プロジェクト追加して permission をナニ。

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

で、って思ってサンプル見てたんですが、GAE なナニを deploy した方が良いのかどうか。で、試しに GAELauncher から deply してみたら失敗している様子。

と思ったら

appengine 側でアプリを作成してなかった件orz
作成して deploy したらイケました。帰宅後に Android 側の実装を云々する方向で。

帰宅後

以下を盛り込み。

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        DefaultHttpClient httpClient = new DefaultHttpClient();

        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("Email", "xxx@gmail.com"));
        nvps.add(new BasicNameValuePair("Passwd", "fugahoge"));
        nvps.add(new BasicNameValuePair("service", "ah"));
        nvps.add(new BasicNameValuePair("source", "test-yamanetoshi"));
        nvps.add(new BasicNameValuePair("accountType", "HOSTED_OR_GOOGLE"));

        //Login at Google.com
        HttpPost httpost = new HttpPost("https://www.google.com/accounts/ClientLogin");
        HttpResponse response = null;
        try {
        	httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        	response = httpClient.execute(httpost);
            Log.i(TAG, "Login Response: " + response.getStatusLine());
        }
        catch (IOException e) {
        	Log.e(TAG, "error (1)");
        }
        finally {
        }
        
        String authKey = null;
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String line = null;
            while ((line = br.readLine()) != null) {
                String[] s = line.split("=");
                if (s.length == 2 && s[0].equalsIgnoreCase("auth")) {
                    authKey = s[1];
                    break;
                }
            }
        }
        catch (IOException e){
        	Log.e(TAG, "error (2)");
        }
        finally {
            if (br != null) try {br.close();} catch (IOException e) {
                Log.e(TAG, "Cannot close stream.", e);
            }
        }
        Log.i(TAG, "AUTH = " + authKey);
        
        HttpGet httpget = new HttpGet("http://test-yamanetoshi.appspot.com/_ah/login?auth=" + authKey);
        try {
        	response = httpClient.execute(httpget);
        	InputStream is = response.getEntity().getContent();
        	BufferedReader isr = null;
        	try {
        		isr = new BufferedReader(new InputStreamReader(is));
        		String line = null;
        		while ((line = isr.readLine()) != null) {
        			Log.i(TAG, "RESPONSE: " + line);
        		}
        	}
        	finally {
        		if (isr != null) isr.close();
        	}
        }
        catch (IOException e) {
        	Log.e(TAG, "error (3)");
        }

        Log.i(TAG, "Appspot.com Login Response: " + response.getStatusLine());
        
        setContentView(R.layout.main);
    }

実行してみたら途中でオチます。ログとか全然出ないので
# おそらく MacBook 起動しっぱなしが原因
デバッガで確認してみたら以下なあたりでコケてる模様。

        HttpGet httpget = new HttpGet("http://test-yamanetoshi.appspot.com/_ah/login?auth=" + authKey);
        try {
        	response = httpClient.execute(httpget);
        	InputStream is = response.getEntity().getContent();

あら? よく考えたらここは json をナニしないといかん所だな。成程。

今日

違う意味で色んな所からパンチ入りまくってへろへろなので今日はこれで止めておきます。