0

我正在尝试在 Google App Engine 上使用 Google Slides API,尽管使用了 Google 代码示例(特别是用于 OAuth2 和 App Engine 上的 Slides API),但我遇到了问题。

这是我的 App Engine 代码,删除了不必要的内容(所有内容都在 main.app 中)。我正在做的是尝试从 HTML 表单中发布一个字符串,然后构建一个空白演示文稿。我已经使用了带有我原型的简单脚本的 Slides 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()

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

我得到的错误(在开发服务器上;但它也在实时应用程序上)是这样的:

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

感觉就像我在这里做的一些微妙但愚蠢的事情。我将不胜感激任何帮助或指示以弄清楚那是什么!

4

1 回答 1

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)

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

于 2017-03-10T04:45:12.530 回答