我正在尝试使用Python3从Google云端硬盘下载文件。我收到以下错误:
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": "abacussocietygla@gmail.com"
}
],
"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": "abacussocietygla@gmail.com"
},
"capabilities": {
"canCopy": true,
"canEdit": true
},
"editable": true,
"copyable": true,
"writersCanShare": true,
"shared": false,
"explicitlyTrashed": false,
"appDataContents": false,
"headRevisionId": "0B8C3Gga47u6-WmEvTGx4cms0eGtJOTVaRXZtU2gwN2JoTG1FPQ",
"spaces": [
"drive"
]
如何解决此问题?有没有其他方法可以使用Python3下载文件?
答案 0 :(得分:0)
根据Python documentation,urlopen
会为那些无法处理的内容HTTPError
提升{403}(请求被禁止)。
403: ('Forbidden',
'Request forbidden -- authorization will not help').
有了这个,请尝试使用urlretrieve()
,它将URL表示的网络对象复制到本地文件。
有关其他见解,请参阅此相关SO post。
答案 1 :(得分:0)
最后,我找到了原因。实际上,当我获得凭据使用Google OAuth 2.0身份验证时,我只添加了读取MetaData范围。我应该添加更多the docs。 因此,通过添加更多范围,它对我有用:)