Search code examples
monorestsharpasana

Asana Authorization error on Mono.NET framework


I'm trying to use the Asana restful API and I receive this error:

{"errors":[{"message":"Not Authorized"}]}

 public static string GetProjects()
{
    string url = "https://app.asana.com/api/1.0/projects/"; // Constants.BaseApiUrl + "projects";
    var client = new RestClient(url);
    System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
    client.Authenticator = new HttpBasicAuthenticator(AsanaAPIKey.GetBase64(), "");
    var req = new RestRequest(Method.GET);
    RestResponse res =(RestResponse) client.Execute(req);

    return res.Content;
}

public static bool CheckValidationResult(object sp,
    X509Certificate cert,
    X509Chain req,
    System.Net.Security.SslPolicyErrors problem)
{
    return true;
}

I've tried plain httpwebrequest/Httpwebresponse and it didn't work either so I tried the restsharp library and still the same problem.

Any ideas why this error is happening?


Solution

  • I don't know .NET but I see you're creating an HttpBasicAuthenticator and it looks like you're passing it a username/password pair. But you are passing it a base64-encoded version of the API key, which is wrong. The documentation on authentication states that when using an HTTP library you should pass the API key as the username, unchanged. You only need to manually base64-encode if you are constructing the full header manually.