Asana NodeJS API – Cannot authenticate request error when using client.useOauth

Tags: , ,



I’m currently setting up and integration with Asana and want to have my users connected through the oAuth side of the library. I have successfully got the auth flow working in the sense of having the user navigated to Asana, authorize the app, then storing the access token and refresh token for use down the line. I’m aware that the access tokens expire after 1 hour and thus when making calls to the api it’s best to use the ‘client.useOauth’ side of things to pass in the access & refresh tokens allows the library to refresh it as needs be without having to do it manually which is great. But with my code implementation below I am getting an error:

/**
 * Create a new asana client
 */
const client = Asana.Client.create({
    clientId: asanaCredentials.id,
    clientSecret: asanaCredentials.secret,
    redirectUri: asanaCredentials.redirect
});
/*
 * Get the access token and refresh token from the parameters passed in
 */
const { access_token, refresh_token } = data;
/*
 * Pass the client access & refresh tokens
 */
client.useOauth({
    access_token, refresh_token
});
/*
 * Get the users workspaces
 */
const workspaces = client.workspaces.getWorkspaces().then((result) => { return result.data; });
/*
 * Return the results back to the app
 */
return workspaces;

When this runs, I am getting the following error printed out:

Unhandled error Error: Cannot authenticate a request without first obtaining credentials
at
OauthAuthenticator.authenticateRequest (/workspace/node_modules/asana/lib/auth/oauth_authenticator.js:42)
at
doRequest (/workspace/node_modules/asana/lib/dispatcher.js:247)
at
(/workspace/node_modules/asana/lib/dispatcher.js:295)
at
Promise._execute (/workspace/node_modules/bluebird/js/release/debuggability.js:300)
at
Promise._resolveFromExecutor (/workspace/node_modules/bluebird/js/release/promise.js:481)
at
Promise (/workspace/node_modules/bluebird/js/release/promise.js:77)
at
Dispatcher.dispatch (/workspace/node_modules/asana/lib/dispatcher.js:244)
at
Dispatcher.get (/workspace/node_modules/asana/lib/dispatcher.js:321)
at
Function.Resource.getCollection (resource.js:36)
at
Workspaces.Resource.dispatchGetCollection (resource.js:77)
at
Workspaces.getWorkspaces (/workspace/node_modules/asana/lib/resources/gen/workspaces.js:73)
at
(/workspace/lib/src/integrations.js:132)
at
Generator.next ()
at
(/workspace/lib/src/integrations.js:8)
at
Promise ()
at
__awaiter (/workspace/lib/src/integrations.js:4)

Any help on what could be causing this would be greatly appreciated, thanks!

Answer

Fixed: My problem was the way in which I was passing in the two tokens, the client.useOauth needed to look like this:

client.useOauth({
    credentials: { access_token, refresh_token }
});


Source: stackoverflow