iterative / PyDrive2

Google Drive API Python wrapper library. Maintained fork of PyDrive.
https://docs.iterative.ai/PyDrive2
Other
580 stars 69 forks source link

Question: is there a special method to just perform authentication/authorization? #225

Closed covalschi closed 2 years ago

covalschi commented 2 years ago

Hello,

I'm using a service account and have to upload files from memory, so I've used this workaround:

def upload_to_gdrive(gauth, folder_id, file_content, file_name):
    metadata = {
        "name": file_name,
        "parents": [folder_id]
    }
    files = {
        'data': ('metadata', json.dumps(metadata), 'application/json'),
        'file': io.BytesIO(file_content)
    }
    r = requests.post(
        "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable",
        headers={"Authorization": "Bearer " + gauth.credentials.access_token},
        files=files
    )
    return

As far as I understand, there's no refresh token for service accounts so as soon as I have the token it's fine. However, I'm forced to perform at least one operation (in my case I fetch folder id in another function) before gauth.credentials.access_token is populated. I've tried calling gauth.ServiceAuth() and gauth.Authorize(), but it didn't populate gauth.credentials.access_token. Is there a way to just get access token using pydrive2?

Thanks in advance.

shcheklein commented 2 years ago

I think you could use gauth.Get_Http_Object() to get an Http client that can be used to do post / get requests. I'm not sure you need to replicate all this logic though (resumable in memory upload), it already exists. Check my answer here #224