Skip to content

Commit 3c70d52

Browse files
committed
Removed pprint and added try except
1 parent 843184a commit 3c70d52

1 file changed

Lines changed: 38 additions & 32 deletions

File tree

drive/driveapp/main.py

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"""
77

88
from __future__ import print_function
9-
import pprint
109
import six
1110
import httplib2
1211
from googleapiclient.discovery import build
@@ -29,34 +28,41 @@
2928
DESCRIPTION = 'A shiny new text document about hello world.'
3029

3130
# Perform OAuth2.0 authorization flow.
32-
flow = oauth2client.client.flow_from_clientsecrets(
33-
CLIENT_SECRETS, OAUTH2_SCOPE)
34-
flow.redirect_uri = oauth2client.client.OOB_CALLBACK_URN
35-
authorize_url = flow.step1_get_authorize_url()
36-
print('Go to the following link in your browser: ' + authorize_url)
37-
# `six` library supports Python2 and Python3 without redefining builtin input()
38-
code = six.moves.input('Enter verification code: ').strip()
39-
credentials = flow.step2_exchange(code)
40-
41-
# Create an authorized Drive API client.
42-
http = httplib2.Http()
43-
credentials.authorize(http)
44-
drive_service = build('drive', 'v2', http=http)
45-
46-
# Insert a file. Files are comprised of contents and metadata.
47-
# MediaFileUpload abstracts uploading file contents from a file on disk.
48-
media_body = googleapiclient.http.MediaFileUpload(
49-
FILENAME,
50-
mimetype=MIMETYPE,
51-
resumable=True
52-
)
53-
# The body contains the metadata for the file.
54-
body = {
55-
'title': TITLE,
56-
'description': DESCRIPTION,
57-
}
58-
59-
# Perform the request and print the result.
60-
new_file = drive_service.files().insert(
61-
body=body, media_body=media_body).execute()
62-
pprint.pprint(new_file)
31+
try:
32+
flow = oauth2client.client.flow_from_clientsecrets(
33+
CLIENT_SECRETS, OAUTH2_SCOPE)
34+
flow.redirect_uri = oauth2client.client.OOB_CALLBACK_URN
35+
authorize_url = flow.step1_get_authorize_url()
36+
print('Go to the following link in your browser: ' + authorize_url)
37+
# `six` library supports Python2 and Python3 without redefining builtin input()
38+
code = six.moves.input('Enter verification code: ').strip()
39+
credentials = flow.step2_exchange(code)
40+
41+
# Create an authorized Drive API client.
42+
http = httplib2.Http()
43+
credentials.authorize(http)
44+
drive_service = build('drive', 'v2', http=http)
45+
46+
# Insert a file. Files are comprised of contents and metadata.
47+
# MediaFileUpload abstracts uploading file contents from a file on disk.
48+
media_body = googleapiclient.http.MediaFileUpload(
49+
FILENAME,
50+
mimetype=MIMETYPE,
51+
resumable=True
52+
)
53+
# The body contains the metadata for the file.
54+
body = {
55+
'title': TITLE,
56+
'description': DESCRIPTION,
57+
}
58+
59+
# Perform the request and print the result.
60+
new_file = drive_service.files().insert(
61+
body=body, media_body=media_body).execute()
62+
file_title = new_file.get('title')
63+
file_desc = new_file.get('description')
64+
if file_title == TITLE and file_desc == DESCRIPTION:
65+
print(f"File is uploaded \nTitle : {file_title} \nDescription : {file_desc}")
66+
67+
except Exception as error:
68+
print('An error occurred: %s' % error)

0 commit comments

Comments
 (0)