|
6 | 6 | """ |
7 | 7 |
|
8 | 8 | from __future__ import print_function |
9 | | -import pprint |
10 | 9 | import six |
11 | 10 | import httplib2 |
12 | 11 | from googleapiclient.discovery import build |
|
29 | 28 | DESCRIPTION = 'A shiny new text document about hello world.' |
30 | 29 |
|
31 | 30 | # 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