Drive API 错误:python 未找到 json 快速入门文件

Ney*_*yoh 4 python google-drive-api google-oauth

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

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()
Run Code Online (Sandbox Code Playgroud)

结果 :

C:\Python27\lib\site-packages\oauth2client_helpers.py:255: UserWarning: 无法访问 C:\Users\Neyoh.credentials\drive-python-quickstart.json: 没有这样的文件或目录warnings.warn(_MISSING_FILE_MESSAGE.format(文档名称))

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

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

文件“C:\Python27\lib\site-packages\oauth2client\client.py”,第 302 行,在 new_from_json module_name = data['_module'] KeyError: '_module'

小智 5

我遇到了与谷歌日历 api 相同的问题。要解决该问题,只需使用您CLIENT_SECRET_FILE喜欢的绝对文件路径:

CLIENT_SECRET_FILE = r'C:\Users\xxx\client_secret.json'
Run Code Online (Sandbox Code Playgroud)

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