Skip to content

Instantly share code, notes, and snippets.

@dotherightthing
Last active October 1, 2019 12:52
Show Gist options
  • Save dotherightthing/1d6155c2d72df6a0a361c8764bf080a1 to your computer and use it in GitHub Desktop.
Save dotherightthing/1d6155c2d72df6a0a361c8764bf080a1 to your computer and use it in GitHub Desktop.
Using Postman to test Google Docs API

Using Postman to test Google Docs API

1. Get credentials from Google

  1. Using OAuth 2.0 to Access Google APIs > Basic steps

Visit the Google API Console to obtain OAuth 2.0 credentials such as a client ID and client secret that are known to both Google and your application.

  1. Credentials > Create credentials > OAuth client ID > Other
  2. Close the window containing your Client ID and Client Secret
  3. In the Credentials list, click on the name of the new OAuth 2.0 client ID
  4. Click DOWNLOAD JSON
  5. Open in VS Code, format document
{
  "installed": {
    "client_id": "xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
    "project_id": "dtrt-cv-xxxxxxxxxxxxx",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_secret": "xxxxxxxxxxxxxxxxxxxxxxxx",
    "redirect_uris": ["urn:ietf:wg:oauth:2.0:oob", "http://localhost"]
  }
}

2. Get the Scope

https://www.googleapis.com/auth/documents

3. Request a new token in Postman

  1. Open Postman.app
  2. Create a new tab
  3. Authorization
    1. OAuth 2.0
    2. Get New Access Token
  4. Get New Access Token window opens
    1. Token Name: Whatever
    2. Grant Type: Authorization Code
    3. Callback URL: installed.redirect_uris.1
    4. Auth URL: installed.auth_uri
    5. Access Token URL: installed.token_uri
    6. Client ID: installed.client_id
    7. Client Secret: installed.client_secret
    8. Scope: scope
    9. State: [blank]
    10. Client Authentication: Send as Basic Auth header
  5. Click Request Token
  6. Google will show the Sign In modal
    1. Enter your Google email address
    2. Enter your Google password
    3. If login was successful, Postman will show a window containing the response, and populate the Access Token field.

4. Create an empty Google Doc

Method: documents.create

Creates a blank document using the title given in the request. Other fields in the request, including any provided content, are ignored.

POST: https://docs.googleapis.com/v1/documents

Body:

{
  "title": "Google Docs API Test 1"
}

Send.

Returns a response object contain various things including the documentId

5. Populate the Google Doc

Method: documents.batchUpdate

POST: https://docs.googleapis.com/v1/documents/documentId:batchUpdate

Body:

{
  "requests": [
    {
      "insertText": {
        "text": "Hello world",
        "location": {
          "index": 1
        }
      }
    }
  ]
}

Send.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment