Connect to an OAuth2 API

Most APIs require that you enter some credentials to prove your identity. Usually this will be an API key, which is a unique value similar to a password. With API Connector for Google Sheets, these API keys are easy to use, as they can be simply entered as Header keys or URL query strings. However, many APIs provide access using OAuth 2.0, which is a more secure (and complex) method of authentication and authorization that works through a process of exchanging and refreshing tokens. To enable API access via OAuth 2.0, API Connector includes OAuth 2.0 integrations for several popular API platforms, or lets you add your own custom OAuth2 connection.

This feature is available only for business-plan subscribers, please upgrade to access.

Contents

Why Use OAuth 2.0

  • some APIs provide access only through OAuth 2.0
  • connect by clicking a button rather than digging through documentation for API keys
  • avoid needing to manually manage the process of exchanging & refreshing tokens
  • connect securely, without needing to enter (and potentially reveal) sensitive access credentials.

How to Connect

  1. To access the current list of integrated OAuth 2.0 platforms, click Add-ons > API Connector > Manage Connections, or open the Create screen and click the “Manage connections” link.
    oauth2-img1
  2. You will see a screen listing the available services.
    oauth2-img2
  3. Click on the platform you’re interested in connecting to, and you will see a screen on their site prompting you to connect through your account. Every service will look a little different but you may be prompted to accept the connection.
    oauth2-img3
  4. You’ll now see your new connection reflected in the list. This means you’ve now successfully connected via OAuth 2.0.
    oauth2-img4

How to Add a Custom OAuth2 API Connection

If API Connector does not provide a pre-built OAuth2 integration for your platform, you can add your own custom OAuth2 connection.

  1. If you haven’t already, enter the Manage Connections screen by clicking Add-ons > API Connector > Manage Connections, or by clicking the “Manage connections” link on the Create screen. Once there, scroll to the bottom and click Add Custom OAuth:
    oauth2-img7
  2. You will be presented with a screen containing input values for Name, Authorization Base URL, Token URL, Client ID, and Client Secret:
    oauth2-img8
  3. Choose a name for your custom connection and enter it in the Name field. The other values will be provided by your API platform. Typically you will need to begin by creating an “app”, after which the API will give you a Client ID and Client Secret.
  4. During this setup process, your API platform may request that you enter a callback URL. This is the callback URL: https://script.google.com/macros/d/12COOkin8nodCH7fZGIBu0D2jWY8-AEA0uvElt4Ph4wRbLUD4wslqQUfG/usercallback
  5. Every API will work a bit differently, but as an example of what to look out for, if you were connecting to the Google Analytics API, you’d first create an app and register it using the Google API Console. Google then provides the client ID and a client secret, and you’d read their documentation to find the following base and token URLs:
    • Authorization Base URL: https://accounts.google.com/o/oauth2/v2/auth?scope=https://www.googleapis.com/auth/analytics.readonly
    • Token URL: https://oauth2.googleapis.com/token
  6. After you’ve entered your custom OAuth2 values correctly, click Save. It will then appear on your list of Connections, where you can connect, disconnect, edit, and delete the connection.

How to Make an OAuth 2.0 API Request

Once you have your OAuth 2.0 connection in place, you can select it from the drop-down menu in the Create screen:
oauth2-img5

The rest of your request will be constructed as usual. Consult your API platform’s documentation for detailed instructions.
oauth2-img6

Notes

16 thoughts on “Connect to an OAuth2 API”

    • Hi Adam, thank you for the suggestion. IAP is something I really know nothing about. I’ve mostly been adding high-demand connections like Facebook and YouTube etc, so this one might make more sense as a custom OAuth2 connection that you add privately, but I will investigate.

      Reply
  1. Hey,

    I have a question about Connections. Is there a way to create a global connection for all the accounts editing the google spreadsheet? For example, we would like to create a Quickbook connection that can be used by all the editors to create requests. Thank you for your answer!

    Reply
    • Hi Erik! Thank you for your message. The OAuth2 connections are per user, since OAuth2 is an authentication framework based around allowing access only for the specific users that have authorized the application. If you would like multiple people to access the connection, you could either have each user authorize the connection separately, or you could create a shared gmail account for use with Sheets, or you could designate someone to set up / schedule requests and just give everyone else read access to the data. I hope that helps clarify, if not, please let me know.

      Reply
    • Hey David, I haven’t connected to the Google Ads API myself but I think you could do this by following Google’s documentation on how to get your OAuth2 details and connect to the API. It seems something is wrong with their servers right now because I keep getting a Service Unavailable error when I click on the newest Google Ads documentation, but once it’s back I’ll look into this and see if I can go through the process myself.

      Reply
  2. I’m very new to this and trying to connect to Etsy.

    I уntered the following data in the “Add Custom OAuth” form:

    Authorization Base URL:
    https://openapi.etsy.com/v2

    Token URL:
    https://openapi.etsy.com/v2/oauth/request_token?scope=email_r

    Client ID:
    azm3uoqeh5glvuaqa1jj9XXX

    Client Secret:
    xsargf6XXX

    Then a page opened in a new tab with the following error:
    “API request missing api_key or valid OAuth parameters”

    How this can be solved? Any help appreciated.

    Here is Etsy page about their OAuth:
    https://www.etsy.com/developers/documentation/getting_started/oauth

    Reply

Leave a Comment