Not authorized response

I have Asana Premium. I have code that uses API to get task information. I get the task info just fine. I then need to get last story date/time - I am looking for tasks that are sitting without activity. For some reason I am getting the following response:

“StatusCode: Unauthorized, Content-Type: application/json; charset=UTF-8, Content-Length: 231)”

I can use the same URL in a web browser to get the json manually so I know that I can GET the info.

What could be wrong?

My code:

        button2.Enabled = false;
    var client = new RestClient(@"URL REMOVED BUT THIS ONE GETS A RESULT");
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
    var request = new RestRequest(Method.GET);
    request.AddHeader("Authorization", "Bearer " + asanaToken);
    IRestResponse response = client.Execute(request);

    if (!response.IsSuccessful)
    {
        Log.Error(response?.ErrorException?.Message);
        Log.CloseAndFlush();
        MessageBox.Show("There was an error connecting to Asana. Please contact Engineering.");
    }
    else
    {
        TaskRootObject JSONObj = JsonConvert.DeserializeObject<TaskRootObject>(response.Content);

        //Loop through the tass and get stories
        foreach (Task item in JSONObj.data)
        {
                string url = String.Format(@"https://app.asana.com/api/1.0/tasks/{0}/stories", item.gid);
                var storiesClient = new RestClient(url);
                //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
                var storiesRequest = new RestRequest(Method.GET);
                request.AddHeader("Authorization", "Bearer " + asanaToken);
                IRestResponse storiesResponse = client.Execute(storiesRequest);
                
        }
        asanaListView.Objects = JSONObj?.data;
        asanaListView.Sort(Status, SortOrder.Ascending);
    }

My first thought is, have you queried item.gid inside the foreach loop to make sure it’s the value you’re expecting?

My second thought is, Asana OAuth tokens expire and when a token is expired, you’ll get a “Not authorized” error.

Thanks Phil:
I thought the same thing on the item.gid in the foreach. Not the issue. I can copy the url that is produced and past it into a web browser and get good json back.

If the token expires then nothing would work using that token correct? I get results back in my first call.

Looks like maybe a throttling issue???
I just pasted my hand created story url in my code and got the story first and I received a valid result.

Found it. It was a bug in my code. I was reusing the first request and adding a authorization token to it.

1 Like