【问题标题】:Google Calendar API for OAuth Web Application in Python FlaskPython Flask 中用于 OAuth Web 应用程序的 Google Calendar API
【发布时间】:2018-05-26 14:47:19
【问题描述】:

我一直在 SO 和 SE 寻找这个问题的特定答案,但似乎找不到答案。我正在构建一个简单的 Web 应用程序,它使用 Python Flask 访问 Google 日历并根据条件填写日历。我的问题是 OAuth2 并让 redirect_uris 正确。下面是我的代码,每次我尝试运行我的代码时,我都会收到重定向 URI 不正确的错误,并且应用程序将无法继续运行。它似乎痴迷的重定向uri是http://localhost:8080。我已经尝试设置将该重定向 URI 添加到我的开发人员控制台,添加带有尾部“/oauth2callback”(带有和不带有尾部“/”)的 URI。我也尝试过完全指定一个不同的 URI,但错误一直提到http://localhost:8080,尽管使用了不同的 client_secret.json 文件并在 flow_from_client_secrets 中指定了其他 uri,但似乎永远无法识别任何其他重定向 URI。在每次运行脚本以及在 chrome 的隐身模式下运行之前,我还清除了我的缓存。似乎没有任何帮助。我知道它一定很简单,如果有人能指出它是什么,我将非常感激!

SCOPES = 'https://www.googleapis.com/auth/calendar'
CLIENT_SECRET_FILE = 'client_secret_web.json'
APPLICATION_NAME = 'Example Calendar Application'
REDIRECT_URI = 'http://placeholder.com/oauth2callback'

home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    redirect_uri=""
    credential_path = os.path.join(credential_dir,
                                   'calendar-python-quickstart.json')

    store = Storage(credential_path)
    credentials = store.get()
    flow = client.flow_from_clientsecrets(self.CLIENT_SECRET_FILE, 
    self.SCOPES, prompt='consent', redirect_uri=self.REDIRECT_URI)
    flow.user_agent = self.APPLICATION_NAME
    if self.flags:
        credentials = tools.run_flow(flow, store, self.flags)
    else: # Needed only for compatibility with Python 2.6
        credentials = tools.run(flow, store)

        print('Storing credentials to ' + credential_path)
    return credentials

【问题讨论】:

  • 有谁可以回答这个问题吗?老实说,我把头撞在墙上已经有一段时间了。仍然无法找到有效的答案。

标签: python flask oauth oauth-2.0 google-api-python-client


【解决方案1】:

我认为您必须删除在 ~/.credentials/calendar-python-quickstart.json 创建的文件,以便程序再次请求权限

【讨论】:

    猜你喜欢
    • 2022-09-23
    • 1970-01-01
    • 2018-04-28
    • 2011-02-03
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    • 2011-07-28
    相关资源
    最近更新 更多