休息api没有给出理想的结果

时间:2014-03-07 02:36:33

标签: c# rest http buddy.com

我没有得到文件说的结果。我登录好友;创建申请;复制此URL并分配给url字符串;当我执行程序时,我没有得到预期的结果(状态+ Accesstoken),如文档所述。任何人都可以告诉我,如果我错过了http呼叫的新手。它在http请求者上运行,但不在Poster firefox附加组件上运行!

文档 http://dev.buddyplatform.com/Home/Docs/Getting%20Started%20-%20REST/HTTP

代码

string parameters =“{appid:'xxxxxx',appkey:'xxxxxxx',platform:'REST Client'}”;

private async void SimpleRequest()
    {
        HttpWebRequest request = null;
        HttpWebResponse response = null;

        try
        {
            request = (HttpWebRequest)WebRequest.Create(url);
            request.Accept = "application/json";
            request.ContentType = "application/json";
            request.Method = "POST";

            StreamWriter sw = new StreamWriter(await request.GetRequestStreamAsync());
            sw.WriteLine(parameters);
            sw.Close();

            response = (HttpWebResponse) await request.GetResponseAsync();

         }
        catch (Exception)
        { }
    }

2 个答案:

答案 0 :(得分:0)

在Firefox上使用HTTP请求者加载项,我成功检索了一个访问令牌,以便他们的API工作。

在C#中,他们提供了一行代码来提交你的appid和appkey,这可能是问题所在:

Buddy.Init("yourAppId", "yourAppKey");

我的猜测是你必须使用他们的.NET SDK!

答案 1 :(得分:0)

您当然可以使用原始REST中的REST API,尽管.NET SDK将处理更改服务根目录的一些更复杂的细节。我使用自己的Buddy凭证运行您的代码,然后我能够获得包含访问令牌的JSON。您可能需要将响应流作为JSON读回以检索访问令牌。我使用以下代码将JSON转储到控制台:

request = (HttpWebRequest)WebRequest.Create(url);
request.Accept = "application/json";
request.ContentType = "application/json";
request.Method = "POST";

StreamWriter sw = new StreamWriter(await request.GetRequestStreamAsync());
sw.WriteLine(parameters);
sw.Close();

response = (HttpWebResponse)await request.GetResponseAsync();
Console.WriteLine(await new StreamReader(response.GetResponseStream()).ReadToEndAsync());

使用Newtonsoft.Json我可以像这样解析我的accessToken:

Uri url = new Uri("https://api.buddyplatform.com/devices");

request = (HttpWebRequest)WebRequest.Create(url);
request.Accept = "application/json";
request.ContentType = "application/json";
request.Method = "POST";

StreamWriter sw = new StreamWriter(await request.GetRequestStreamAsync());
sw.WriteLine(parameters);
sw.Close();

response = (HttpWebResponse)await request.GetResponseAsync();
var parsed = JsonConvert.DeserializeObject<IDictionary<string,object>>( (await new StreamReader(response.GetResponseStream()).ReadToEndAsync()));
var accessToken = (parsed["result"] as JObject).GetValue("accessToken").ToString();
Console.WriteLine(accessToken);

3.0 SDK为您完成所有这些工作,同时通过瘦REST包装器公开其余服务,migration guide for the 3.0 SDK应该有帮助。