2017-10-18 1 views
0

Python3을 사용하여 Google 드라이브에서 파일을 다운로드하려고합니다. 나는 다음과 같은 오류가 점점 오전 :HttpError 403이 Python3 Google 드라이브 API를 사용하여 파일을 다운로드하는 중입니다.

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

내 코드는 이것이다 :

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에이 코드를 발견했다.

파일의 메타 데이터에 액세스 할 수 있지만 파일을 다운로드하는 데 권한 오류가 있습니다. 인증을 위해이 파일을 다운로드하려는 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을 사용하여 파일을 다운로드 할 수있는 다른 방법이 있습니까?

답변

0

Python documentation에 기반하여 urlopen은 '403'(금지 요청)처럼 처리 할 수없는 메시지에 대해 HTTPError을 발생시킵니다. list of error codes에서

이와

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

는 로컬 파일에 대한 URL로 표시 네트워크 객체를 복사합니다 urlretrieve()를 사용해보십시오.

자세한 내용은 관련 SO post을 참조하십시오.

0

마지막으로 이유를 발견했습니다. 실제로 Google OAuth 2.0 인증을 사용하는 자격증 명을 얻었을 때 메타 데이터 범위을 추가했습니다. 나는 scopes을 더 추가해야했다. 그래서 더 많은 범위를 추가함으로써, 그것은 나를 위해 일했습니다 :)

관련 문제