gpt4 book ai didi

python - 使用 oauth2 和 Google API 的无法识别的参数

转载 作者:太空宇宙 更新时间:2023-11-03 11:18:57 24 4
gpt4 key购买 nike

我在某些脚本中使用 Google API 服务时遇到了一些问题。这个错误有点奇怪,但我们开始吧。我有一个列出我的 Google 云端硬盘文件的脚本。

from apiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools

SCOPES = 'https://www.googleapis.com/auth/drive.readonly.metadata'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
DRIVE = discovery.build('drive', 'v3', http=creds.authorize(Http()))

files = DRIVE.files().list().execute().get('files', [])
for f in files:
print(f['name'], f['mimeType'],f['id'])

它运行良好,我从 Google API 下载 client_secret.json 并将其保存在同一文件夹中,然后我启动脚本以检查一切是否正常。

然后我开始编辑我的文件以更改我执行它的方式并且不读取文件,而是调用脚本并发送 client_idclient_secret 脚本的字符串,最终版本是这样的:

import sys
from apiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools

# ID and SECRET arguments
client_id = sys.argv[1]
client_secret = sys.argv[2]

SCOPES = 'https://www.googleapis.com/auth/drive.readonly.metadata'

def listFiles(drive):
"""Receive the service and list the files"""
files = drive.files().list().execute().get('files', [])
for f in files:
print(f['name'], f['mimeType'],f['id'])

def main():
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.OAuth2WebServerFlow(client_id, client_secret, SCOPES)
creds = tools.run_flow(flow, store, tools.argparser.parse_args())
DRIVE = discovery.build('drive', 'v3', http=creds.authorize(Http()))
listFiles(DRIVE)


if __name__ == "__main__":
main()

我第一次启动这个新版本的脚本时它起作用了,因为旧版本中的脚本已经创建了 storage.json 文件。然后我将我的新版本脚本移动到另一个文件夹或机器(其中 storage.json 文件不存在)以检查它是否有效然后我得到这个:

$ python3 drive_list.py asdasdasdsa jijfkljflksdjflksdj

/usr/local/lib/python3.4/dist-packages/oauth2client/_helpers.py:255: UserWarning: Cannot access storage.json: No such file or directory
warnings.warn(_MISSING_FILE_MESSAGE.format(filename))
usage: drive_list.py [--auth_host_name AUTH_HOST_NAME]
[--noauth_local_webserver]
[--auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT ...]]]
[--logging_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}]
drive_list.py: error: unrecognized arguments: asdasdasdsa jijfkljflksdjflksdj

关于 storage.json 文件的警告是正常的,出现在两个脚本版本中,是 oauth2client 的一部分。

这是古玩部分,为什么在 storage.json 文件存在时识别参数(仅在读取 client_secret.json 时创建)?如果脚本第一次启动,它会创建文件。这个错误真的很奇怪,我只是想找出到底发生了什么。如果有人能帮助我,我将非常感激。

最佳答案

发生这种情况是因为您正在导入 oauth2client.tools 模块。

要正常工作,此模块依赖于标准 argparse模块。如果您不知道,这个标准模块用于编写用户友好的命令行界面,可以轻松管理命令行参数。这与您对 sys.argv[1]sys.argv[2] 参数的使用不一致。

要解决此问题,您可以向命令行添加新参数,如下例所示。通过此修改,您将像这样运行该工具

python3 drive_list.py -ci "your_client_id" -cs "your_client_secret"

这里是你的代码稍微修改以添加新的命令行参数:

import argparse
from apiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools

# ID and SECRET arguments as new command line parameters
# Here is where you extend the oauth2client.tools startnd arguments
tools.argparser.add_argument('-ci', '--client-id', type=str, required=True, help='The client ID of your GCP project')
tools.argparser.add_argument('-cs', '--client-secret', type=str, required=True,
help='The client Secret of your GCP project')

SCOPES = 'https://www.googleapis.com/auth/drive.readonly.metadata'


def list_files(drive):
"""Receive the service and list the files"""
files = drive.files().list().execute().get('files', [])
for f in files:
print(f['name'], f['mimeType'], f['id'])


def main():
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
# You want to be sure to parse the args to take in consideration the new parameters
args = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
flow = client.OAuth2WebServerFlow(args.client_id, args.client_secret, SCOPES)
creds = tools.run_flow(flow, store, tools.argparser.parse_args())
drive_sdk = discovery.build('drive', 'v3', http=creds.authorize(Http()))
list_files(drive_sdk)


if __name__ == "__main__":
main()

关于python - 使用 oauth2 和 Google API 的无法识别的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46737536/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com