0

I am using Asana API PHP class, hosted here: https://github.com/ajimix/asana-api-php-class

And I am using their almost exact sample code:

<?php
  // See class comments and Asana API for full info
  $asana = new Asana(array('apiKey' => 'xxxxxxxxxxxxxxxxxxxxxxxx')); // Your API Key, you can get it in Asana
  $workspaceId = 'XXXXXXXXXXXXXXXXXXX'; // The workspace where we want to create our task
  $projectId = 'XXXXXXXXXXXXXXXXXXX'; // The project where we want to save our task

  // First we create the task
  $result = $asana->createTask(array(
      'workspace' => $workspaceId, // Workspace ID
      'name' => 'Hello World!', // Name of task
      'assignee' => 'bigboss@bigcompany.com', // Assign task to...
  'followers' => array('XXXXX', 'XXXXXXXX') // We add some followers to the task... (this time by ID), this is totally optional
  ));

  // As Asana API documentation says, when a task is created, 201 response code is sent back so...
  if ($asana->responseCode != '201' || is_null($result)) {
      echo 'Error while trying to connect to Asana, response code: ' . $asana->responseCode;
      return;
  }

  $resultJson = json_decode($result);
  $taskId = $resultJson->data->id; // Here we have the id of the task that have been created
  // Now we do another request to add the task to a project
  $result = $asana->addProjectToTask($taskId, $projectId);
  if ($asana->responseCode != '200') {
      echo 'Error while assigning project to task: ' . $asana->responseCode;
  }

Changes I made to this original code:

  1. I got my API key from Asana, so I am assuming it should be nothing wrong with it, and put it inside the apiKey array index
  2. I don't actually know what the workspace ID is, but the URL of my project is in the format of https://app.asana.com/{integer}/{integer}/{integer}, so I used the first integer as the $workspaceId and the second one (which is the same as the third one) as the $projectId. I also tried using the second integer as both the $projectId and the $workspaceId with the same outcome
  3. I put my own Asana email under the assignee array index in the createTask() call
  4. I removed the followers array item from the createTask() call

With only those changes, and then running this code, I get Error while trying to connect to Asana, response code: 400. No further error code explanations or a FAQ are available on the Asana page. What could be the problem?

Digital Ninja
  • 3,415
  • 5
  • 26
  • 51

1 Answers1

0

URLs in Asana are (typically) of the form 0/{project}/{task or project} - if you want to know your workspace ID, you should GET /workspaces as the current user - this will give you a list of the workspaces the current user is in, along with the name of the workspace and the workspace ID.

We return a 400 Bad Request when there is something wrong with the request - unfortunately it appears the PHP library you are using doesn't pass the error from Asana back to you :-( However, given that the first integer in the path is 0 which is not a valid workspace, that's likely to be at least part of the problem.

agnoster
  • 3,744
  • 2
  • 21
  • 29