PythonでGoogleドライブAPI(v3)を使用して、ファイルを取得してGoogleドライブアカウントにアップロードしようとしています。GoogleドライブAPI v3 PythonのArgparseを上書きする
私はセットアップには、このガイドに私の認証を使用: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方法はしかし、異なるargparserを使用していますが、 google wrote(http://oauth2client.readthedocs.io/en/latest/source/oauth2client.tools.htmlを参照)
私はこのスクリプトをユーザー名、ファイル名などの自分の入力で実行するたびに、「認識できない引数」というエラーが表示され続けます。
arg_serverをrun_flowに上書きする方法を教えてください。
EDIT:
誰かがparse_known_argsを使用して示唆しました()。
まあ、args、flags = parser.parse_known_args()と言ってパースするようにコードを修正しました。入力はフラグに入ります。
私はスクリプトを実行して3つの引数を渡すと、引数を "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メソッドとパーサーには何があるのか分かりません。
それらゴードでif__name__ブロックはちょうど(メイン初期化)。 argparseの部分は、関数の外側の始めにあります。私は実際にこの記事について考えていましたが、どうすれば同様のことができるのでしょうか?https://stackoverflow.com/questions/26130741/using-argparse-with-google-admin-apiそれはちょうどsamples_tools.init()がここにないことです新しいバージョンでは – shishy
parse_known_argsの問題は、get_infoメソッドがこのエラーを引き起こしていることです。これはgoogleメソッドの一部です。私がコマンドラインに情報を渡すと、それはそれを解析して、「ああ、これは私が期待しているものではない」と言うことを試みている – shishy