简体   繁体   English

REST API没有给出预期的结果

[英]rest api is not giving desired results

I am not getting the results that documentation says. 我没有得到文档所说的结果。 I login the Buddy; 我登录好友; created application; 创建的应用程序; copy this URL and assign to url string; 复制此网址并分配给网址字符串; when I execute the program I am not getting results that are expected (status + Accesstoken) as documentation says. 当我执行程序时,没有得到文档所述的预期结果(状态+ Accesstoken)。 Can anyone please tell me if I am missing something as newbie to http calls. 任何人都可以告诉我是否缺少http呼叫的新手。 Its running on http requester but not on Poster firefox add-on! 它可以在http请求者上运行,但不能在Poster firefox插件上运行!

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

Code

string parameters = "{appid:'xxxxxx', appkey: 'xxxxxxx', platform: 'REST Client'}"; 字符串参数=“ {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)
        { }
    }

Using the HTTP requester add-on on Firefox, I successfully retrieved an access token so their API work. 使用Firefox上的HTTP请求者插件,我成功检索了访问令牌,因此它们的API可以正常工作。

In C# they provide a line of code to submit your appid and appkey, that might be the problem : 在C#中,他们提供了一行代码来提交您的appid和appkey,这可能是问题所在:

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

My guess is you have to use their .NET SDK! 我的猜测是您必须使用他们的.NET SDK!

You can certainly use the REST API from raw REST the way you're doing, though the .NET SDK will handle some of the more complex details of changing service root. 尽管.NET SDK将处理更改服务根目录的一些更复杂的细节,但是您当然可以从原始REST中使用REST API。 I ran your code using my own Buddy credentials and I was able to get JSON containing an Access Token back. 我使用自己的Buddy凭据运行了您的代码,并且能够获取包含访问令牌的JSON。 You may need to read the response stream back as JSON to retrieve the access token. 您可能需要将响应流读回为JSON以检索访问令牌。 I used the following code to dump the JSON to the console: 我使用以下代码将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());

Using Newtonsoft.Json I can parse out my accessToken like this: 使用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);

The 3.0 SDK does all of this for you while exposing the rest of the service through a thin REST wrapper, the migration guide for the 3.0 SDK should help with this. 3.0 SDK为您完成了所有这些任务,同时通过瘦REST包装器公开了其余服务, 3.0 SDK迁移指南应对此有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM