The authorization URL and the token URL required to configure OAuth2 authentication were somewhat difficult to find in Google’s docs (found here استخدام OAuth 2.0 لتطبيقات خادم الويب | Authorization | Google for Developers ), so I thought I would post a template for authenticating Coda packs with Google APIs.
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
Here is the relevant code 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",
},
});