2016-09-06 27 views
0

私はGoogleドライブからスプレッドシートをダウンロードするためにGoogle APIにアクセスするためにPythonを使用しようとしています。私はGoogle apiの例からコードを借用し、しかし、毎回実行すると、私はいつも「不十分な許可」を持っていると言ってこのhttpErrorを返します。私は下に私のコードを添付して、誰かがそれを見て、私にどの部分が間違っているかを教えてもらえるかどうか疑問に思っています。ありがとうPYTHON Googleドライブスプレッドシートをダウンロード

import httplib2 
import os 
from apiclient.http import MediaIoBaseDownload 
from apiclient import discovery 
import oauth2client 
from oauth2client import client 
from oauth2client import tools 
import io 

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

SCOPES = 'https://www.googleapis.com/auth/drive' 
CLIENT_SECRET_FILE = 'client_secret.json' 
APPLICATION_NAME = 'Drive API Python Quickstart' 

def get_credentials(): 

    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 = oauth2client.file.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(): 

    credentials = get_credentials() 
    http = credentials.authorize(httplib2.Http()) 
    service = discovery.build('drive', 'v3', http=http) 
    file_id = '1tRL7q2AYdQuuaWQyfT700H3WG6Klod9EtjF2yTMoGiE' 
    request = service.files().export_media(fileId=file_id, 
               mimeType='text/csv') 
    fh = io.FileIO('LISAtrial.csv') 
    downloader = MediaIoBaseDownload(fh, request) 
    done = False 
    while done is False: 
     status, done = downloader.next_chunk() 


if __name__ == '__main__': 
    main() 

答えて

0

ファイルにアクセスするユーザーのアクセス許可を確認したい場合があります。 Handling API Errors GoogleドライブAPI V3のドキュメント、

403:ファイルアクセス権が不十分です。ユーザーはファイル{fileId}に対して十分な権限を持っていません。

{ 
"error": { 
"errors": [ 
{ 
"domain": "global", 
"reason": "insufficientFilePermissions", 
"message": "The user does not have sufficient permissions for file {fileId}." 
} 
], 
"code": 403, 
"message": "The user does not have sufficient permissions for file {fileId}." 
} 
} 

またfiles.getによって取り出されたメタデータでのユーザーのアクセスレベルをチェックして、読み取り専用のUIにあなたのUIを変更するためにそれを使用することをお勧めします。

ユーザの許可とScopesの不適切な使用は、Insufficient Permissionという結果になる可能性があります。

:ここ

は、いくつかのSOの質問に関連しています

関連する問題