【问题标题】:Trello Rest API Create Card returns 401 Unauthorized - but only via CodeTrello Rest API 创建卡返回 401 Unauthorized - 但只能通过代码
【发布时间】:2022-06-10 21:10:53
【问题描述】:

我目前正在尝试使用 Trello Rest API 将 Trello 集成到 Unity 中。我能够用它的列表和卡片显示给定的板。到目前为止没有问题。但是,一旦我尝试创建或更新卡片,就会收到未经授权的异常。我的令牌具有写入权限,当我通过 ReqBin Curl 测试器运行命令时,命令一切正常,卡片将被添加到板上。但是 HTTP-Request 给了我未经授权的错误。

有效的 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"

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

这是我用来运行请求的 url

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

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

【问题讨论】:

  • 您没有添加令牌?
  • 是的。令牌已添加且对读写有效
  • 不在你的代码中它不是。
  • 它是 URL 的一部分。但是,我能够修复它。我稍微重写了 Post 函数。我会立即发布修复程序

标签: c# unity3d trello


【解决方案1】:

我自己修好了^^

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

现在不再需要将有效负载作为 url 的一部分。这是新网址

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

所以仍然不确定为什么响应是 (401) Unauthorized,但是使用此代码和 url 它可以正常工作。

【讨论】:

    猜你喜欢
    • 2019-06-17
    • 2021-09-27
    • 2012-08-18
    • 2012-08-20
    • 2021-05-14
    • 2020-06-11
    • 1970-01-01
    • 2022-07-26
    • 1970-01-01
    相关资源
    最近更新 更多