Google app-engine (http://spititan.appspot.com) でホストされているアプリケーションがあります。また、ClientLogin を介してそのアプリケーションにアクセスするためのコマンド ライン ツールもあります。コード スニペットは次のとおりです。
138 # get an AuthToken from Google accounts
139 auth_uri = 'https://www.google.com/accounts/ClientLogin'
140 authreq_data = urllib.parse.urlencode({ "Email": email_address,
141 "Passwd": password,
142 "service": "ah",
143 "source": appname,
144 "accountType": "HOSTED_OR_GOOGLE" })
145 request = urllib.request.Request(auth_uri, data=authreq_data)
146 response = opener.open(request)
147 response_body = str(response.read(), 'utf-8')
148 response_dict = dict(x.split("=") for x in response_body.split("\n") if x)
149 return response_dict["Auth"]
...
...
112 # Send the auth token to the AppEngine to login
113 continue_location = "http://localhost/"
114 args = {"continue": continue_location, "auth": auth_token}
115 host = "spititan.appspot.com" % appname
116 url = "https://spititan/_ah/login?%s" % urllib.parse.urlencode(args)
...
...
このツールは、数日ごとにパスワードを提供する必要があるという小さな煩わしさで、かなり長い間うまく機能します。OAuth2 が現在推奨されている認証方法であることに気付いたので、ドキュメント (http://code.google.com/p/google-api-python-client /wiki/OAuth2):
59 storage = oauth2client.file.Storage(
60 os.path.join(FLAGS.data_dir, 'confidential.dat'))
61
62 credentials = storage.get()
63
64 if credentials is None or credentials.invalid == True:
65 flow = oauth2client.client.OAuth2WebServerFlow(
66 client_id='<xxxxx>',
67 client_secret='<xxxxx>',
68 scope='<xxxxx>',
69 user_agent='<xxxx>')
70
71 credentials = oauth2client.tools.run(flow, storage)
72
73 http = httplib2.Http(cache=".cache")
74 http = credentials.authorize(http)
私の理解では、アプリケーションを登録すると「client_id」と「client_secret」が取得されます。user_agent は自由形式の文字列です。問題は、「スコープ」に何を入れればよいかということです。http://spititan.appspot.com/spititanを試しましたが、うまくいきませんでした。
ありがとう