简体   繁体   English

Trello Rest API 创建卡返回 401 Unauthorized - 但只能通过代码

[英]Trello Rest API Create Card returns 401 Unauthorized - but only via Code

I'm currently trying to implement a Trello integration into Unity using the Trello Rest API.我目前正在尝试使用 Trello Rest API 将 Trello 集成到 Unity 中。 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.我的令牌具有写入权限,当我通过 ReqBin Curl 测试器运行命令时,命令一切正常,卡片将被添加到板上。 But the HTTP-Request gives me the unauthorized error.但是 HTTP-Request 给了我未经授权的错误。

The curl command that works有效的 curl 命令

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) HTTP-Request 函数(数据当前是一个空字符串,因为我目前正在尝试将数据添加到 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这是我用来运行请求的 url

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我不知道为什么 curl 请求有效而 http-request 无效,我仔细检查了所有内容,但没有发现任何错误

I was able to fix it myself ^^我能够自己修复它^^

The Http request function now looks like this Http 请求函数现在看起来像这样

        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.现在不再需要将有效负载作为 url 的一部分。 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.所以仍然不确定为什么响应是 (401) Unauthorized,但是使用此代码和 url 它工作得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM