0

i am getting a 401 response from Asana with my request.

var url = "https://app.asana.com/api/1.0/users/me";
byte[] encodedByte = System.Text.ASCIIEncoding.ASCII.GetBytes(APIKey);
APIKey = Convert.ToBase64String(encodedByte);
WebRequest wrGETURL;
wrGETURL = WebRequest.Create(url);
wrGETURL.Headers.Add("Authorization: Basic " + APIKey);
string result;
using (StreamReader reader = new StreamReader(wrGETURL.GetResponse().GetResponseStream()))
{
    result = reader.ReadToEnd();
}
return result;
Billybonks
  • 1,568
  • 3
  • 15
  • 32

1 Answers1

1

The way HTTP basic auth works, you encode the username and password together as base64, separated by a colon. In the Asana API the key is the username and there is no password.

From the docs at https://asana.com/developers/documentation/getting-started/authentication#sts=API%20Keys :

Note: Most utilities and libraries that allow you to specify a username and password will handle proper encoding of the header for you. However, if you need to set the Authorization header manually, the header value is constructed by adding a colon (:) to the API key, then base64-encoding that string. You can read more on basic authentication if you need further details.

So, you should probably do:

byte[] encodedByte = System.Text.ASCIIEncoding.ASCII.GetBytes(APIKey + ":")

Justin
  • 3,418
  • 3
  • 27
  • 37
Greg S
  • 2,079
  • 1
  • 11
  • 10