Hi all,
What is the way, in nodejs sdk v3, to use multiples token in the same code ? Because Asana.TasksApi() for exemple, does not ask the client.
Thanks
@Thomas_Buisson - welcome to the forum! I’m not 100% sure on this, so hopefully someone else can confirm or correct this, but I think you can create multiple API instances in the same code and just pass them different authentications (see here for that documentation).
The example code in their readme just has you instantiate an ApiClient, set the access token, and then instantiate a specific endpoint with that client, so I think you would create separate clients for each authentication you want and then pass the correct client instance as an argument in your instantiation of each endpoint.
so I think you would create separate clients for each authentication you want
I would agree; I think that’s how I would approach it.
Hi, thanks for your reply. But in the doc, it is not possible to pass a client instance as an argument of an instantiation of an endpoint :
const Asana = require('asana');
let client = Asana.ApiClient.instance;
let token = client.authentications['token'];
token.accessToken = '<YOUR_ACCESS_TOKEN>';
let tasksApiInstance = new Asana.TasksApi();
let body = {"data": {"<PARAM_1>": "<VALUE_1>", "<PARAM_2>": "<VALUE_2>",}}; // Object | The task to create.
let opts = {
'opt_fields': "actual_time_minutes,approval_status,assignee,assignee.name,assignee_section,assignee_section.name,assignee_status,completed,completed_at,completed_by,completed_by.name,created_at,created_by,custom_fields,custom_fields.asana_created_field,custom_fields.created_by,custom_fields.created_by.name,custom_fields.currency_code,custom_fields.custom_label,custom_fields.custom_label_position,custom_fields.date_value,custom_fields.date_value.date,custom_fields.date_value.date_time,custom_fields.description,custom_fields.display_value,custom_fields.enabled,custom_fields.enum_options,custom_fields.enum_options.color,custom_fields.enum_options.enabled,custom_fields.enum_options.name,custom_fields.enum_value,custom_fields.enum_value.color,custom_fields.enum_value.enabled,custom_fields.enum_value.name,custom_fields.format,custom_fields.has_notifications_enabled,custom_fields.id_prefix,custom_fields.is_formula_field,custom_fields.is_global_to_workspace,custom_fields.is_value_read_only,custom_fields.multi_enum_values,custom_fields.multi_enum_values.color,custom_fields.multi_enum_values.enabled,custom_fields.multi_enum_values.name,custom_fields.name,custom_fields.number_value,custom_fields.people_value,custom_fields.people_value.name,custom_fields.precision,custom_fields.representation_type,custom_fields.resource_subtype,custom_fields.text_value,custom_fields.type,dependencies,dependents,due_at,due_on,external,external.data,followers,followers.name,hearted,hearts,hearts.user,hearts.user.name,html_notes,is_rendered_as_separator,liked,likes,likes.user,likes.user.name,memberships,memberships.project,memberships.project.name,memberships.section,memberships.section.name,modified_at,name,notes,num_hearts,num_likes,num_subtasks,parent,parent.created_by,parent.name,parent.resource_subtype,permalink_url,projects,projects.name,resource_subtype,start_at,start_on,tags,tags.name,workspace,workspace.name"
};
tasksApiInstance.createTask(body, opts).then((result) => {
console.log('API called successfully. Returned data: ' + JSON.stringify(result.data, null, 2));
}, (error) => {
console.error(error.response.body);
});
@Thomas_Buisson - from the documentation (lines 23 and on) linked in my earlier post:
export class UsersApi {
/**
* Constructs a new UsersApi.
* @alias module:api/UsersApi
* @class
* @param {module:ApiClient} [apiClient] Optional API client implementation to use,
* default to {@link module:ApiClient#instanc
e} if unspecified.
*/
constructor(apiClient) {
this.apiClient = apiClient || ApiClient.instance;
}
The apiClient
parameter is optional when constructing, so in their example code, they leave it blank (since they’ve only instantiated one client) and it just defaults to that. I believe if you instantiate multiple, you should be able to just pass the correct client in, so: let tasksApiInstance1 = new Asana.TasksApi(client1)
. (Disclaimer: I haven’t tried this yet myself, so can’t confirm this is accurate).