Asana Node.js Help :)

api
accesstoken

#1

Using the below in Node trying to create a task within Workspace. I have omitted the AccessToken & WorkspaceID numbers. Suspect I’m doing something really dumb :slight_smile:

var asana = require('asana');
var client = asana.Client.create().useAccessToken('0/XXXXXXXX');
var newTask = { name: "Your Mission" }; client.tasks.createInWorkspace(1111111111, newTask).then(function(response) { tasks = response.data; console.log(tasks);
});

At runtime I get the following output…

Unhandled rejection Error: Invalid Request at InvalidRequest.AsanaError (/Users/D/Asana_Crons/node_modules/asana/lib/errors/error.js:4:11) at new InvalidRequest (/Users/D/Asana_Crons/node_modules/asana/lib/errors/invalid_request.js:5:14) at Request._callback (/Users/D/Asana_Crons/node_modules/asana/lib/dispatcher.js:152:23) at Request.self.callback (/Users/D/Asana_Crons/node_modules/request/request.js:186:22) at emitTwo (events.js:125:13) at Request.emit (events.js:213:7) at Request.<anonymous> (/Users/D/Asana_Crons/node_modules/request/request.js:1163:10) at emitOne (events.js:115:13) at Request.emit (events.js:210:7) at IncomingMessage.<anonymous> (/Users/D/Asana_Crons/node_modules/request/request.js:1085:12) at Object.onceWrapper (events.js:314:30) at emitNone (events.js:110:20) at IncomingMessage.emit (events.js:207:7) at endReadableNT (_stream_readable.js:1047:12) at _combinedTickCallback (internal/process/next_tick.js:102:11) at process._tickCallback (internal/process/next_tick.js:161:9)

#2

I’ve tried to reproduce your issue, but the code works for me (with one minor change). The response is already JSON. You don’t need to call .data on it:

var newTask = { name: "Your Mission" }; client.tasks.createInWorkspace(1111111111, newTask) .then(function(response) { console.log(response); });


#3

Hey Jeff thanks for the quick response. I have updated my code with yours and I’m still receiving rejection message. Anyway you could package up your code into repo, or bin where I can fetch it to run exact copy? appreciate the help :slight_smile:


#4

This is the exact code that works for me:

var asana = require('asana');

var my_access_token ="0/0XXXXXXXX"
var client = asana.Client.create().useAccessToken(my_access_token);

var newTask = { name: "Your Mission" }; 

client.tasks.createInWorkspace(123456789, newTask)
	.then(function(response) {
		console.log(response);
	})

#5
Unhandled rejection Error: Invalid Request
    at InvalidRequest.AsanaError (/Users/D/Asana_Crons/node_modules/asana/lib/errors/error.js:4:11)
    at new InvalidRequest (/Users/D/Asana_Crons/node_modules/asana/lib/errors/invalid_request.js:5:14)
    at Request._callback (/Users/D/Asana_Crons/node_modules/asana/lib/dispatcher.js:152:23)
    at Request.self.callback (/Users/D/Asana_Crons/node_modules/request/request.js:186:22)
    at emitTwo (events.js:125:13)
    at Request.emit (events.js:213:7)
    at Request.<anonymous> (/Users/D/Asana_Crons/node_modules/request/request.js:1163:10)
    at emitOne (events.js:115:13)
    at Request.emit (events.js:210:7)
    at IncomingMessage.<anonymous> (/Users/D/Asana_Crons/node_modules/request/request.js:1085:12)
    at Object.onceWrapper (events.js:314:30)
    at emitNone (events.js:110:20)
    at IncomingMessage.emit (events.js:207:7)
    at endReadableNT (_stream_readable.js:1047:12)
    at _combinedTickCallback (internal/process/next_tick.js:102:11)
    at process._tickCallback (internal/process/next_tick.js:161:9)

I copied your code updating the workspace and access tokens, and get the above output from cli.


#6

Hi @david3,

Could you try catching your exception and logging its value? Something like:

try {
  // Your current code, i.e.
  client.tasks.createInWorkspace(...)
  ...

} catch (e) {
  console.log(e.value);
} 

I believe that our client library will put the body of the response in that value property, and if we can see that we can see what our servers think is wrong, whether it’s having trouble parsing the JSON or whatever. Thanks!


#7

Here is a running Glitch of @Jeff_Schneider’s code (with the addition of a try ... catch, which is just :ok_hand: practice): glitch.com/edit/#!/asana-post-task

If you add your personal access token and workspace ID to .env there, it should work just fine. You can see the response from Asana by clicking the Logs button over in the upper left.

Though, eyeballing your code, it looks like it should work just fine.

