rest api is not giving desired results

129 views Asked by At

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. Can anyone please tell me if I am missing something as newbie to http calls. Its running on http requester but not on Poster firefox add-on!

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

Code

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

There are 2 answers

4
Pak On

Using the HTTP requester add-on on Firefox, I successfully retrieved an access token so their API work.

In C# they provide a line of code to submit your appid and appkey, that might be the problem :

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

My guess is you have to use their .NET SDK!

0
LrdCasimir On

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. I ran your code using my own Buddy credentials and I was able to get JSON containing an Access Token back. You may need to read the response stream back as JSON to retrieve the access token. I used the following code to dump the JSON to the console:

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:

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.