2017-03-05 71 views
0

我似乎無法將標籤添加到新的或現有的任務。Asana API:使用任務/ addTag或使用標籤創建任務時出現無效請求

我使用的是從GitHub的API在https://github.com/Asana/php-asana

%的文檔here,我設置的選項,併發射了API調用任務的端點。它失敗:

致命錯誤:在/mydir/asana/Asana/Errors/AsanaError.php:29

// create new task 
    $newTaskOptions = array(
     'name' => $taskName, 
     'notes' => $taskNotes, 
     'projects' => [11111111115445], 
     'tags' => [11111119991, 11111119992] // without this, the task is created ok 
    ); 

    $newTask = $client->tasks->create($newTaskOptions); 

未捕獲的異常 '體位\錯誤\ InvalidRequestError' 有消息 '無效請求'這裏是發送到request程序對象:

array(2) { 
    ["headers"]=> array(1) { 
    ["content-type"] => string(16) "application/json" 
    } 
    ["data"]=> array(2) { 
    ["data"]=> array(4) { 
     ["name"]=> string(17) "module 1 - task 1" 
     ["notes"]=> string(32) "description of module 1 - task 1" 
     ["projects"]=> array(1) { 
     [0]=> int(11111111115445) 
     } 
     ["tags"]=> array(2) { 
     [0]=> int(11111119991) 
     [1]=> int(11111119992) 
     } 
    } 
    ["options"]=> array(0) { 
    } 
    } 
} 

即使他們的[ { id: 59746, name: 'Grade A' }, ... ]例子(使用正確的標籤ID和姓名),但仍然出現了錯誤。實際上,它會在第一個「{」處引發語法錯誤。

接下來,如果我嘗試tasks/taskid/addTag到現有任務,我收到類似的錯誤。下面是這段代碼。

foreach ($tags as $tag){ 
    $newTag = $client->tasks->addTag($newTask->id, array('tag' => $tag)); 
} 

addTag命令的第二部分需要數組,並根據該文檔使用tag作爲數組鍵。我嘗試了其他鍵,如textdatatags無濟於事。

回答

0

這裏是問題....標籤分配給工作區。因此,從工作區-1 /項目-1 /任務-1複製到workspace-2/project-1/task-1的標籤將無法工作,除非標籤最初是在工作區-2中創建的。

在目標工作區中創建標籤後,代碼完美無缺地工作。

0

我看了一遍再現你的第一個例子,它完全爲我工作。 (不幸的是,我們的API在一些地方是不對稱的,下面是其中一個:發送ID數組是最好的例子,但是你會得到的是在響應中嵌套的{ID, name}對)

我不確定你可能會遇到什麼 - 我懷疑它可能是你正在使用的實際PHP代碼之外的東西。如果Asana中不存在標籤的ID,我能夠得到無效的請求錯誤,這是否會成爲問題?

爲了排除錯誤,我們發回了我們希望的回覆中相當友好的消息。如果更改上面

try { 
    $newTask = $client->tasks->create($newTaskOptions); 
} catch (Asana\Errors\InvalidRequestError $e) { 
    var_dump($e->response->body); 
} 

希望它會幫助你的請求的代碼,你看看這是怎麼回事(即我的清樣,我有)

object(stdClass)#24 (1) { 
    ["errors"]=> 
    array(1) { 
    [0]=> 
    object(stdClass)#25 (2) { 
     ["message"]=> 
     string(40) "tags: [1]: Unknown object: 1980346754317" 
     ["help"]=> 
     string(155) "For more information on API status codes and how to handle them, read the docs on errors: https://asana.com/developers/documentation/getting-started/errors" 
    } 
    } 
} 
+0

我發現問題....標籤被分配給工作區。因此,我從workspace-1/project-1/task-1複製到** workspace-2 **/project-1/task-1的標籤將無法工作,除非我首先在workspace-2中創建標籤。 – limeygent