Complete the steps described in the rest of this page, and in less than five minutes you'll have a simple Drive app that uploads a file to Google Drive.
First, you need to enable the Drive API for your app. You can do this in your app's API project in the Google APIs Console.
- Create an API project in the Google APIs Console.
- Select the Services tab in your API project, and enable the Drive API.
- Select the API Access tab in your API project, and click Create an OAuth 2.0 client ID.
- In the Branding Information section, provide a name for your application (e.g. "Drive Quickstart Sample"), and click Next. Providing a product logo is optional.
- In the Client ID Settings section, do the following:
- Select Installed application for the Application type (or Web application for the JavaScript sample).
- Select Other for the Installed application type.
- Click Create Client ID.
- In the API Access page, locate the section Client ID for installed applications and note or copy the two pieces of information you'll need later to run the sample: the Client ID and the Client Secret
To run the quickstart sample, you'll need to install the Google API client library.
bundle install
You'll need to create two files: a test file to upload to Drive (in this case
document.txt), and the given sample code, modified to include the unique
Client ID and Client Secret you created in the "Enable the Drive API"
step.
- Create a text file named
document.txt, containing the textHello world!. - After following the directions below to copy source code, replace
YOUR_CLIENT_IDwith the Client ID you generated in the "Enable the Drive API" step. - Replace
YOUR_CLIENT_SECRETwith your Client Secret, also generated previously.
After you have set up your Google API project, installed the Google API client library, and set up the sample source code, the sample is ready to run. The command-line samples provide a link you'll need to visit in order to authorize the sample.
bundle exec ruby quickstart.rb
- Browse to the provided URL in your web browser.
- If you are not already logged into your Google account, you will be prompted to log in. If you are logged into multiple Google accounts, you will be asked to select one account to use for the authorization.
- Copy the code you're given after browsing to the link, and paste it into the
prompt
Enter authorization code:. Click Enter.
When you finish these steps, document.txt is now stored in Google Drive.
The command-line samples print Information about the Google Drive file to the screen.
The file document.txt is accessible in Google Drive, and is titled "My
Document".
By editing the sample code to provide paths to new files and new titles, you can run a few more simple upload tests. When you're ready, you could try running some other Drive API methods such as files.list.
If your goal is to let users create and open files directly from the Drive UI using your app, see Integrate with the Drive UI. Our end-to-end Example Apps demonstrate a simple Drive UI-integrated web app.
If your goal is to expand the quickstart sample into something for your own installed application, consult the API Reference. The API Reference discusses all of the features of the Drive API, and gives samples in each language on how to use a feature.
All requests to the Drive API must be authorized by an authenticated user. To examine more authorization code and learn how to authorize requests, see Retrieve and Use Credentials.