كيف يمكنك الوصول إلى مصادقة Google App Engine الخدمة من (non-web) بيثون العميل ؟

StackOverflow https://stackoverflow.com/questions/101742

سؤال

لدي محرك جوجل التطبيق التطبيق http://mylovelyapp.appspot.com/ أنه يحتوي على صفحة mylovelypage

لحظة, الصفحة فقط لا self.response.out.write('OK')

إذا قمت بتشغيل التالية الثعبان في جهاز الكمبيوتر:

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

فإنه يطبع على "OK"

المشكلة إذا قمت بإضافة login:required إلى هذه الصفحة في التطبيق yaml

ثم هذا بطباعة HTML حسابات Google صفحة تسجيل الدخول

لقد حاولت "طبيعية" المصادقة النهج.على سبيل المثال

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 مرة أخرى.

لقد حاولت جوجل ClientLogin مصادقة 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 العميل المكتبة أن تفعل هذا النوع من الشيء ؟ من ما قرأت ، وأعتقد أنه يجب أن تكون قادرا على الوصول إلى التطبيق محرك التطبيقات ، ولكن لم يتم أي أكثر نجاحا في الحصول على مصادقة يعمل التطبيق محرك الاشياء هناك إما

أي مؤشرات على عينات من المواد ، أو حتى مجرد كلمات يجب أن يكون البحث عن أن أبدأ سيكون محل تقدير كبير جدا.

وذلك بفضل!

هل كانت مفيدة؟

المحلول

appcfg.py, أداة تحميل البيانات إلى التطبيق المحرك يجب أن تفعل هذا بالضبط لمصادقة نفسها مع التطبيق محرك الخادم.ذات الصلة وظيفة المستخرجة إلى appengine_rpc.py.باختصار ، فإن الحل هو:

  1. استخدام جوجل ClientLogin API للحصول على رمز والتوثيق.appengine_rpc.py هل هذا في _GetAuthToken
  2. وإرسال رمز المصادقة على رابط خاص على التطبيق الخاص بك محرك التطبيق.تلك الصفحة ثم يعود كوكي و توجيه 302.تجاهل توجيه و تخزين ملف تعريف الارتباط.appcfg.py هل هذا في _GetAuthCookie
  3. استخدم عاد كوكي في جميع الطلبات المستقبلية.

قد تحتاج أيضا إلى النظر في _Authenticate, أن نرى كيف appcfg يتعامل مع مختلف رموز الإرجاع من ClientLogin ، _GetOpener, أن نرى كيف appcfg يخلق urllib2 OpenerDirector التي لا تتبع HTTP الموجهات.أو هل يمكن أن مجرد استخدام AbstractRpcServer و HttpRpcServer دروس بالجملة, لأنها إلى حد كبير كل ما تحتاجه.

نصائح أخرى

شكرا عنكب للإجابة - عملت كما اقترح

هنا هو نسخة مبسطة من القانون ، في حالة أنها مفيدة إلى الشخص التالي في محاولة!

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

بالنسبة لأولئك الذين لا يستطيعون الحصول على ClientLogin العمل ، في محاولة التطبيق المحرك أوث الدعم.

Im لم تكن مألوفة جدا مع لمحرك تطبيقات أو جوجل واجهات برمجة التطبيقات ويب, ولكن نهج القوة الغاشمة يمكنك كتابة برنامج نصي مع شيء مثل مكننة (http://wwwsearch.sourceforge.net/mechanize/) ببساطة المشي من خلال عملية تسجيل الدخول قبل البدء في القيام بعمل حقيقي من العميل.

أنا لست الثعبان الخبراء أو التطبيق محرك الخبراء.ولكن هل حاولت التالية العينة appl في http://code.google.com/appengine/docs/gettingstarted/usingusers.html.أنا خلقت واحدة في http://quizengine.appspot.com, يبدو أن تعمل بشكل جيد مع مصادقة جوجل و كل شيء.مجرد اقتراح, ولكن انظر إلى دليل بدء التشغيل.أعتبر أن من السهل إذا كان اقتراح يبدو ساذجا.:) شكرا

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top