0

我在我的 Google App Engine 应用程序中使用了 Python OAuth2 装饰器。我从 API 控制台将客户端机密下载为 json 文件。将应用程序部署到 appspot.com 后,本地版本 (localhost:8080) 无法正常工作。对 decorator.has_credentials() 的调用返回 false。apppot 版本运行良好。

更新:代码片段(简化)

CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), 'client_secrets.json')

oauth2_decorator = oauth2decorator_from_clientsecrets(
    CLIENT_SECRETS,
    scope='https://www.googleapis.com/auth/calendar',
    message=MISSING_CLIENT_SECRETS_MESSAGE)


class MyRequestHandler(webapp2.HandleRequest):
  @oauth2_decorator.oauth_aware
  def get(self):
    if oauth2_decorator.has_credentials():
      # do stuff...
    else:
      self.out.write("<html><body>Invalid credentials</body></html>")


app = webapp2.WSGIApplication([
    ('/', MyRequestHandler),
    (oauth2_decorator.callback_path, oauth2_decorator.callback_handler())
], debug=True)

更新 2:这是我的 client_secrets.json 的内容,没有明智的信息

{"web":  
  {
    "auth_uri":"https://accounts.google.com/o/oauth2/auth",
    "client_secret":"MY_CLIENT_SECRET",
    "token_uri":"https://accounts.google.com/o/oauth2/token",
    "client_email":"MY_CLIENT_ID@developer.gserviceaccount.com",
    "redirect_uris":[
      "http://MY_APP_NAME.appspot.com",
      "http://localhost:8080"
    ],    
    "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/MY_CLIENT_ID@developer.gserviceaccount.com",
    "client_id":"MY_CLIENT_ID.apps.googleusercontent.com",
    "auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
    "javascript_origins":[
      "http://MY_APP_NAME.appspot.com",
      "http://localhost:8080"
    ]
  }
}

我在本地计算机上收到“无效凭据”。在 apppot.com 中运行良好。在部署到 apppot 之前,它在我的机器上工作。

会发生什么?

提前致谢

4

1 回答 1

0

好吧,我太傻了。我第一次忘记向用户提供授权链接,而不是错误。

CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), 'client_secrets.json')

oauth2_decorator = oauth2decorator_from_clientsecrets(
    CLIENT_SECRETS,
    scope='https://www.googleapis.com/auth/calendar',
    message=MISSING_CLIENT_SECRETS_MESSAGE)


class MyRequestHandler(webapp2.HandleRequest):
  @oauth2_decorator.oauth_aware
  def get(self):
    if oauth2_decorator.has_credentials():
      # do stuff...
    else:
      self.out.write(oauth2_decorator.authorize_url())


app = webapp2.WSGIApplication([
    ('/', MyRequestHandler),
    (oauth2_decorator.callback_path, oauth2_decorator.callback_handler())
], debug=True)

现在它工作正常:-)。

于 2013-01-05T12:43:11.663 回答