python - 在没有本地网络服务器的情况下在 Python 中使用 Google 客户端库

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

我有一个与 Google API 交互的 Python 程序,该程序通过 OAuth 使用 Google 提供的示例代码进行身份验证:

# Perform OAuth 2.0 authorization.
flow = oauth2client.client.flow_from_clientsecrets(
  parameters[self.PARAM_SECRETS], scope=self.GCE_SCOPE)
storage = oauth2client.file.Storage(LocalState.get_oauth2_storage_location(
  parameters[self.PARAM_KEYNAME]))
credentials = storage.get()

if credentials is None or credentials.invalid:
  credentials = oauth2client.tools.run(flow, storage)

# Build the service
return apiclient.discovery.build('compute', self.API_VERSION), credentials

但是,每当我运行此命令时,它都会强制打开浏览器并要求用户单击按钮进行身份验证。如果程序在没有本地 Web 服务器的计算机上运行,​​这会导致问题。我见过this question ,这似乎表明问题可以通过使用 --noauth_local_webserver 来解决,但它说我必须重写我的程序以使用 gflags 进行参数处理,并且我的现有代码已使用 argparse

有没有一种方法可以在不强制打开浏览器的情况下对用户进行身份验证,而无需重写整个应用程序以使用 gflags

最佳答案

好吧,今天它不会对你有帮助,但是客户端库中对 gflags 的依赖是 being removed在 1.2 版本中。更新后run() command now uses argparse ,并且通常不会依赖于核心库中的任何特定标志库。不幸的是,这并不能解决你今天的问题。我希望在本月底之前发布 1.2 版本,但不能保证。

关于python - 在没有本地网络服务器的情况下在 Python 中使用 Google 客户端库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16860571/

相关文章:

php - Facebook OAuth redirect_uri 问题 'Given URL is not permitted by the application configuration.'

python - 使用 python 更新谷歌电子表格的单元格背景颜色

python - YouTube 数据 API v3 刷新 token 在发布状态设置为测试的应用程序上不断过期

c# - 访问/保护 Restful 服务 oAuth2

javascript - 基于 Angular.js + Node.js/PassportJS 的 SPA 上的 oauth2 身份验证

gmail-api - GMail API-messages()。list()仅返回消息ID

python - 按垂直顺序写入文本文件

从同一目录调用时,Python subprocess.run 不起作用

python - Azure 存储访问被拒绝

python - 使用 timedelta 将 15 分钟添加到当前时间戳