用Python覆盖Google Drive API v3 Argparse
我正在尝试将Google Drive API(v3)与Python结合使用,以获取文件并将其上传到我的Google Drive帐户。
我使用此指南来设置身份验证:https
:
//developers.google.com/drive/v3/web/quickstart/python
但是对于我的程序,我想通过命令行输入用户名,文件名和output_filename。我修改了Google文档代码,并执行了以下操作:
from __future__ import print_function
import httplib2
import os
from sys import argv
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
from apiclient.http import MediaIoBaseDownload, MediaIoBaseUpload
import io
try:
import argparse
parser = argparse.ArgumentParser(description="I want your name, the file ID, and the folder you want to dump output to")
parser.add_argument('-u', '--username', help='User Name', required=True)
parser.add_argument('-f', '--filename', help='File Name', required=True)
parser.add_argument('-d', '--dirname', help = 'Directory Name', required=True)
flags = parser.parse_args()
except ImportError:
flags = None
SCOPES = 'https://www.googleapis.com/auth/drive'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Drive API Python Quickstart'
...#rest of the code is from the Google Drive Documentation (see above)
def get_credentials():
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'drive-python-quickstart.json')
store = Storage(credential_path)
credentials = store.get()
#Credentials returns NONE
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if args:
credentials = tools.run_flow(flow, store)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
print("check")
return credentials
问题是在get_credentials方法中,有一行显示:
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
不过,run_flow方法使用了Google编写的另一个argparser(请参阅:http
:
//oauth2client.readthedocs.io/en/latest/source/oauth2client.tools.html)
因此,每当我使用自己的输入(包括用户名,文件名等)运行此脚本时,都会不断收到错误消息,显示“无法识别的参数”。
如何使argparser覆盖run_flow中的argparser?
编辑:
有人建议使用parse_known_args()。
好吧,我修改了我的代码以解析为args,flags = parser.parse_known_args(),因为那样的话,任何杂项。输入将变成标志。
这个想法是,如果我运行脚本并给我3个args,它将把它们拉入“ args”。
但是,这又是一个问题,就是稍后我在get_credentials中调用run_flow方法时,它会引发错误:
用法:name.py [–auth_host_name AUTH_HOST_NAME] [–noauth_local_webserver]
[–auth_host_port [AUTH_HOST_PORT …]]] [–logging_level
{DEBUG,INFO,WARNING,ERROR,CRITICAL}]无法识别的参数:-u shishy -f fname -d
random_name
我认为它仍将我的命令行输入传递给get_info方法,而解析器不知道该如何处理…