Skip to content
This repository has been archived by the owner on Jul 22, 2021. It is now read-only.

Google Api Drive Issue while downloading a file #222

Open
bhavnish07 opened this issue Mar 8, 2021 · 1 comment
Open

Google Api Drive Issue while downloading a file #222

bhavnish07 opened this issue Mar 8, 2021 · 1 comment

Comments

@bhavnish07
Copy link

bhavnish07 commented Mar 8, 2021

Google api Client don't give me latest version of file stored on google drive. It will always give me older version of file

Any help in this will be helpful
Thanks in Advance

 def get_drive_service(self):
        if not self.env.user.oauth_access_token:
            raise UserError(_("Please Add google access token to your account for uploading files to google drive"))
        cred = Credentials(self.env.user.oauth_access_token)
        service = build("drive", "v3", credentials=cred)
        return service
  def convert_to_doc(self):
        if not self.google_drive_link:
            raise ValidationError(_("Please Add url of file"))
        file_id = self._get_key_from_url(self.google_drive_link)
        service = self.get_drive_service()
        with google_service_context() as manager:
            file = service.files().get(fileId=file_id).execute()
        request = service.files().get_media(fileId=file_id)
        fh = io.BytesIO()
        with google_service_context() as manager:
            downloader = MediaIoBaseDownload(fh, request)
        done = False
        while done is False:
            with google_service_context() as manager:
                status, done = downloader.next_chunk()
        fh.seek(0)
        self.write(
            {'doc_type': 'upload_attachment', 'content_binary': base64.b64encode(fh.read()), 'name': file.get('name')})
        return self
@rondreas
Copy link

rondreas commented Apr 9, 2021

Not exactly sure what could be the issue, I'm getting the latest version with this but only reading a textfile

import os

from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseDownload
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials

SCOPES = ['https://www.googleapis.com/auth/drive']

def get_credentials(credentials="credentials.json", token="token.json"):
    """ Get the credentials for accessing the google api 
    :param credentials: path to credentials
    :param token: path to token
    """
    creds = None

    # The token file stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists(token):
        creds = Credentials.from_authorized_user_file(token, SCOPES)

    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                    'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)

        # Save the credentials for the next run
        with open(token, 'w') as token:
            token.write(creds.to_json())

    return creds


def main():
    creds = get_credentials()
    service = build('drive', 'v3', credentials=creds)
    
    # Find the file id for the file "hello.txt"
    response = service.files().list(q="name = 'hello.txt'").execute()
    for file in response.get("files", []):
        fileId = file.get('id')
        
    request = service.files().get_media(fileId=fileId)

    # Download the content to a file.
    with open('download.txt', 'wb') as fout:
        download = MediaIoBaseDownload(fout, request)
        done = False
        while not done:
            status, done = download.next_chunk()

    # Now open it for read,
    with open('download.txt', 'r') as f:
        print(f.read())


if __name__ == '__main__':
    main()

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants