尝试使用API​​创建任务时获取响应代码400

时间:2015-02-11 10:48:15

标签: asana

我使用的是Asana API PHP类,托管在这里:https://github.com/ajimix/asana-api-php-class

我正在使用他们几乎精确的示例代码:

<?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;
  }

我对此原始代码所做的更改:

  1. 我从Asana获得了我的API密钥,所以我假设它应该没有错,并把它放在apiKey数组索引
  2. 我实际上并不知道工作区ID是什么,但我的项目的网址格式为https://app.asana.com/{integer}/{integer}/{integer},所以我使用第一个整数作为$workspaceId和第二个整数一个(与第三个相同)和$projectId。我也尝试使用第二个整数作为$projectId$workspaceId具有相同的结果
  3. 我在assignee电话
  4. 中的createTask()数组索引下放了我自己的Asana电子邮件
  5. 我从followers调用
  6. 中删除了createTask()数组项

    只有这些更改,然后运行此代码,我得到Error while trying to connect to Asana, response code: 400。在Asana页面上没有进一步的错误代码说明或常见问题解答。可能是什么问题呢?

1 个答案:

答案 0 :(得分:0)

Asana中的(通常)格式为0/{project}/{task or project} - 如果您想知道自己的工作区ID,则应GET /workspaces作为当前用户 - 这将为您提供工作区列表当前用户以及工作区名称和工作区ID。

当请求出现问题时,我们会返回400 Bad Request - 遗憾的是,您正在使用的PHP库似乎没有将Asana的错误传回给您:-(但是,鉴于第一个整数路径中的0是非有效工作空间,可能至少是问题的一部分。