C# Documentation


#1

Is there introductory documentation on how to begin with the API using C#?
I found this code which I am trying:
public string GetUsers()
{
var req = WebRequest.Create(“https://app.asana.com/api/1.0/users”);
SetBasicAuthentication(req);
try
{
return new StreamReader(req.GetResponse().GetResponseStream()).ReadToEnd();
}
catch (System.Net.WebException exWeb)
{
return “Error”;
}
}

    void SetBasicAuthentication(WebRequest req)
    {
        var authInfo = apiKey + ":";
        var encodedAuthInfo = Convert.ToBase64String(
            Encoding.Default.GetBytes(authInfo));
        req.Headers.Add("Authorization", "Basic " + encodedAuthInfo);
    }

But it is giving me error: The underlying connection was closed: An unexpected error occurred on a send, and the Status is SendFailure


#2

@Paul_Mayer can help!


#3

Thanks. What does that mean, exactly? Do I try to contact him, or you’ve tagged him and he’ll see this question?


#4

FYI I used https://github.com/acron0/AsanaNet as a starting point for the basis of my C# integration; you might want to take a look at that.

I’m not sure if your WebRequest-handling code is exactly correct or not but there are issues with your SetBasicAuthentication routine. First, I’m not sure why you’re adding the “:” to your apiKey but that shouldn’t be done. Second, change "Basic " to "Bearer ". Lastly, are you setting “apiKey” to the value of a Personal Access Token that you generated? Unless you’re using OAuth for authorization, “apiKey” will need to be a Personal Access Token.


#5

Thank you and disregard my Q right before your reply - we must’ve cross-posted.


#6

We did cross-post but note that I’m not @Paul_Mayer - he should surely chime in as well if he has things to add! :slight_smile:


#7

Right, you are not Paul! That’s what I get for skimming.
Anyway, I knew it was something obvious/stupid. When I tried this line:
var asana = new Asana(YOUR_API_KEY, AuthenticationType.Basic, errorCallback);
It said type/namespace asana can’t be found. How do I get the libraries?


#8

And @Phil_Seeman, reagarding my first code snippet and your comments on what to change, this was where I got it: https://stackoverflow.com/questions/10317171/how-do-i-connect-to-the-asana-rest-api-using-c


#9

Hmmm interesting on that StackOverflow post - the only thing I can think of is that perhaps the API has changed since 2012 when that post was made.

It looks like here you’re trying to use the AsanaNet library I mentioned? If so, you’ll need to download and compile the library from that Github repo and reference it in your test app.


#10

What exactly do I download and compile? I’m not using Python or javascript or Ruby… That is why I specifically titled this thread “C# Documentation”. Mine is a C# Windows desktop application.


#11

If my assumption is correct that you’re trying to use the AsanaNet library, then you’ll need to compile and reference that library - found here.


#12

Thank you! I am trying to build the library and I am getting an error in their code on “Function”. Here is their code and the error.

namespace AsanaNet
{
public partial class AsanaFunction
{
private static Dictionary<Function, AsanaFunction> Functions = new Dictionary<Function, AsanaFunction>();

“The type or namespace name Function could not be found…missing a user directive…?”

(Can we post screenshots here?)


#13

I found this, but a solution was never posted.


#14

OK, I took the plunge and chose Transform All T4 Templates off the build menu because that looked very interesting, and it built!


#15

OK, so now I am calling successfully into the asana DLL and I realize all the WebRequest code I had written is in the DLL so I don’t need any of that.

However, I am not getting anything written to my Output window when this executes:
var asana = new Asana(apiKey, AuthenticationType.Basic, errorCallback);

        asana.GetMe(o =>
        {
            var user = o as AsanaUser;
            Console.WriteLine("Hello, " + user.Name);
            System.Diagnostics.Debug.WriteLine("Hello, " + user.Name);
        });

        asana.GetWorkspaces(o =>
        {
            foreach (AsanaWorkspace workspace in o)
            {
                Console.WriteLine("Workspace: " + workspace.Name);
                System.Diagnostics.Debug.WriteLine("Workspace: " + workspace.Name);
            }
        });

What might be wrong now?


#16

I’d suggest wrapping that code in a Try/Catch block then stepping through it line by line in the debugger - you’re probably hitting an error somewhere and that’ll help you determine if you are, and on what line, and by examining the exception details, what the error is.


#17

No errror…no output.
I changed the code somewhat so I could better interrogate the variables. sReturned is empty string. sbReturned.ToString() is empty string. So it’s not working but there are no errors indicating what is wrong.
string sReturned = “”;
StringBuilder sbReturned = new StringBuilder();
try
{
//var asana = new Asana(apiKey, AuthenticationType.Basic, (s1, s2, s3) => { });
var asana = new Asana(apiKey, AuthenticationType.Basic, errorCallback);

            asana.GetMe(o =>
            {
                var user = o as AsanaUser;
                Console.WriteLine("Hello, " + user.Name);
                System.Diagnostics.Debug.WriteLine("Hello, " + user.Name);
                sReturned = "Hello, " + user.Name;
            });

            asana.GetWorkspaces(o =>
            {
                foreach (AsanaWorkspace workspace in o)
                {
                    Console.WriteLine("Workspace: " + workspace.Name);
                    System.Diagnostics.Debug.WriteLine("Workspace: " + workspace.Name);
                    sbReturned.AppendFormat("Workspace: {0} ", workspace.Name);
                }
            });

        }
        catch (Exception ex)
        {

        }

#18

I apologize for my poor code formatting. Are there tags I can use? I pasted a whole block and only some of it is automatically recognized as code.


#19

I am still trying to find out what is wrong. I am back at the documentation reading how to get started. I thought maybe I had to register my app so Asana knows it has my permission. It is a windows application. But the registration page said APP NAME and APP URL. There is no url as it is not a web application. Is this the correct way to get started accessing Asana and if so, what information do I need to give it?


#20

I have narrowed down the issue. Please see continuation of this problem in It works in VB.NET but not in C#