Search code examples
c#uwpasanaasana-api

Asana: 401: Not authorized on put requests but not on get requests


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.


Solution

  • I used Authorization: bearer instead of Authorization: Bearer. Note the capitalization, that does the trick! It's strange though, that the lower-case bearer is return during oauth (token_type in the response) and works well with GETting but not PUTing.

    Anyway, thanks Agnoster.