Skip to content

Commit a63cd6c

Browse files
committed
Initial import.
0 parents  commit a63cd6c

File tree

5 files changed

+134
-0
lines changed

5 files changed

+134
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ve

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
Quickstart: Run a Drive App in Python
2+
==
3+
4+
Complete the steps described in the rest of this page, and in about five minutes
5+
you'll have a simple Drive app that uploads a file to Google Drive.
6+
7+
To run a quickstart example you'll need:
8+
9+
* Access to the internet and a web browser, in order to authorize the sample app.
10+
* A Google account with Drive enabled.
11+
* An environment to run programs in your selected language.
12+
13+
Step 1: Get the sample
14+
--
15+
16+
Clone the `git` repository at `https://github.com/googledrive/quickstart-python`.
17+
18+
git clone https://github.com/googledrive/quickstart-python
19+
20+
Step 2: Install the Google Client Library
21+
--
22+
23+
To install the Google API Python Client on a system, you should use the `pip` command.
24+
25+
pip install --upgrade google-api-python-client
26+
27+
Alternatively, if you are using `virtualenv`, create the environment and install the client library.
28+
29+
virtualenv ve
30+
./ve/bin/pip install --upgrade google-api-python-client
31+
32+
If you need to access the Google API Python Client from a Google App Engine
33+
project, you can follow the instructions
34+
[here](https://developers.google.com/api-client-library/python/platforms/google_app_engine).
35+
36+
37+
Step 3: Run the sample
38+
--
39+
40+
After you have set up your Google API project, installed the Google API client
41+
library, and set up the sample source code, the sample is ready to run.
42+
43+
python main.py
44+
45+
Or if using `virtualenv`.
46+
47+
./ve/bin/python main.py
48+
49+
When you run the sample from command-line, it provides a link you'll need to
50+
visit in order to authorize.
51+
52+
1. Browse to the provided URL in your web browser.
53+
2. If you are not already logged into your Google account, you will be prompted
54+
to log in. If you are logged into multiple Google accounts, you will be
55+
asked to select one account to use for the authorization.
56+
3. Copy the code you're given after browsing to the link, and paste it into the
57+
prompt `Enter authorization code:`. Click **Enter**.
58+
59+
Note: The authorization flow in this example is greatly simplified for
60+
demonstration purposes and should not be used in web applications. For more
61+
information, see [Authorizing your App with Google Drive](http://developers.google.com/drive/about-auth).
62+
63+
When you finish these steps,
64+
the sample prints information about the Google Drive file to the screen.
65+
The file `document.txt` is accessible in Google Drive, and is titled `My
66+
New Text Document`.
67+
68+
By editing the sample code to provide paths to new files and new titles,
69+
you can run a few more simple upload tests. When you're ready, you
70+
could try running some other Drive API methods such as
71+
[files.list](http://developers.google.com/drive/v2/reference/files/list).

client_secrets.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"installed":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","client_secret":"ckGVrYYQ_GE7O4rL80ozlEXR","token_uri":"https://accounts.google.com/o/oauth2/token","client_email":"","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","oob"],"client_x509_cert_url":"","client_id":"665081966568.apps.googleusercontent.com","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs"}}

document.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This file will be uploaded to Google Drive as plain text.
2+
3+
Hello world :)

main.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/python
2+
3+
"""Google Drive Quickstart in Python.
4+
5+
This script uploads a single file to Google Drive.
6+
"""
7+
8+
import pprint
9+
10+
import httplib2
11+
import apiclient.discovery
12+
import apiclient.http
13+
import oauth2client.client
14+
15+
# OAuth 2.0 scope that will be authorized.
16+
# Check https://developers.google.com/drive/scopes for all available scopes.
17+
OAUTH2_SCOPE = 'https://www.googleapis.com/auth/drive'
18+
19+
# Location of the client secrets.
20+
CLIENT_SECRETS = 'client_secrets.json'
21+
22+
# Path to the file to upload.
23+
FILENAME = 'document.txt'
24+
25+
# Metadata about the file.
26+
MIMETYPE = 'text/plain'
27+
TITLE = 'My New Text Document'
28+
DESCRIPTION = 'A shiny new text document about hello world.'
29+
30+
# Perform OAuth2.0 authorization flow.
31+
flow = oauth2client.client.flow_from_clientsecrets(CLIENT_SECRETS, OAUTH2_SCOPE)
32+
flow.redirect_uri = oauth2client.client.OOB_CALLBACK_URN
33+
authorize_url = flow.step1_get_authorize_url()
34+
print 'Go to the following link in your browser: ' + authorize_url
35+
code = raw_input('Enter verification code: ').strip()
36+
credentials = flow.step2_exchange(code)
37+
38+
# Create an authorized Drive API client.
39+
http = httplib2.Http()
40+
credentials.authorize(http)
41+
drive_service = apiclient.discovery.build('drive', 'v2', http=http)
42+
43+
# Insert a file. Files are comprised of contents and metadata.
44+
# MediaFileUpload abstracts uploading file contents from a file on disk.
45+
media_body = apiclient.http.MediaFileUpload(
46+
FILENAME,
47+
mimetype=MIMETYPE,
48+
resumable=True
49+
)
50+
# The body contains the metadata for the file.
51+
body = {
52+
'title': TITLE,
53+
'description': DESCRIPTION,
54+
}
55+
56+
# Perform the request and print the result.
57+
new_file = drive_service.files().insert(body=body, media_body=media_body).execute()
58+
pprint.pprint(new_file)

0 commit comments

Comments
 (0)