1

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)
        { }
    }
LrdCasimir
  • 123
  • 5
user3102858
  • 128
  • 10

2 Answers2

0

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!

Pak
  • 2,123
  • 22
  • 28
  • Thanks for your reply @Pak; I am also getting token in firefox add-on. But its not coming in code. can you please see my code or give me some other code that can do http request ? I will be very thankful to you! – user3102858 Mar 08 '14 at 16:05
  • Its running on http requester but not on Poster add-on – user3102858 Mar 08 '14 at 18:52
  • Well you have to use their C# SDK at least to load the application parameters but otherwise I think your code's fine. – Pak Mar 09 '14 at 13:37
  • Here is their [getting started guide](http://dev.buddyplatform.com/Home/Docs/Getting%20Started%20-%20.NET/HTTP?) – Pak Mar 09 '14 at 13:49
0

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.

LrdCasimir
  • 123
  • 5