I'm trying to use requests in python with the google drive api v3. I can get my script to authenticate and run calls with a service. But I notice that most of the documentation for API V3 shows examples using http requests under the "reference" tab. When I try to complete a call using requests, I'm not sure how to put my Oath 2.0 authentication in to reference the token.pickle. Tried to force the token to a string but did not work. The whole script is below. You can see the "#delete mp4File" section is where I'm trying to create a request and add authentication. This is where I need help.
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import os
import io
from googleapiclient.http import MediaIoBaseDownload
import requests
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/drive.metadata',
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file'
]
def main():
"""Shows basic usage of the Drive v3 API.
Prints the names and ids of the first 10 files the user has access to.
"""
creds = None
# The file token.pickle 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.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# 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(
'client_secrets.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('drive', 'v3', credentials=creds)
# List contents of mp4 folder
results = service.files().list(
q="'XXXXXXXXXXXXXXXXXXXX' in parents", pageSize=1).execute()
items = results.get('files', [])
if not items:
print('No files found.')
else:
for item in items:
mp4File=(u'{0}'.format(item['id']))
mp4Name=(u'{0}'.format(item['name']))
print (mp4Name + " " + mp4File)
# Download mp4File
file_id = mp4File
request = service.files().get_media(fileId=file_id)
fh = io.FileIO(mp4Name, 'wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
#delete mp4File
url = "https://www.googleapis.com/drive/v3/files/" + file_id
headers = {
'Authorization': 'Bearer ' + str(token)
}
response = requests.request("DELETE", url, headers=headers)
print(response.text.encode('utf8'))
if __name__ == '__main__':
main()
When I try to complete a call using requests, I'm not sure how to put my Oath 2.0 authentication in to reference the token.pickle.. Can I ask you about the detail of your current issue and your goal? For example, is Quickstart for python useful?token.picklefile and reauthenticate, as the quickstart only provides a read-only scope in its example.