Skip to content

Commit d4fa754

Browse files
committed
Merge @aliafshar's Drive App sample to a separate folder. It still works.
Change-Id: I6ac1d0f7e448711199a3dfbf57d882765c750669
2 parents 83c07d6 + 728883f commit d4fa754

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

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

drive/driveapp/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"}}

drive/driveapp/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 :)

drive/driveapp/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)