I’m sure you have, but still I’d double or triple check your Workspace ID and access token.

  1. Assuming this is a Workspace you have membership for in Asana, you can find the ID by going here https://app.asana.com/api/1.0/users/me and taking a peak through the array under the workspaces key.
  2. Consider generating a new access token and trying that.

Let me know if that helps! :pray:


#8

I made a copy of your Glitch code. Only change made was adding in the WORKSPACE ID and TOKEN as Const at top of file, my real access token is active and checked Workspace ID again. Here is a copy of what I have in Atom (I have shown how I pass the access token & workspace ID, just of cause with false data )

const PERSONAL_ACCESS_TOKEN = "0/4324234234324343244234234324"
const WORKSPACE_ID = 24324324234234234234

const asana = require('asana');

const client = asana.Client.create().useAccessToken(process.env.PERSONAL_ACCESS_TOKEN);

const newTask = { name: "Your Mission" };

try {
client.tasks.createInWorkspace(process.env.WORKSPACE_ID, newTask)
  .then(response => console.log(response))
} catch (error) {
  console.log(error.value)
}

On Runtime. My Error is now as follows…

$D node daniel.js
Unhandled rejection Error: Cannot authenticate a request without first obtaining credentials
    at OauthAuthenticator.authenticateRequest (/Users/D/Asana_Crons/node_modules/asana/lib/auth/oauth_authenticator.js:42:11)
    at doRequest (/Users/D/Asana_Crons/node_modules/asana/lib/dispatcher.js:144:26)
    at /Users/D/Asana_Crons/node_modules/asana/lib/dispatcher.js:182:5
    at tryCatcher (/Users/D/Asana_Crons/node_modules/bluebird/js/main/util.js:26:23)
    at Promise._resolveFromResolver (/Users/D/Asana_Crons/node_modules/bluebird/js/main/promise.js:483:31)
    at new Promise (/Users/D/Asana_Crons/node_modules/bluebird/js/main/promise.js:71:37)
    at Dispatcher.dispatch (/Users/D/Asana_Crons/node_modules/asana/lib/dispatcher.js:141:10)
    at Dispatcher.post (/Users/D/Asana_Crons/node_modules/asana/lib/dispatcher.js:222:15)
    at Tasks.Resource.dispatchPost (/Users/D/Asana_Crons/node_modules/asana/lib/resources/resource.js:90:42)
    at Tasks.createInWorkspace (/Users/D/Asana_Crons/node_modules/asana/lib/resources/gen/tasks.js:73:15)
    at Object.<anonymous> (/Users/D/Asana_Crons/daniel.js:11:14)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)

Appreciate anymore help you guys can give :slight_smile:


#9

This may be just an artifact from mocking the code for your post here, but it looks like you might be binding your access token and to const PERSONAL_ACCESS_TOKEN, but still referencing it as process.env.PERSONAL_ACCESS_TOKEN. Same thing with your WORKSPACE_ID


#10
const PERSONAL_ACCESS_TOKEN = '0/4324324324234324'
const WORKSPACE_ID = 324234234234234234

const asana = require('asana');

const client = asana.Client.create().useAccessToken(PERSONAL_ACCESS_TOKEN);

const newTask = { name: "Your Mission" };

try {
client.tasks.createInWorkspace(WORKSPACE_ID, newTask)
  .then(response => console.log(response))
} catch (error) {
  console.log(error.value)
}

Still gives me this at runtime…

Unhandled rejection Error: Invalid Request

:disappointed_relieved::disappointed_relieved::disappointed_relieved: As ever appreciate the help & support !


#11

Oh no. :frowning: Sorry for all this trouble.

One more thing to try, let’s wrap the creation of the client in a try ... catch:

try {
   const client = asana.Client.create().useAccessToken(PERSONAL_ACCESS_TOKEN);
} catch (error) {
   console.log(error.value);
}

(You could also just expand the try ... catch you already have to include the client creation in try)

You should be handling the call to Asana with the other try ... catch, so this is the only thing I could imagine not being handled here.

Beyond what that error message might hold, would you mind DMing me the workspace ID you’re using? Maybe I can poke around at what’s happening on our end.


#12

Thanks Daniel. Good news I have it all working fine now, and once again enjoying everything life has to offer.

I wish the resolution was a tricky complex problem. However it would seem only me and the Asana God’s understand what I was attempting to do.

I realised from going back into docs & API explorer that I had taken a project ID as being the Workspace ID. I failed to notice that the difference alongside the need to specify workspace & pass the project in the data sent with the newTask object.

I wish the error handling could of returned “Read the docs fully dumbass” :joy:. My mistake sorry for the loss of time for all involved in solving my ‘NodeJS Day1’ Error :joy::joy::joy:


#13

No worries, @david3! That’s the sort of thing that it always ends up being. ¯_(ツ)_/¯

Just happy you were able to get things working. Let us know if anything else comes up. :+1: