Coda Pack not performing an OAuth token refresh

I have a pack that uses Google OAuth but the OAuth token expires very quickly (like 15 to 30 mins). I thought Coda would automatically request a refresh token if there was a 401 returned but that’s not happening and instead the pack fails to refresh the table with an error once the token expires and I have to resign/auth to Google to refresh the table.

image

The Auth setup is:

pack.addNetworkDomain("googleapis.com");

// OAuth2 Authentication
pack.setUserAuthentication({
  type: coda.AuthenticationType.OAuth2,
  authorizationUrl: "https://accounts.google.com/o/oauth2/auth",
  tokenUrl: "https://oauth2.googleapis.com/token",
  scopes: ["https://www.googleapis.com/auth/spreadsheets"],
});

The error being returned is a 401

According to the Coda refresh docs I thought that would trigger a automatic refresh behind the screnes but it’s not. So I added a try/catch that is suggested in the docs but nothing.

What am I doing wrong here? Thanks in advance!

Hi Ryan - There are a few common causes for this:

  1. Your OAuth flow didn’t return a refresh_token. Every API has different rules as to when these are returned or not. For Google OAuth it requires specifying the access_type and prompt query parameters as shown in this code sample: API setup samples - Coda Pack SDK.

  2. Your code is swallowing the 401 error. We only attempt a refresh of the access token when the Pack throws a 401 StatusCodeError. If you have a try/catch that is handling these in some other way (logging them, throwing a different error type, etc) then the refresh won’t be attempted. You can read more about that here: Authenticating using OAuth - Coda Pack SDK

Check to see if either of those could be affecting your Pack, and let me know what you find.

Looking at your post more carefully, it seems like cause #1 is the issue here. Try adding those extra settings.

Thanks @Eric_Koleda. I’m try/catching and rethrowing the 401 and here’s the code where the 401 is thrown and a refresh isn’t happening.

  getSchema: async function (context, params) {
    ...
    let response = null;
    try {
      response = await context.fetcher.fetch({
        method: "GET",
        url: url,
      });
    } catch(error) {
       throw error
    }
    ...
  }

So believe i’m not swallowing it.

I actually do have the additional params for Google (accidentally deleted it when streamlining the code in the post). I originally too the code directly from the link you posted.

// OAuth2 Authentication
pack.setUserAuthentication({
  type: coda.AuthenticationType.OAuth2,
  authorizationUrl: "https://accounts.google.com/o/oauth2/auth",
  tokenUrl: "https://oauth2.googleapis.com/token",
  scopes: ["https://www.googleapis.com/auth/spreadsheets"],


  additionalParams: {
    token_access_type: "offline",
    prompt: "consent",
  },


});

Any insight to what to try next?

Hmm, those are the usual suspects. If you share the doc with support and then send me the link I can take a look at the logs and see if anything jumps out.

Thanks @Eric_Koleda. I’ve shared a new Coda doc using the pack for testing/debug purposes with support.

Happy to share the pack as well but looks like I would need an email as I don’t see a share with support option.

Thanks for sharing. The internal error states that it couldn’t find a refresh_token, and looking at the responses from the OAuth flow there indeed is none being returned. Looking at your code more closely, I think this may be the issue:

token_access_type: "offline",

As per the sample code I pointed to, it should be:

access_type: "offline",

Try changing that and see if it starts working.

1 Like

I don’t know how I missed that. Greatly appreciate the help and apologies for taking up your time.

No worries, that’s what I’m here for!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.