我正在尝试使用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中\站点包\ oauth2client_helpers.py:255: UserWarning:无法访问 C:\ Users \ Neyoh.credentials \ drive-python-quickstart.json:没有这样的文件 或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:当我在client_secret.json
而不是\.credentials\
中使用drive-python-quickstart.json
时出现此错误:
文件“C:\ Python27 \ lib \ site-packages \ oauth2client \ client.py”,第302行, 在new_from_json module_name = data ['_ module'] KeyError:'_ module'
答案 0 :(得分:2)
我使用google calendar api遇到了同样的问题。
要解决此问题,只需使用CLIENT_SECRET_FILE
的绝对文件路径,如:
CLIENT_SECRET_FILE = r'C:\Users\xxx\client_secret.json'
文件drive-python-quickstart.json
将在验证后自动生成。