I am trying to create a token.json file to save data without having to log in every time to approve an account
This is a code that deletes 100 files at the same time. Every time I run a code, I must log in to the account for approval. All you need to do is add a save file token.json
<code>from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.http import BatchHttpRequest
SCOPES = ['https://www.googleapis.com/auth/drive']
def callback(request_id, response, exception):
if exception:
print(f"An error occurred: {exception}")
else:
print(f"Deleted file ID: {request_id}")
def main():
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
service = build('drive', 'v3', credentials=creds)
results = service.files().list(pageSize=100, fields="nextPageToken, files(id)").execute()
items = results.get('files', [])
if not items:
print('No files found.')
else:
batch = service.new_batch_http_request(callback=callback)
for item in items:
batch.add(service.files().delete(fileId=item['id']))
batch.execute()
if __name__ == '__main__':
main()
</code>
<code>from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.http import BatchHttpRequest
SCOPES = ['https://www.googleapis.com/auth/drive']
def callback(request_id, response, exception):
if exception:
print(f"An error occurred: {exception}")
else:
print(f"Deleted file ID: {request_id}")
def main():
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
service = build('drive', 'v3', credentials=creds)
results = service.files().list(pageSize=100, fields="nextPageToken, files(id)").execute()
items = results.get('files', [])
if not items:
print('No files found.')
else:
batch = service.new_batch_http_request(callback=callback)
for item in items:
batch.add(service.files().delete(fileId=item['id']))
batch.execute()
if __name__ == '__main__':
main()
</code>
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.http import BatchHttpRequest
SCOPES = ['https://www.googleapis.com/auth/drive']
def callback(request_id, response, exception):
if exception:
print(f"An error occurred: {exception}")
else:
print(f"Deleted file ID: {request_id}")
def main():
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
service = build('drive', 'v3', credentials=creds)
results = service.files().list(pageSize=100, fields="nextPageToken, files(id)").execute()
items = results.get('files', [])
if not items:
print('No files found.')
else:
batch = service.new_batch_http_request(callback=callback)
for item in items:
batch.add(service.files().delete(fileId=item['id']))
batch.execute()
if __name__ == '__main__':
main()
New contributor
mr king is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.