gpt4 book ai didi

python - 驱动器 API 错误 : python doesn't found json quickstart file

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

我正在尝试使用 Google-drive-api,但遇到了问题。我准确地遵循了 google Drive api 快速入门教程的每条说明,但我有一个我没有的文件错误。 Python quickstart tutorial这是我的代码:

from __future__ import print_function
import httplib2
import os

from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage

try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None

# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/drive-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/drive.metadata'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Drive API Python Quickstart'


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()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials

def main():
"""Shows basic usage of the Google Drive API.

Creates a Google Drive API service object and outputs the names and IDs
for up to 10 files.
"""
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('drive', 'v3', http=http)

results = service.files().list(
pageSize=10,fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])
if not items:
print('No files found.')
else:
print('Files:')
for item in items:
print('{0} ({1})'.format(item['name'], item['id']))

if __name__ == '__main__':
main()

结果:

C:\Python27\lib\site-packages\oauth2client_helpers.py:255: UserWarning: Cannot access C:\Users\Neyoh.credentials\drive-python-quickstart.json: No such file or directorywarnings.warn(_MISSING_FILE_MESSAGE.format(filename))

我的 python 脚本目录中有 client_secret.json 文件,但在 HOME/.credentials/ 中,我什么都没有。本教程讲的是 client_secret.json 而不是 drive-python-quickstart.json 。它从未被提及。什么是 drive-python-quickstart.json ?是同一个文件吗?

Edit1 :当我在 \.credentials\ 中使用 client_secret.json 而不是 drive-python-quickstart.json 我有这个错误:

File "C:\Python27\lib\site-packages\oauth2client\client.py", line 302, in new_from_json module_name = data['_module'] KeyError: '_module'

最佳答案

我遇到了与 google calendar api 相同的问题。要解决此问题,只需使用您的 CLIENT_SECRET_FILE 的绝对文件路径,例如:

CLIENT_SECRET_FILE = r'C:\Users\xxx\client_secret.json'

文件 drive-python-quickstart.json 将在 身份验证后自动创建。

关于python - 驱动器 API 错误 : python doesn't found json quickstart file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40546319/

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