1
- import os .path
1
+ import os .path
2
2
3
- from google .auth .transport .requests import Request
4
- from google .oauth2 .credentials import Credentials
5
- from google_auth_oauthlib .flow import InstalledAppFlow
6
- from googleapiclient .discovery import build
7
- from googleapiclient .errors import HttpError
3
+ from google .auth .transport .requests import Request
4
+ from google .oauth2 .credentials import Credentials
5
+ from google_auth_oauthlib .flow import InstalledAppFlow
6
+ from googleapiclient .discovery import build
7
+ from googleapiclient .errors import HttpError
8
8
from pprint import pprint
9
9
10
10
# If modifying these scopes, delete the file token.json.
11
- SCOPES = ["https://www.googleapis.com/auth/drive.metadata.readonly" , "https://www.googleapis.com/auth/drive" ]
11
+ SCOPES = [
12
+ "https://www.googleapis.com/auth/drive.metadata.readonly" ,
13
+ "https://www.googleapis.com/auth/drive" ,
14
+ ]
15
+
16
+
17
+ def get_creds ():
18
+ creds = None
19
+ # The file token.json stores the user's access and refresh tokens, and is
20
+ # created automatically when the authorization flow completes for the first
21
+ # time.
22
+ if os .path .exists ("token.json" ):
23
+ creds = Credentials .from_authorized_user_file ("token.json" , SCOPES )
24
+ # If there are no (valid) credentials available, let the user log in.
25
+ if not creds or not creds .valid :
26
+ if creds and creds .expired and creds .refresh_token :
27
+ creds .refresh (Request ())
28
+ else :
29
+ flow = InstalledAppFlow .from_client_secrets_file ("credentials.json" , SCOPES )
30
+ creds = flow .run_local_server (port = 0 )
31
+ # Save the credentials for the next run
32
+ with open ("token.json" , "w" ) as token :
33
+ token .write (creds .to_json ())
34
+ return creds
12
35
13
- def get_creds ():
14
- creds = None
15
- # The file token.json stores the user's access and refresh tokens, and is
16
- # created automatically when the authorization flow completes for the first
17
- # time.
18
- if os .path .exists ("token.json" ):
19
- creds = Credentials .from_authorized_user_file ("token.json" , SCOPES )
20
- # If there are no (valid) credentials available, let the user log in.
21
- if not creds or not creds .valid :
22
- if creds and creds .expired and creds .refresh_token :
23
- creds .refresh (Request ())
24
- else :
25
- flow = InstalledAppFlow .from_client_secrets_file (
26
- "credentials.json" , SCOPES
27
- )
28
- creds = flow .run_local_server (port = 0 )
29
- # Save the credentials for the next run
30
- with open ("token.json" , "w" ) as token :
31
- token .write (creds .to_json ())
32
- return creds
33
36
34
37
def get_service (creds ):
35
- service = build ("drive" , "v3" , credentials = creds )
36
- return service
38
+ service = build ("drive" , "v3" , credentials = creds )
39
+ return service
37
40
38
- def main ():
39
- """Shows basic usage of the Drive v3 API.
40
- Prints the names and ids of the first 10 files the user has access to.
41
- """
42
- creds = get_creds ()
43
41
42
+ def main ():
43
+ """Shows basic usage of the Drive v3 API.
44
+ Prints the names and ids of the first 10 files the user has access to.
45
+ """
46
+ creds = get_creds ()
44
47
45
- try :
46
- service = get_service (creds )
48
+ try :
49
+ service = get_service (creds )
47
50
48
- # Call the Drive v3 API
49
- results = (
50
- service .files ()
51
- .list (pageSize = 10 , fields = "nextPageToken, files(id, name)" )
52
- .execute ()
53
- )
54
- items = results .get ("files" , [])
51
+ # Call the Drive v3 API
52
+ results = (
53
+ service .files ()
54
+ .list (pageSize = 10 , fields = "nextPageToken, files(id, name)" )
55
+ .execute ()
56
+ )
57
+ items = results .get ("files" , [])
55
58
56
- if not items :
57
- print ("No files found." )
58
- return
59
- print ("Files:" )
60
- for item in items :
61
- pprint (item )
62
- print (f"{ item ['name' ]} ({ item ['id' ]} )" )
63
- except HttpError as error :
64
- # TODO(developer) - Handle errors from drive API.
65
- print (f"An error occurred: { error } " )
59
+ if not items :
60
+ print ("No files found." )
61
+ return
62
+ print ("Files:" )
63
+ for item in items :
64
+ pprint (item )
65
+ print (f"{ item ['name' ]} ({ item ['id' ]} )" )
66
+ except HttpError as error :
67
+ # TODO(developer) - Handle errors from drive API.
68
+ print (f"An error occurred: { error } " )
66
69
67
70
68
- if __name__ == "__main__" :
69
- main ()
71
+ if __name__ == "__main__" :
72
+ main ()
0 commit comments