Open bhavnish07 opened 3 years ago
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()
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