0

我正在尝试使用 Asana API 来更新和添加数据到 Asana。我如何在 C# 中做到这一点?

我得到的数据很好 - 下面的示例代码:

string apiKey = "xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxx";
var req = WebRequest.Create("https://app.asana.com/api/1.0/workspaces/xxxxxxxxxxxxxxx/projects");

var authInfo = apiKey + ":";
var encodedAuthInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
req.Headers.Add("Authorization", "Basic " + encodedAuthInfo);
var response = new StreamReader(req.GetResponse().GetResponseStream()).ReadToEnd();

但我不知道如何发布数据。

他们文档中的所有示例都使用我没有经验的 Python。

我已联系 Asana,但尚未收到回复。这就是我到目前为止所拥有的。我在最后一行收到 400 错误

var url = "https://app.asana.com/api/1.0/workspaces/xxxxxxxxxxxxxxxxxxx/tasks";
string json =
    "\"data\": {   " +
    "\"workspace\": nnnnnnnnnnnnnnnn," +
    "\"name\": \"test\"," +
    "\"notes\": \"testing API POST\"" +
    "}";          

byte[] bytes = Encoding.UTF8.GetBytes(json);
var req = WebRequest.Create(url) as HttpWebRequest;

req.Method = "POST";
req.ContentLength = bytes.Length;
req.ContentType = "application/json";

var requestStream = req.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);

var response = req.GetResponse();  //error
4

2 回答 2

1

这是我专门用来与 Asana API 交互的。编码参数函数采用键值对列表并将它们转换为“key=value&key=value”形式的字符串。

代码的发送数据部分不是我的,但我不记得我从哪里得到的。

我刚开始用 C# 开发一个 API 包装器,所以这是非常简单的测试。它可能没有它应该的那么强大,但我知道它确实工作得很好,可以在 Asana 中创建一个新的工作区。

    private string EncodeParameters(ICollection<KeyValuePair<string, string>> parameters)
    {
        string ret = "";
        foreach (KeyValuePair<string, string> pair in parameters)
        {
            ret += string.Format("{0}={1}&", pair.Key, pair.Value);
        }
        ret = ret.TrimEnd('&');

        return ret;
    }

    public string GetResponse(string uri, ICollection<KeyValuePair<string, string>> parameters, RequestMethods method = RequestMethods.POST)
    {
        return GetResponse(uri, EncodeParameters(parameters), method);
    }

    public string GetResponse(string uri, string data = "", RequestMethods method = RequestMethods.GET)
    {
        System.Diagnostics.Trace.WriteLine(uri);

        // create request
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
        request.PreAuthenticate = true;
        request.Method = method.ToString().ToUpper();
        request.ContentType = "application/x-www-form-urlencoded";

        // log in
        string authInfo = API_KEY + ":" + ""; // blank password
        authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
        request.Headers["Authorization"] = "Basic " + authInfo;

        // send data
        if (data != "")
        {
            byte[] paramBytes = Encoding.ASCII.GetBytes(data);
            request.ContentLength = paramBytes.Length;
            Stream reqStream = request.GetRequestStream();
            reqStream.Write(paramBytes, 0, paramBytes.Length);
            reqStream.Close();
        }

        // get response
        try
        {
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            return new StreamReader(response.GetResponseStream()).ReadToEnd();
        }
        catch (WebException ex)
        {
            HttpWebResponse response = ((HttpWebResponse)ex.Response);
            throw new Exception(uri + " caused a " + (int)response.StatusCode + " error.\n" + response.StatusDescription);
        }
    }
于 2013-03-02T19:04:31.587 回答
0

我就是这样做的:

public string SendRequest(WebRequestModel requestModel)
{
    var request = WebRequest.Create(uri);
    var encoding = new UTF8Encoding();
    var requestData = encoding.GetBytes(requestModel.POSTData);

    request.ContentType = requestModel.ContentType;

    request.Method = WebRequestMethods.Http.Post;
    request.Timeout = (300 * 1000); 
    request.ContentLength = requestData.Length;
    // add your header value here
    //request.Headers["myheader"] = "";

    using (var stream = request.GetRequestStream())
    {
          stream.Write(requestData, 0, requestData.Length);
    }
    response = request.GetResponse();

    using (var reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII))
    {
        result = reader.ReadToEnd();
    }
    return result;
}

我的 WebRequestModel 看起来像这样:

public class WebRequestModel
{
  public string Url { get; set; }
  public string POSTData { get; set; }
  public string ContentType { get; set; }
}
于 2012-12-10T15:02:40.523 回答