HTTPError 401 with Google Slides API & OAuth2 on App Engine



我正在尝试在Google App Engine上使用Google Slides API,尽管使用了Google代码示例(特别是针对OAuth2& the App Engine上的幻灯片API),但我还是遇到问题。

这是我的应用发动机代码,并删除了不必要的Cruft(所有内容都在Main.App中)。我正在做的是尝试从HTML表单中发布字符串,然后构建空白演示文稿。我已经使用了我原型的简单脚本使用了幻灯片API。我现在正在尝试通过应用引擎应用程序进行自我服务,但是正是身份验证的变化使我绊倒了。

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()

我想指出,我直接从API控制台下载了最新的client_secrets.json,因此应该正确匹配client_secrets。

我遇到的错误(在Dev Server上;但也在实时应用程序上)是:

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.">

感觉就像我在这里做的是微妙但愚蠢的事情。我感谢任何帮助或指示,以找出那是什么!

发生此错误,因为您的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)

这将解决您的问题。要进一步参考,请阅读此应用引擎装饰器的文档

最新更新