-1

I'm currently trying to implement a Trello integration into Unity using the Trello Rest API. I am able to display the a given board with it's lists and cards. No problem so far. But as soon as I try to create or update a card, I get an unauthorized exception. My Token has write permission and when I run the command through ReqBin Curl tester everything is fine with the command and the card will be added to the board. But the HTTP-Request gives me the unauthorized error.

The curl command that works

curl -X POST https://api.trello.com/1/cards?idList={id_list}&key={app_key}&token={app_token} -d '{"name":"TestCard","desc":"description"}' --header "Content-Type: application/json"

The HTTP-Request function (data is currently an empty string, since I'm currently trying to add the data to the url)

private static async Task<bool> SendTrelloPostHttpRequest(string url, string data) {
   Debug.Log(url);
   using (var httpClient = new HttpClient()) {
      using (var request = new HttpRequestMessage(System.Net.Http.HttpMethod.Post, url)) {
         HttpResponseMessage response = await httpClient.PostAsync(url, new StringContent(data));
         if (!response.IsSuccessStatusCode) {
            Debug.LogError("Failed " + response.StatusCode);
            return false;
         } else {
            Debug.Log("Sucessfully " + response.Content.ToString());
           return true;
      }
   }
}

And this is the url I use to run the request

string url = $"{_trelloAPI}cards?idList={listId}&key={_trelloAppKey}&token={_trelloAppToken} -d '{{\"name\":\"{card.Name}\",\"desc\":\"{card.Desc}\"}}\' --header \"Content-Type: application/json\"";

I have no Idea why the curl request works and the http-request not, I double checkt everything but I can't spot any errors

bryanbcook
  • 16,210
  • 2
  • 40
  • 69

1 Answers1

0

I was able to fix it myself ^^

The Http request function now looks like this

        private static async Task<bool> TrelloHttpPostRequest(string url, string data) {
        Debug.Log("Post request - " + url);

        try {
            using (var httpClient = new HttpClient()) {
                HttpContent content = new StringContent(data, System.Text.Encoding.UTF8, "application/json");

                HttpResponseMessage response = await httpClient.PostAsync(url, content);

                if (!response.IsSuccessStatusCode) {
                    Debug.LogError($"HTTP Post failed ({response.Content.ToString()}) {response.StatusCode}");
                    return false;
                } else {
                    Debug.Log("HTTP Post sucessfully " + response.Content.ToString());
                    return true;
                }
            }
        } catch (System.Exception e) {
            Debug.LogError(e.Message.ToString());
            return false;
        }
    }

Now there is no need to have the payload as a part of the url anymore. This is the new url

string url = $"{_trelloAPI}cards?idList={card.IdList}&key={_trelloAppKey}&token={_trelloAppToken}";

So still not sure why the response was (401) Unauthorized, but with this code and url it works just fine.