简体   繁体   中英

Python Google Drive API Download Private Files Google Login Screen

I can download public files without problem. As I admin, i will make a list who can download private file. (Google Drive's feature) This is my code: https://stackoverflow.com/posts/39225272/revisions I have oauth2client and google-drive-credentials.json file on.credentials folder. In this file (google-drive-credentials) "access_token" and "refresh_token" available. When I try to download PRIVATE files, google returns Login screen html file with request. I need to pass this with automatic. This should never come.

Here is my code:

 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)

with that code as i said, i can download PUBLIC files but can not download access given private files. I think request not access, idk. And sorry for English. Thanks for all.

FIXED Changed 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM