Skip to content

Instantly share code, notes, and snippets.

@loucadufault
Last active January 7, 2022 23:03
Show Gist options
  • Save loucadufault/c74182c228efb499665a20e28e6ce4e8 to your computer and use it in GitHub Desktop.
Save loucadufault/c74182c228efb499665a20e28e6ce4e8 to your computer and use it in GitHub Desktop.
Recipe for adding per-user authentication for Google APIs to a Coda pack
import * as coda from "@codahq/packs-sdk";
export const pack = coda.newPack();
pack.setUserAuthentication({
type: coda.AuthenticationType.OAuth2,
authorizationUrl: "https://accounts.google.com/o/oauth2/v2/auth", // see https://developers.google.com/identity/protocols/oauth2/web-server#creatingclient
tokenUrl: "https://oauth2.googleapis.com/token", // see https://developers.google.com/identity/protocols/oauth2/web-server#exchange-authorization-code
scopes: [
// add any scopes your project requires to access user data from their Google Account
// these will generally correspond to the same scopes you have registered for your app in the Google Cloud Platform
// note that you must include the entire URL, as the path alone (e.g. "calendar.events" or "auth/calendar.events") is not sufficient
// e.g.
"https://www.googleapis.com/auth/calendar.events",
"https://www.googleapis.com/auth/calendar.calendarlist.readonly"
// after adding a scope and building a new version, further requests from your pack to the Google API will likely no longer be authorized
// you will need to connect your pack again to a Google Account from scratch. To do so, remove the pack from your test doc, add it again, and add a new account after clicking to sign in. From there, you will be redirected to a Google consent page where you can authorize the new permissions.
],
additionalParams: {
// these parameters ensure that you get a refresh token in your Google OAuth2 response, allowing the Pack to automatically refresh its access token.
// @see https://community.coda.io/t/simple-recipe-for-adding-per-user-authentication-for-google-apis-to-a-coda-pack/27547/2
access_type: "offline",
prompt: "consent",
},
});
// make sure you add the OAuth credentials to the pack, following this guide: https://coda.github.io/packs-sdk/guides/advanced/authentication/#oauth-20
@loucadufault
Copy link
Author

The raw version without any comments:

pack.setUserAuthentication({
  type: coda.AuthenticationType.OAuth2,
  authorizationUrl: "https://accounts.google.com/o/oauth2/v2/auth",
  tokenUrl: "https://oauth2.googleapis.com/token",
  scopes: [
  ],
  additionalParams: {
    access_type: "offline",
    prompt: "consent",
  },
});

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