我如何连接到嘉尚REST API使用C#? [英] How do I connect to the Asana Rest API using c#?

查看:176
本文介绍了我如何连接到嘉尚REST API使用C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有一个代码片段使用C#连接到嘉尚API?



有是在其网站上一个Hello World应用程序,但不幸的是它是写在红宝石



的https:/ /asana.com/developers/documentation/examples-tutorials/hello-world



我在做这个作为一个快速侧的项目,只能奉献出少量的时间到它。任何帮助将不胜感激。



在此先感谢!






追问:



我试过访问/验证多种方式和我不断收到一个401没有获得许可误差



我最新的代码如下所示:

  HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(HTTPS:/ /app.asana.com/api/1.0/users/me); 
request.Method =GET;
request.Headers.Add(授权:基本+* * MyUniqueAPIKey);

request.ContentType =申请/的X WWW的形式,进行了urlencoded
request.Accept =应用/ JSON;

WebResponse的WS = request.GetResponse();


解决方案

试试这个代码。我能得到用户的列表与它:

 常量字符串apiKey =whateverYourApiKeyIs; 

公共字符串GetUsers()
{
VAR REQ = WebRequest.Create(https://app.asana.com/api/1.0/users);
SetBasicAuthentication(REQ);
返回新的StreamReader(req.GetResponse()GetResponseStream()。)ReadToEnd的()。
}

无效SetBasicAuthentication(WebRequest的REQ)
{
VAR AUTHINFO = apiKey +:
VAR encodedAuthInfo = Convert.ToBase64String(
Encoding.Default.GetBytes(AUTHINFO));
req.Headers.Add(授权,基本+ encodedAuthInfo);
}

这只是返回数据作为一个字符串。解析JSON就留给读者自己练习。 ;)



我在这里借用了 SetBasicAuthentication 方法:



强制基本的HTTP身份验证HttpWebRequest的(在.NET中/ C#)


Does anyone have a snippet of code for connecting to the Asana API using c#?

There is a Hello World application on their site but unfortunately it is written in ruby.

https://asana.com/developers/documentation/examples-tutorials/hello-world

I'm doing this as a quick side project and can only dedicate a small amount of time to it. Any help would be greatly appreciated.

Thanks in advance!


Follow up:

I've tried multiple ways of accessing/authentication and I keep getting a 401 Not Authorized error.

My latest code looks like this:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://app.asana.com/api/1.0/users/me");
request.Method = "GET";
request.Headers.Add("Authorization: Basic " + "*MyUniqueAPIKey*");

request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "application/json";

WebResponse ws = request.GetResponse();

解决方案

Try this code. I was able to get a list of users with it:

const string apiKey = "whateverYourApiKeyIs";

public string GetUsers()
{
    var req = WebRequest.Create("https://app.asana.com/api/1.0/users");
    SetBasicAuthentication(req);
    return new StreamReader(req.GetResponse().GetResponseStream()).ReadToEnd();
}

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

This just returns the data as a string. Parsing the JSON is left as an exercise for the reader. ;)

I borrowed the SetBasicAuthentication method from here:

Forcing basic http authentication for HttpWebRequest (in .NET/C#)

这篇关于我如何连接到嘉尚REST API使用C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