16 """Shows basic usage of the Drive v3 API.
17 Prints the names and ids of the first 10 files the user has access to.
23 if os.path.exists(
'token.json'):
24 creds = Credentials.from_authorized_user_file(
'token.json', SCOPES)
26 if not creds
or not creds.valid:
27 if creds
and creds.expired
and creds.refresh_token:
28 creds.refresh(Request())
30 flow = InstalledAppFlow.from_client_secrets_file(
31 'client_secret.json', SCOPES)
32 creds = flow.run_local_server(port=0)
34 with open(
'token.json',
'w')
as token:
35 token.write(creds.to_json())
38 service = build(
'drive',
'v3', credentials=creds)
41 results = service.files().list(
42 pageSize=10, fields=
"nextPageToken, files(id, name)").execute()
43 items = results.get(
'files', [])
46 print(
'No files found.')
50 print(
u'{0} ({1})'.format(item[
'name'], item[
'id']))
51 except HttpError
as error:
53 print(f
'An error occurred: {error}')