2017-10-18 15 views
0

Python3を使用してGoogleドライブからファイルをダウンロードしようとしています。私は、次のエラーを取得しています:HttpError 403がPython3 GoogleドライブAPIを使用してファイルをダウンロード中

import io 
    from googleapiclient.http import MediaIoBaseDownload 
    file_id = '0B8C3Gga47u6-MVlPMnR1MENYb2M' 
    request = drive_service.files().get_media(fileId=file_id) 
    fh = io.BytesIO() 
    downloader = MediaIoBaseDownload(fh, request) 
    done = False 
    while done is False: 
     status, done = downloader.next_chunk() 
     print("Downloading", status.progress() * 100) 

私はGoogle's official tutorialにこのコードを見つけました:

Traceback (most recent call last): 
    File "/run/media/prateek/EC3096E23096B356/Users/Prateek Agrawal/Google Drive/projects/mini-project/cloudDrive/a.py", line 91, in <module> 
    status, done = downloader.next_chunk() 
    File "/usr/lib/python3.6/site-packages/oauth2client/_helpers.py", line 133, in positional_wrapper 
    return wrapped(*args, **kwargs) 
    File "/usr/lib/python3.6/site-packages/googleapiclient/http.py", line 692, in next_chunk 
    raise HttpError(resp, content, uri=self._uri) 
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/drive/v2/files/0B8C3Gga47u6-MVlPMnR1MENYb2M?alt=media returned "The authenticated user has not granted the app 159208223574 read access to the file 0B8C3Gga47u6-MVlPMnR1MENYb2M"> 

私のコードはこれです。

ファイルのメタデータにアクセスできますが、ファイルをダウンロードする際に権限エラーが発生しています。認証のために、このファイルをダウンロードしようとしているのと同じGoogleアカウントを使用しています。私はファイルのメタデータをチェックしており、メタデータには私が所有者であるためダウンロードする権限があることがわかりました。

次は、ファイルのメタデータである:

{ 
"kind": "drive#file", 
"id": "0B8C3Gga47u6-MVlPMnR1MENYb2M", 
"etag": "\"bFBIJPAQTIelBiVAnqXY4FaYwW4/MTUwODI3NjI3NDUzNA\"", 
"selfLink": "https://www.googleapis.com/drive/v2/files/0B8C3Gga47u6-MVlPMnR1MENYb2M", 
"webContentLink": "https://drive.google.com/uc?id=0B8C3Gga47u6-MVlPMnR1MENYb2M&export=download", 
"alternateLink": "https://drive.google.com/file/d/0B8C3Gga47u6-MVlPMnR1MENYb2M/view?usp=drivesdk", 
"embedLink": "https://drive.google.com/file/d/0B8C3Gga47u6-MVlPMnR1MENYb2M/preview?usp=drivesdk", 
"iconLink": "https://drive-thirdparty.googleusercontent.com/16/type/application/zip", 
"title": "folder2-20171017T213157Z-001.zip", 
"mimeType": "application/zip", 
"labels": { 
    "starred": false, 
    "hidden": false, 
    "trashed": false, 
    "restricted": false, 
    "viewed": true 
}, 
"createdDate": "2017-10-17T21:37:54.534Z", 
"modifiedDate": "2017-10-17T21:37:54.534Z", 
"modifiedByMeDate": "2017-10-17T21:37:54.534Z", 
"lastViewedByMeDate": "2017-10-18T09:18:25.901Z", 
"markedViewedByMeDate": "1970-01-01T00:00:00.000Z", 
"version": "4", 
"parents": [ 
    { 
    "kind": "drive#parentReference", 
    "id": "0AMC3Gga47u6-Uk9PVA", 
    "selfLink": "https://www.googleapis.com/drive/v2/files/0B8C3Gga47u6-MVlPMnR1MENYb2M/parents/0AMC3Gga47u6-Uk9PVA", 
    "parentLink": "https://www.googleapis.com/drive/v2/files/0AMC3Gga47u6-Uk9PVA", 
    "isRoot": true 
    } 
], 
"userPermission": { 
    "kind": "drive#permission", 
    "etag": "\"bFBIJPAQTIelBiVAnqXY4FaYwW4/InBwySrqVAcXWoXoJ-e22C6as5M\"", 
    "id": "me", 
    "selfLink": "https://www.googleapis.com/drive/v2/files/0B8C3Gga47u6-MVlPMnR1MENYb2M/permissions/me", 
    "role": "owner", 
    "type": "user" 
}, 
"originalFilename": "folder2-20171017T213157Z-001.zip", 
"fileExtension": "zip", 
"md5Checksum": "a97c09989b2c14e3c5866c8390da1daa", 
"fileSize": "777", 
"quotaBytesUsed": "777", 
"ownerNames": [ 
    "Abacus Gla" 
], 
"owners": [ 
    { 
    "kind": "drive#user", 
    "displayName": "Abacus Gla", 
    "picture": { 
     "url": "https://lh3.googleusercontent.com/-f5pVFhZGOyo/AAAAAAAAAAI/AAAAAAAAAA0/idMajpfWGVA/s64/photo.jpg" 
    }, 
    "isAuthenticatedUser": true, 
    "permissionId": "16118565815584902923", 
    "emailAddress": "[email protected]" 
    } 
], 
"lastModifyingUserName": "Abacus Gla", 
"lastModifyingUser": { 
    "kind": "drive#user", 
    "displayName": "Abacus Gla", 
    "picture": { 
    "url": "https://lh3.googleusercontent.com/-f5pVFhZGOyo/AAAAAAAAAAI/AAAAAAAAAA0/idMajpfWGVA/s64/photo.jpg" 
    }, 
    "isAuthenticatedUser": true, 
    "permissionId": "16118565815584902923", 
    "emailAddress": "[email protected]" 
}, 
"capabilities": { 
    "canCopy": true, 
    "canEdit": true 
}, 
"editable": true, 
"copyable": true, 
"writersCanShare": true, 
"shared": false, 
"explicitlyTrashed": false, 
"appDataContents": false, 
"headRevisionId": "0B8C3Gga47u6-WmEvTGx4cms0eGtJOTVaRXZtU2gwN2JoTG1FPQ", 
"spaces": [ 
    "drive" 
] 

私はこの問題を解決することができますか? Python3を使ってファイルをダウンロードする別の方法はありますか? Python documentationから基づい

答えて

0

urlopenは、それは `「403」(要求は禁止)のように扱うことができない人のためHTTPErrorを発生させます。 list of error codesから

、これにより

403: ('Forbidden', 
      'Request forbidden -- authorization will not help'). 

は、ローカルファイルへのURLで表されるネットワーク上のオブジェクトをコピーしますurlretrieve()を使用してみてください。

この詳細については、SO postをご覧ください。

0

最後に、理由を見つけました。実際に、Google OAuth 2.0認証を使用して資格情報を取得したときは、のみを追加しました。MetaDataスコープを読んでください。私はさらにscopesを追加する予定だった。 スコープを追加することで、それは私のために働いた:)

関連する問題