2016-11-11 13 views
3

GoogleドライブAPIを使用しようとしていますが、問題があります。私はgoogle Drive APIのクイックスタートチュートリアルのすべての指示に正確に従いました。私は持っていないファイルにはエラーがあります。私は私のPythonスクリプトのディレクトリにclient_secret.jsonファイルを持っていますが、HOME/.credentials/に、私は何もありませんドライブAPIエラー:pythonが見つかりませんjsonクイックスタートファイル

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

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

結果: Python quickstart tutorial は、ここに私のコードです。 チュートリアルでは、client_secret.jsonについては説明していますが、約drive-python-quickstart.jsonについては説明していません。それは決して言及されていない。 drive-python-quickstart.jsonとは何ですか?同じファイルですか?

EDIT1:私は\.credentials\の代わりdrive-python-quickstart.jsonclient_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

私はGoogleカレンダーAPIと同じ問題に入りました。

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

ファイルdrive-python-quickstart.jsonは自動的に認証を作成されます。 は、単にあなたのCLIENT_SECRET_FILEのように絶対ファイルパスを使用して問題を解決するには。

関連する問題