Python Google Drive API下载私有文件Google登录屏幕

2024-10-04 11:28:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以毫无问题地下载公共文件。 作为我的管理员,我会列出谁可以下载私人文件。(谷歌硬盘的功能) 这是我的代码:https://stackoverflow.com/posts/39225272/revisions 我有oauth2client和google drive-凭据.json文件位于.credentials文件夹中。 在此文件(google驱动器凭据)中,“access\u token”和“refresh\u token”可用。 当我尝试下载私人文件时,google会返回带有请求的登录屏幕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。对不起英语。谢谢大家。你知道吗


Tags: 文件pathhttpscomtokenbytesosgoogle
1条回答
网友
1楼 · 发布于 2024-10-04 11:28:57

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

相关问题 更多 >