首页 > 解决方案 > 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。对不起英语。谢谢大家。

标签: pythonoauthdownloadgoogle-drive-api

解决方案


固定更改的 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

推荐阅读