【发布时间】:2020-05-23 11:12:29
【问题描述】:
我曾尝试使用 python 脚本将文件从 Google Drive 下载到我的本地系统,但在运行 Python 脚本时遇到了“禁止”问题。脚本如下:
import requests
url = "https://www.googleapis.com/drive/v3/files/1wPxpQwvEEOu9whmVVJA9PzGPM2XvZvhj?alt=media&export=download"
querystring = {"alt":"media","export":"download"}
headers = {
'Authorization': "Bearer TOKEN",
'Host': "www.googleapis.com",
'Accept-Encoding': "gzip, deflate",
'Connection': "keep-alive",
}
response = requests.request("GET", url, headers=headers, params=querystring)
print(response.url)
#
import wget
import os
from os.path import expanduser
myhome = expanduser("/home/sunarcgautam/Music")
### set working dir
os.chdir(myhome)
url = "https://www.googleapis.com/drive/v3/files/1wPxpQwvEEOu9whmVVJA9PzGPM2XvZvhj?alt=media&export=download"
print('downloading ...')
wget.download(response.url)
在这个脚本中,我遇到了禁止的问题。我在脚本中做错了吗?
我还尝试了另一个在 Google Developer 页面上找到的脚本,如下所示:
import auth
import httplib2
SCOPES = "https://www.googleapis.com/auth/drive.scripts"
CLIENT_SECRET_FILE = "client_secret.json"
APPLICATION_NAME = "test_Download"
authInst = auth.auth(SCOPES, CLIENT_SECRET_FILE, APPLICATION_NAME)
credentials = authInst.getCredentials()
http = credentials.authorize(httplib2.Http())
drive_serivce = discovery.build('drive', 'v3', http=http)
file_id = '1Af6vN0uXj8_qgqac6f23QSAiKYCTu9cA'
request = drive_serivce.files().export_media(fileId=file_id,
mimeType='application/pdf')
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print ("Download %d%%." % int(status.progress() * 100))
这个脚本给了我一个网址不匹配错误。
那么在 Google 控制台凭据中应该为重定向 URL 提供什么?或任何其他解决方案?我是否必须在两个脚本中从 Google 授权我的 Google 控制台应用程序?如果是这样,授权该应用程序的过程将是什么,因为我还没有找到任何相关文件。
【问题讨论】:
-
使用 google discovery api for python:developers.google.com/drive/api/v3/quickstart/python
-
你好@Ramon,我已经尝试了这两个链接,但面临同样的禁止问题。
-
您是否在API console 中创建了项目?
-
@Aerials 是的,我已经在 API 控制台中创建了项目。