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.

Frosty
  • 46
  • 4
  • In general, there is no reason you should be able to GET a resource but get an Unauthorized when PUTting to it, unless you're trying to update something you can't (like setting the name of a Team you can see but not modify), and even then you should get a 403 Forbidden. Could you try replicating the case using `curl -v`? Does it still happen then? I'm not really familiar with UWP so getting this to the point of "looking at bits going over the wire" might help. – agnoster Dec 14 '15 at 09:35
  • I tried it, I get the exact same result... anything specific you're looking for in the verbose output of curl? my request: curl --request PUT -H "Authorization: bearer " https://app.asana.com/api/1.0/tasks/ -d "notes=" -v – Frosty Dec 14 '15 at 16:26
  • Hmm, actually "Authorization: bearer" will return Not Authorized because the "bearer" needs to be capitalized. Does the curl call still fail if you fix that to "Authorization: Bearer [TOKEN]"? – agnoster Dec 14 '15 at 19:38
  • Old thread but my problem was a missing space after "Bearer". It worked when the tokens used to start with "0", but it doesn't work with tokens starting with "1". ¯\_(ツ)_/¯ Anyway, I hope that helps someone – joehanna Feb 05 '22 at 05:28

1 Answers1

1

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.

Frosty
  • 46
  • 4