JavaScript Quickstart

Complete the steps described in the rest of this page, and in about five minutes you'll have a simple JavaScript application that makes requests to the Drive API.


To run this quickstart, you'll need:

  • Python 2.4 or greater (to provide a web server).
  • Access to the internet and a web browser.
  • A Google account with Google Drive enabled.

Step 1: Turn on the Drive API

  1. Use this wizard to create or select a project in the Google Developers Console and automatically turn on the API. Click Continue, then Go to credentials.
  2. On the Add credentials to your project page, click the Cancel button.
  3. At the top of the page, select the OAuth consent screen tab. Select an Email address, enter a Product name if not already set, and click the Save button.
  4. Select the Credentials tab, click the Create credentials button and select OAuth client ID.
  5. Select the application type Web application.
  6. In the Authorized JavaScript origins field, enter the URL http://localhost:8000. You can leave the Authorized redirect URIs field blank.
  7. Click the Create button.
  8. Take note of the client ID in the resulting dialog. You will need it in a later step.
  9. Click OK to dismiss the resulting dialog.

Step 2: Set up the sample

Create a file named quickstart.html and copy in the following code:

    <script type="text/javascript">
      // Your Client ID can be retrieved from your project in the Google
      // Developer Console,
      var CLIENT_ID = '<YOUR_CLIENT_ID>';

      var SCOPES = [''];

       * Check if current user has authorized this application.
      function checkAuth() {
            'client_id': CLIENT_ID,
            'scope': SCOPES.join(' '),
            'immediate': true
          }, handleAuthResult);

       * Handle response from authorization server.
       * @param {Object} authResult Authorization result.
      function handleAuthResult(authResult) {
        var authorizeDiv = document.getElementById('authorize-div');
        if (authResult && !authResult.error) {
          // Hide auth UI, then load client library.
 = 'none';
        } else {
          // Show auth UI, allowing the user to initiate authorization by
          // clicking authorize button.
 = 'inline';

       * Initiate auth flow in response to user clicking authorize button.
       * @param {Event} event Button click event.
      function handleAuthClick(event) {
          {client_id: CLIENT_ID, scope: SCOPES, immediate: false},
        return false;

       * Load Drive API client library.
      function loadDriveApi() {
        gapi.client.load('drive', 'v3', listFiles);

       * Print files.
      function listFiles() {
        var request ={
            'pageSize': 10,
            'fields': "nextPageToken, files(id, name)"

          request.execute(function(resp) {
            var files = resp.files;
            if (files && files.length > 0) {
              for (var i = 0; i < files.length; i++) {
                var file = files[i];
                appendPre( + ' (' + + ')');
            } else {
              appendPre('No files found.');

       * Append a pre element to the body containing the given message
       * as its text node.
       * @param {string} message Text to be placed in pre element.
      function appendPre(message) {
        var pre = document.getElementById('output');
        var textContent = document.createTextNode(message + '\n');

    <script src="">
    <div id="authorize-div" style="display: none">
      <span>Authorize access to Drive API</span>
      <!--Button for the user to click to initiate auth sequence -->
      <button id="authorize-button" onclick="handleAuthClick(event)">
    <pre id="output"></pre>

Replace the placeholder <YOUR_CLIENT_ID> in the copied code with the client ID you created in Step 1.

Step 3: Run the sample

  1. Start the web server using the following command from your working directory:

Python 2.x

python -m SimpleHTTPServer 8000

Python 3.x

python -m http.server 8000

  1. Load the URL http://localhost:8000/quickstart.html into your browser.

The first time you run the sample, it will prompt you to authorize access:

  1. Click the Authorize button to open the authorization window.

    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.

  2. Click the Accept button.


  • After the initial user authorization, calls to gapi.auth.authorize that use immediate:true mode will obtain an auth token without user interaction.

Further reading


Error: origin_mismatch

This error will occur during the authorization flow if the host and port used to serve the web page doesn't match an allowed JavaScript origin on your Google Developers Console project. Make sure you completed the Step 1.e and that the URL in your browser matches.

Send feedback about...