使用 oauth2 和 Google API 的无法识别的参数
Unrecognized arguments using oauth2 and Google APIs
我在某些脚本中使用 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_id 和 client_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()
我在某些脚本中使用 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_id 和 client_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()