Trello error 401 PHP

Hi, I’m new at Trello API and I’m working with on a PHP project. I’m trying to write a comment or put a label on a card following the example on the documentation (https://developer.atlassian.com/cloud/trello/rest/api-group-cards/#api-cards-id-actions-comments-post), but the only result is I got is error 401.

The error doesn’t occur with the Unirest\Request::get example

I’m quite sure that my token has all the authorizations. On the authorization page, I can see that the token can do everything but read my password.

What I am missing?

Thanks

Can you see the error message coming along with the 401?

Generally the error will respond with a message that includes some clue as to what is happening.

Here the error when I try to delete a card:

object(Unirest\Response)#1 (4) {
  ["code"]=>
  int(401)
  ["raw_body"]=>
  string(11) "invalid key"
  ["body"]=>
  string(11) "invalid key"
  ["headers"]=>
  array(25) {
    [0]=>
    string(10) "HTTP/2 401"
    ["x-dns-prefetch-control"]=>
    string(3) "off"
    ["x-frame-options"]=>
    string(4) "DENY"
    ["x-download-options"]=>
    string(6) "noopen"
    ["x-permitted-cross-domain-policies"]=>
    string(4) "none"
    ["referrer-policy"]=>
    string(31) "strict-origin-when-cross-origin"
    ["surrogate-control"]=>
    string(8) "no-store"
    ["cache-control"]=>
    string(53) "no-store, no-cache, must-revalidate, proxy-revalidate"
    ["pragma"]=>
    string(8) "no-cache"
    ["expires"]=>
    string(1) "0"
    ["x-trello-version"]=>
    string(8) "1.2308.0"
    ["x-trello-environment"]=>
    string(10) "Production"
    ["access-control-allow-origin"]=>
    string(1) "*"
    ["access-control-allow-methods"]=>
    string(22) "GET, PUT, POST, DELETE"
    ["access-control-allow-headers"]=>
    string(35) "Authorization, Accept, Content-Type"
    ["access-control-expose-headers"]=>
    string(188) "x-rate-limit-api-key-interval-ms, x-rate-limit-api-key-max, x-rate-limit-api-key-remaining, x-rate-limit-api-token-interval-ms, x-rate-limit-api-token-max, x-rate-limit-api-token-remaining"
    ["content-type"]=>
    string(25) "text/plain; charset=utf-8"
    ["content-length"]=>
    string(2) "11"
    ["date"]=>
    string(29) "Thu, 10 Dec 2020 10:12:44 GMT"
    ["x-envoy-upstream-service-time"]=>
    string(2) "93"
    ["expect-ct"]=>
    string(108) "report-uri="https://web-security-reports.services.atlassian.com/expect-ct-report/trello-edge", max-age=86400"
    ["strict-transport-security"]=>
    string(25) "max-age=63072000; preload"
    ["x-content-type-options"]=>
    string(7) "nosniff"
    ["x-xss-protection"]=>
    string(13) "1; mode=block"
    ["server"]=>
    string(16) "globaledge-envoy"
  }
}

That means that the API key isn’t making it through correctly. Can you double-check that you are using the correct API key?

The key works correctly with the get request.
This is all the code I’m using:

<?php
require_once '/Users/silvio/vendor/mashape/unirest-php/src/Unirest.php';
$headers = array(
  'Accept' => 'application/json'
);

$query = array(
  'key' => '{MYKEY}',
  'token' => '{MY TOKEN}'
  );

  // $response = Unirest\Request::get(
  //   'https://api.trello.com/1/cards/5fd1f3aff7f21008fca68a1b',
  //   $headers,
  //   $query
  // );

  $response = Unirest\Request::post(
    'https://api.trello.com/1/cards/5fd1f3aff7f21008fca68a1b/idLabels',
    $query
  );

var_dump($response);
?>

What are you trying to do in that request?

It looks like a POST to /cards/{idCard}/idLabels with only a key and token in the parameters.

I’d expect a request to create a new label to look like this:

curl 'https://trello.com/1/card/5d1d0f762f29801443e2a61b/labels/?key={{key}}&token={{token}}' \
  -H 'accept: application/json' \
  -H 'content-type: application/json' \
  --data-binary '{"name":"asd","color":"pink","idBoard":"586e8f681d4fe9b06a928307"}' \
  --compressed

You’ll notice that there are additional parameters required to create a label.

I notice that in your code after the idCard you use /labels/ and then all the parameters.

in the example code found here the url is:
/cards/{id}/idLabels

I change my URL from /idLabel/ to /labels/ and it works, even without all the parameters.
Then I’ve put also all the correct parameters and still works