2016-11-11 30 views
3

我试图使用Google-drive-api,并且出现问题。我准确地遵循了谷歌Drive api的快速入门教程的每一条指令,并且我有一个我没有的文件出错。 Python quickstart tutorial 这里是我的代码:驱动器API错误:python未找到json quickstart文件

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))

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

EDIT1:当我在\.credentials\,而不是drive-python-quickstart.json使用client_secret.json我有这样的错误:

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

+0

@DaImTo当我使用'client_secret.json',我有这个错误:'File“C:\ Python27 \ lib \ site-packages \ oauth2client \ client.py”,302行,在new_from_json中 module_name = data [ '_module'] KeyError:'_module' – Neyoh

+0

我发现,'drive-python-quickstart.json'必须是一个空文件。 – Neyoh

回答

1

我就与谷歌日历API相同的问题。 为了解决这个问题,只需使用绝对路径到你CLIENT_SECRET_FILE像:

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

文件drive-python-quickstart.json将自动创建认证。