52

私は Google App Engine アプリを持っています - http://mylovelyapp.appspot.com/ それはページを持っています - mylovelypage

現時点では、ページはself.response.out.write('OK')

コンピューターで次の Python を実行すると、次のようになります。

import urllib2
f = urllib2.urlopen("http://mylovelyapp.appspot.com/mylovelypage")
s = f.read()
print s
f.close()

「OK」と出力されます

問題はlogin:required、アプリの yaml でこのページに追加した場合です

これにより、Google アカウントのログイン ページの HTML が出力されます。

「通常の」認証アプローチを試しました。例えば

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()

auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(None,
                          uri='http://mylovelyapp.appspot.com/mylovelypage',
                          user='billy.bob@gmail.com',
                          passwd='billybobspasswd')
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)

しかし、違いはありません。ログイン ページの HTML が返されます。

Google の ClientLogin auth APIを試しましたが、うまくいきません。

h = httplib2.Http()

auth_uri = 'https://www.google.com/accounts/ClientLogin'
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
myrequest = "Email=%s&Passwd=%s&service=ah&source=DALELANE-0.0" % ("billy.bob@gmail.com", "billybobspassword")
response, content = h.request(auth_uri, 'POST', body=myrequest, headers=headers)

if response['status'] == '200':
    authtok = re.search('Auth=(\S*)', content).group(1)

    headers = {}
    headers['Authorization'] = 'GoogleLogin auth=%s' % authtok.strip()
    headers['Content-Length'] = '0'

    response, content = h.request("http://mylovelyapp.appspot.com/mylovelypage", 
                                  'POST', 
                                  body="", 
                                  headers=headers)

    while response['status'] == "302":        
        response, content = h.request(response['location'], 'POST', body="", headers=headers) 

    print content

トークンを正しく取得できるようですが、「mylovelypage」を呼び出すときにヘッダーでトークンを使用しようとしても、ログイン ページの HTML が返されます。:-(

誰でも助けてもらえますか?

GData クライアント ライブラリを使用して、この種のことを行うことはできますか? 私が読んだことから、App Engineアプリにアクセスできるはずだと思いますが、App Engineのものに対して認証を機能させることにこれ以上成功していません

サンプル、記事、または検索を開始するために検索する必要があるキーワードへのポインターは、非常に高く評価されます。

ありがとう!

4

5 に答える 5

39

App Engine にデータをアップロードするツールである appcfg.py は、App Engine サーバーで自身を認証するために、まさにこれを行う必要があります。関連する機能は appengine_rpc.py に抽象化されています。一言で言えば、解決策は次のとおりです。

  1. Google ClientLogin APIを使用して認証トークンを取得します。appengine_rpc.py は_GetAuthTokenでこれを行います
  2. 認証トークンを App Engine アプリの特別な URL に送信します。そのページは、Cookie と 302 リダイレクトを返します。リダイレクトを無視して Cookie を保存します。appcfg.py は _GetAuthCookie でこれを行います
  3. 返された Cookie を今後のすべてのリクエストで使用します。

appcfg が ClientLogin からのさまざまなリターン コードをどのように処理するかを確認するには _Authenticate を、HTTP リダイレクトに従わない urllib2 OpenerDirector を appcfg がどのように作成するかを確認するには _GetOpener を確認することもできます。または、実際には、AbstractRpcServer および HttpRpcServer クラスを大規模に使用することもできます。これらのクラスは、必要なほとんどすべてのことを実行するからです。

于 2008-09-19T14:55:24.780 に答える
34

答えてくれたArachnidに感謝-提案どおりに機能しました

次の人が試してみるのに役立つ場合に備えて、コードの簡略化されたコピーを次に示します。

import os
import urllib
import urllib2
import cookielib

users_email_address = "billy.bob@gmail.com"
users_password      = "billybobspassword"

target_authenticated_google_app_engine_uri = 'http://mylovelyapp.appspot.com/mylovelypage'
my_app_name = "yay-1.0"



# we use a cookie to authenticate with Google App Engine
#  by registering a cookie handler here, this will automatically store the 
#  cookie returned when we use urllib2 to open http://currentcost.appspot.com/_ah/login
cookiejar = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
urllib2.install_opener(opener)

#
# get an AuthToken from Google accounts
#
auth_uri = 'https://www.google.com/accounts/ClientLogin'
authreq_data = urllib.urlencode({ "Email":   users_email_address,
                                  "Passwd":  users_password,
                                  "service": "ah",
                                  "source":  my_app_name,
                                  "accountType": "HOSTED_OR_GOOGLE" })
auth_req = urllib2.Request(auth_uri, data=authreq_data)
auth_resp = urllib2.urlopen(auth_req)
auth_resp_body = auth_resp.read()
# auth response includes several fields - we're interested in 
#  the bit after Auth= 
auth_resp_dict = dict(x.split("=")
                      for x in auth_resp_body.split("\n") if x)
authtoken = auth_resp_dict["Auth"]

#
# get a cookie
# 
#  the call to request a cookie will also automatically redirect us to the page
#   that we want to go to
#  the cookie jar will automatically provide the cookie when we reach the 
#   redirected location

# this is where I actually want to go to
serv_uri = target_authenticated_google_app_engine_uri

serv_args = {}
serv_args['continue'] = serv_uri
serv_args['auth']     = authtoken

full_serv_uri = "http://mylovelyapp.appspot.com/_ah/login?%s" % (urllib.urlencode(serv_args))

serv_req = urllib2.Request(full_serv_uri)
serv_resp = urllib2.urlopen(serv_req)
serv_resp_body = serv_resp.read()

# serv_resp_body should contain the contents of the 
#  target_authenticated_google_app_engine_uri page - as we will have been 
#  redirected to that page automatically 
#
# to prove this, I'm just gonna print it out
print serv_resp_body
于 2008-09-19T16:22:06.287 に答える
1

ClientLogin が機能しない場合は、アプリ エンジンのOAuth サポートを試してください。

于 2011-01-27T06:59:18.943 に答える
0

私は AppEngine や Google の Web API にはあまり詳しくありませんが、ブルート フォース アプローチの場合は、機械化 ( http://wwwsearch.sourceforge.net/mechanize/ ) のようなスクリプトを記述して、ログイン プロセスを単純に実行することができます。クライアントの実際の作業を開始します。

于 2008-09-19T14:16:24.217 に答える
-1

私は Python の専門家でも、アプリ エンジンの専門家でもありません。しかし、http: //code.google.com/appengine/docs/gettingstarted/usingusers.html のサンプル アプリケーションに従ってみましたか。http://quizengine.appspot.comで作成しましたが、Google 認証とすべてで正常に動作するようでした。単なる提案ですが、入門ガイドを参照してください。提案が素朴に聞こえる場合は、気楽に考えてください。:) ありがとう。

于 2008-09-19T14:16:12.377 に答える