0

我試圖在Google App Engine上使用Google幻燈片API,儘管使用了Google代碼示例(專門用於App Engine上的幻燈片API的OAuth2 &),但我遇到問題。HTTPError 401與App Engine上的Google幻燈片API和OAuth2

這是我的App Engine代碼,刪除了不必要的代碼(一切都在main.app中)。我正在做的是試圖從HTML表單發佈一個字符串,然後構建一個空白的演示文稿。我已經使用幻燈片API和我原型的一個簡單腳本;我現在正在嘗試通過App Engine應用程序進行自助式服務,但這是身份驗證的變化讓我沮喪。

from googleapiclient import discovery 
from oauth2client import client 
from oauth2client.contrib import appengine 
from google.appengine.api import memcache 

CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), 'client_secrets.json') 
MISSING_CLIENT_SECRETS_MESSAGE = """[omitted]""" % CLIENT_SECRETS  

http = httplib2.Http() 
service = discovery.build('slides', 'v1', http=http) 
decorator = appengine.OAuth2DecoratorFromClientSecrets(
    CLIENT_SECRETS, 
    scope='https://www.googleapis.com/auth/presentations https://www.googleapis.com/auth/drive', 
    message=MISSING_CLIENT_SECRETS_MESSAGE) 

class SlideBuilder(webapp2.RequestHandler): 

    @decorator.oauth_required 
    def post(self): 
    programslug = self.request.get('programid') 
    presoname = str(programslug) + ' Mentors' 

    presentationbody = { 
     'title': presoname 
    } 
    presentation = service.presentations().create(body=presentationbody).execute() 

我想指出的是,我直接下載最新的client_secrets.json從API控制檯,這樣應該正確匹配的CLIENT_SECRETS。

我得到的錯誤(dev的服務器,但它也是在實時應用程序)是這樣的:

Traceback (most recent call last): 
    File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__ 
    rv = self.handle_exception(request, response, e) 
    File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__ 
    rv = self.router.dispatch(request, response) 
    File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher 
    return route.handler_adapter(request, response) 
    File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__ 
    return handler.dispatch() 
    File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch 
    return self.handle_exception(e, self.app.debug) 
    File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch 
    return method(*args, **kwargs) 
    File "/Users/jedc/pm-tools/oauth2client/contrib/appengine.py", line 644, in check_oauth 
    resp = method(request_handler, *args, **kwargs) 
    File "/Users/jedc/pm-tools/main.py", line 113, in post 
    presentation = service.presentations().create(body=presentationbody).execute() 
    File "/Users/jedc/pm-tools/oauth2client/_helpers.py", line 133, in positional_wrapper 
    return wrapped(*args, **kwargs) 
    File "/Users/jedc/pm-tools/googleapiclient/http.py", line 840, in execute 
    raise HttpError(resp, content, uri=self.uri) 
HttpError: <HttpError 401 when requesting https://slides.googleapis.com/v1/presentations?alt=json returned "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project."> 

感覺就像有東西微妙的,但愚蠢的,我在這裏做。我會很感激任何幫助或指示,找出那是什麼!

回答

0

發生此錯誤是因爲您的http未授權憑證。 要授權您使用裝飾者的證書的http。

decorator = appengine.OAuth2DecoratorFromClientSecrets(
    CLIENT_SECRETS, 
    scope='https://www.googleapis.com/auth/presentations https://www.googleapis.com/auth/drive', 
    message=MISSING_CLIENT_SECRETS_MESSAGE) 
http = decorator.http() 
service = discovery.build('slides', 'v1', http=http) 

這將解決您的問題。 如需進一步參考read this app engine decorators documentation from Google