Skip to main content

 

Hey guys,

I’m having issues with refreshing my access token with OAuth2. Seemingly the ‘autoRefresh’ feature isn’t working as I never see the log in ‘refreshAccessToken’.

To replicate the issue we make a API call after the access token is expired from a Zapier trigger. The reason we make API calls after receiving the webhooks is to enrich the data with human readable results. This will work within the access token expiry period, but fails after it needs to be refreshed. All API calls after the expiry seem to use the old access token resulting in 401 errors from our API. 

Am I missing something? What do I need to do to trigger the access token to be refreshed and know its replaced the old token?   

 

Cut down version of authentication config which have been based off this example:

https://github.com/zapier/zapier-platform/blob/main/example-apps/oauth2/authentication.js

export default {
config: {
type: 'oauth2',
oauth2Config: {
authorizeUrl: getAuthorizeUrl,
getAccessToken = async (z: ZObject, bundle: Bundle) => {
const response = await z.request({
url: ...
});

return {
access_token: response.data.access_token,
refresh_token: response.data.refresh_token
};
},
refreshAccessToken = async (z: ZObject, bundle: Bundle) => {
z.console.log('Token refreshing');
const response = await z.request({
url: ...
});

return {
access_token: response.data.id_token,
};
},
autoRefresh: true,
},
fields:
{
key: 'auth_region',
label: 'Select your region',
type: 'string',
required: true,
choices: getRegionChoices(),
},
],
test,
connectionLabel: '{{name}}',
},
befores: includeBearerToken],
afters: rhandleHTTPError],
};

const includeBearerToken = (request, z, bundle) => {
if (bundle.authData && bundle.authData.access_token) {
request.headers.Authorization = `Bearer ${bundle.authData.access_token}`;
}
return request;
};

const handleHTTPError = (response, z: ZObject, bundle: Bundle) => {
if (response.status >= 400) {
const message = `Unexpected status code ${
response.status
}: request url: ${response.request?.url ?? ''}`;
z.console.log(message);
throw new Error(message);
}
return response;
};