python - Python Google Drive API 下载私人文件 Google 登录屏幕
问题描述
我可以毫无问题地下载公共文件。作为管理员,我将列出可以下载私人文件的列表。(Google Drive 的功能)这是我的代码:https : //stackoverflow.com/posts/39225272/revisions 我在 .credentials 文件夹中有 oauth2client 和 google-drive-credentials.json 文件。在此文件(google-drive-credentials)中,“access_token”和“refresh_token”可用。当我尝试下载 PRIVATE 文件时,谷歌会返回带有请求的登录屏幕 html 文件。我需要自动通过这个。这不应该出现。
这是我的代码:
SCOPES = 'https://www.googleapis.com/auth/drive'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'abc'
authInst = auth.auth(SCOPES, CLIENT_SECRET_FILE, APPLICATION_NAME)
credentials = authInst.getCredentials()
URL = "https://docs.google.com/uc?export=download"
session = requests.Session()
session2 = requests.Session()
toplamyari = int(int(toplamboyut) / 2)
if os.path.exists(yazp1):
sizefile = os.path.getsize(yazp1)
yazmaModu = 'ab'
bytearalik = 'bytes=' + str(sizefile) + '-' + str(toplamyari)
else:
bytearalik = 'bytes=0' + '-' + str(toplamyari)
if os.path.exists(yazp1[0:len(yazp1)-1]+"2"):
dosyaboy=os.path.getsize(str(yazp1[0:len(yazp1)-1])+'2')
self.temp2boyut=dosyaboy
bytearalik2 = 'bytes=' + str(toplamyari + 1 + dosyaboy) + '-' + str(toplamboyut)
else:
bytearalik2 = 'bytes=' + str(toplamyari+1) + '-' + str(toplamboyut)
arlik2=int(int(toplamboyut)-int(toplamyari))
accessToken = credentials.access_token
session.headers.update({'Range': bytearalik, 'Authorization':'Bearer " + accessToken})
#session2.headers.update({'Range': bytearalik2}) #don't worry about that, that's for download faster(download 2 part at same time)
response = session.get(URL, params={'id':link}, headers={'Authorization':'Bearer '+accessToken}, stream=True)
正如我所说,使用该代码,我可以下载公共文件,但无法下载给定私人文件的访问权限。我认为请求不能访问,idk。对不起英语。谢谢大家。
解决方案
固定更改的 URL。
accessToken = credentials.access_token
#refreshTok=credentials.refresh_token
tokenTotal = 'Bearer ' + str(accessToken)
session.headers.update({'Range': bytearalik, 'Authorization':tokenTotal, 'token_type':'Bearer'})
session2.headers.update(
{'Range': bytearalik2, 'Authorization':tokenTotal ,'token_type': 'Bearer', 'accept': '*/*',
'accept-encoding': 'gzip, deflate', 'user-agent': '(gzip)',
'x-goog-api-client': 'gdcl/1.7.11 gl-python/3.7.3'})
URL='https://www.googleapis.com/drive/v3/files/'+link+'?alt=media'
download = self.DownladInfo()
download.create(self.getOyunfromID(listedekiIndex), boyut, 0, listedekiIndex, sifre, hizlimiti)
download.driveadi = dosyadi[0:dosyadi.rfind('.')]
self.indirilecek_indexler.append(download)
response = session.get(URL, stream=True)
response2 = session2.get(URL, stream=True)
if response.status_code != 206 or response2.status_code != 206:
self.indirmeyebaslarkenhatalar.append(response.status_code)
return
推荐阅读
- ruby-on-rails - 当我从 After hook 调用 erb 程序时,没有呈现 Html 文件
- java - Google API 不适用于 Spring Boot 和 Maven
- python - 如何在 python 中使用 matplotlib 设置 x 轴刻度线标签?
- keras - 如何纠正错误 ImportError: cannot import name 'MobileNet'?
- c - 如何在C中的给定地址动态分配内存?
- python-3.x - 如何对文本数据(IMDB 评论)执行回归算法?
- python - 在函数中更改具有相似名称的变量值
- python - matplotlib 动画保存不遵守 blit=True 但它似乎在 plt.show() 中工作得很好
- java - bignumber 的 BigDecimal 在浮点值中无法正常工作
- java - 如何有效地计算数组项之间的差异并找出有多少差异小于某个值?