1

I'm developing a UWP app for a quick overview and some time management for my tasks on asana. However, I cannot update any task while getting everything works fine.

The setup is the following: I obtain an oauth token via a WebView-Element (hosted web browser) by intercepting the navigation events in case of navigating to the OAuth redirect url.

I perform my api calls to the rest services as follows:

    private static async Task<JObject> CallApiGet(string api)
    {
        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Add("Authorization", TokenType + " " + AccessToken);
        HttpResponseMessage result = await client.GetAsync(baseAPIUrl + api);
        result.EnsureSuccessStatusCode();

        string asText = await result.Content.ReadAsStringAsync();
        return JObject.Parse(asText);
    }

    private static async Task<JObject> CallApiPut(string api, params string[] properties)
    {
        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Add("Authorization", TokenType + " " + AccessToken);



        var keys = properties.Where((item, index) => index % 2 == 0);
        var values = properties.Where((item, index) => index % 2 != 0);

        string urlEncoded = string.Join("&", keys.Zip(values, (key, value) => Uri.EscapeDataString(key) + "=" + Uri.EscapeDataString(value)));
        HttpContent content = new StringContent(urlEncoded, null, "application/x-www-form-urlencoded");

        HttpResponseMessage result = await client.PutAsync(baseAPIUrl + api, content);
        result.EnsureSuccessStatusCode();

        string asText = await result.Content.ReadAsStringAsync();
        return JObject.Parse(asText);
    }

The strange thing is now that the Get call work while the PUT call fails. One example is

CallApiPut("/tasks/<task id>", "notes","updated notes value");

Any idea why my put calls return 401: Not authorized while my get calls work just fine? Note that I can edit the specified tasks in the web gui of asana.

Thank you very much for your help.

4

1 回答 1

1

我用Authorization: bearer而不是Authorization: Bearer. 注意大写,这样就行了!不过奇怪的是,小写字母bearer在 oauth 期间返回(token_type在响应中)并且适用于 GETting 但不适用于 PUTing。

无论如何,谢谢阿格诺斯特。

于 2015-12-14T21:59:38.797 回答