ASP.net (VB code behind) JSON parsing facebook Graph API response - asp.net

net but wanted to try to do this code in ASP.net instead of my normal classic ASP.
I have been trying to find code examples that would show me how to parse out the name & id in a returned JSON from a facebook API Graph call. The JSON return looks like this from Facebook:
{
"data": [
{
"name": "David xxxxxx",
"id": "05121212",
"administrator": true
},
{
"name": "Billy xxxxxxx",
"id": "0005128888"
}
],
"paging": {
"next": "https://graph.facebook.com/xxxxx/members?format=json&limit=5000&offset=5000&__after_id=xxxxx"
}
}
Any examples on how to go about parsing out just the name and id from the JSON response in ASP.net would be awesome!
Thanks,
David

Go for, http://james.newtonking.com/
string response = <your fb data>; // I am lazy :P
JObject obj = JObject.Parse(response);
JArray data = (JArray)obj["data"];
for(int i=0,int len=data.count; i < len ; i++)
{
string name = data[i]["name"].ToString();
string id = data[i]["id"].ToString();
string administrator = string.Empty;
if(data[i]["administrator"]!=null)
{
string administrator = data[i]["administrator"].ToString();
}
}
I think, this code is enough to get you going.
Always check for null as api data may or may not have that value.
Edit: I noticed that you wanted a VB code, sorry. But it may help others, so leaving it here. You can convert the code from any C# to VB convertor.

Regardless of whether or not there is a known library for .NET and Open Graph, Json is Json. The way I see it you have three options:
1) Use Newtonsoft Json. You can install this package using nuget into your ASP.NET project and from there there are lots of places on the web that talk about working with this library. http://james.newtonking.com/ is the home page of the library, there are also posts here.
2) Use .NET Json. Again, lots of info on the web here. I found a pretty good looking post here Parse JSON in C#
3) Use the C# Facebook SDK. The FacebookClient class has the ability to serialize and de-serialize Json. You can also install this library via Nuget. I admit the documentation on the C# SDK is lacking, but none the less it works well. More information about it can be found here: http://blog.prabir.me/category/Facebook-C-SDK.aspx
I hope this helps you down the right path.
-Eric

There are no known supported libraries for .net for the new graph api. https://github.com/facebook-csharp-sdk has a few samples for using .net though.

Related

Microsoft Graph API online meetings DateTimeOffset format

I have a simple task which involves creating online meetings using Microsoft Graph API. I'm using the basic sample code from the site, something like this:
var onlineMeeting = new OnlineMeeting
{
StartDateTime = DateTimeOffset.Parse("2019-07-12T21:30:34.2444915+00:00"),
EndDateTime = DateTimeOffset.Parse("2019-07-12T21:30:34.2444915+00:00"),
Subject = "This is the subject"
};
var meeting = await graphClient.Users["userid here"].OnlineMeetings.Request().AddAsync(onlineMeeting);
This unfortunately gives a 400 response with a very obsure reasoning. However, I was able to narrow down the probable cause of the problem: the serialization of the DateTimeOffset properties. For some reason, my requests contain the data in the format like 19/07/12 21:30:34 -07:00", instead of the required format, which is basically the same as the argument provided for DateTimeOffset.Parse().
My question is how can I customize the serialization format in the SDK? And more importantly, why should I do this explicitly, and why can't I find any mention of this in the documentation?
Turns out I have not updated the SDK for a while now and I was using an old version. I updated to the latest version and the problem went away :)

Override the method of packing HTTP form-urlencoded parameters in Meteor HTTP call

I'm using Meteor to consume a remote API. One of the endpoints of this API requires an (ordered) array of credentials, so the data would look like
{
"country": "de",
"credentials": ["admin", "password"],
"whatever": "whatever"
}
When I provide this plain-object as the value to param property of HTTP.post like this
HTTP.post('https://api.whatever.org/whatever', {
headers: {
"Authorization": "Basic ".concat(...)
},
params: {
"country": "de",
"credentials": ["admin", "password"],
"whatever": "whatever"
}
});
then the parameters are packed this way:
country=de
credentials=admin,password
whatever=whatever
but they should be packed this way:
country=de
credentials=admin
credentials=password
whatever=whatever
I tried using a Content-Type header but it didn't help.
I tried using content and data instead of params with different outcomes and then ended concatenating all the values into a query string an putting it into content property. But this isn't really a nice piece of code and surely not one that is easy to maintain.
I've read docs but haven't found anything that would help.
Where should I look for the information regarding this topic? Is there a better way to override the way HTTP.post (or, in general, HTTP.call) computes the body of the query to send?
Where should I look for the information regarding this topic?
In the source code.
I know nothing about Meteor, but I’m looking into its source code and I see no public hook which could help you. Of course, you can replace URL._encodeParams with your own function, but that is less maintainable than submitting the encoded params as raw data.

Linq2Dynamodb run expression string on Table

I have an ASP.NET Web API site which records all objects in AWS DynamoDb. I took a quick look at linq2Dynamodb. It seems that the common way to use it is like:
var moviesTable = ctx.GetTable<Movie>();
var inceptionMovie = moviesTable
.Where(m => m.Genre == "Thriller" && m.Title == "Inception")
.Single();
But I want some API like:
moviesTable.Execute(string querystring);
The reason is that from the Web API, I usually get some query like:
http://host/service.svc/Orders?$filter=ShipCountry eq 'France'
I'd like to pass the filter string "ShipCountry eq 'France'" here. Do anyone know if there is a way for me to do this? Thanks.
With Linq2DynamoDb you can expose your DynamoDb tables as OData resources. Please, see the doc here.
As soon as you have an OData resource, you can query it with OData queries of any valid kind.
And you can use any OData client library for that. E.g. with System.Data.Services.Client you would say something like:
var entities = myContext.Execute<MyEntityType>(new Uri("<here goes URI with valid OData query string>"), "GET", true);
or just construct a LINQ query on the client side.
The drawback is that you will need to host your OData service somewhere (DynamoDb itself doesn't support OData, so that's the reason).
The advantages are:
you will be able to cache your data in ElastiCache.
you will be able to implement custom authentication inside your service.
Sorry for a bit late answer.

Twitter Library for ASP.NET MVC that implements REST API

I been looking for a twitter lib for my ASP.NET NET MVC 3 software, but I need to implement the REST API functions that I nor found in Twitter Helper or Twitterizer. Rest API allow me to use a Find People query.
There is another one could solve my problem?
My suggestion would be to try looking into a library, such as TweetSharp that already has wrappers around a substantial amount of the Twitter API methods. If that doesn't work for you, consider writing your own wrapper around the methods you need using libraries such as HammockRest or RestSharp that have support for working with http, oauth and other such features that may assist you.
Looking better in Twitterizer I found what I need, perform Search People, and other functions. I recommend Twitterizer to use twitter in ASP.NET MVC 3, ´cause the oAuth process has a better code.
Here the Sample code to peform Search People in twitter using Twitterizer :
UserSearchOptions options = new UserSearchOptions();
options.NumberPerPage = 40;
options.Page = 1;
TwitterResponse<TwitterUserCollection> usersResponse = TwitterUser.Search(tokens,pesquisa.Conteudo,options);
if (usersResponse.Result == RequestResult.Success)
{
StringBuilder list = new StringBuilder();
foreach (var u in usersResponse.ResponseObject)
{
list2.Append(u.Name + "-" + u.Id);
}
ViewBag.Result_Twitter = list.ToString();
}

MVC2 - Consume RSS feed with RDF and namespace http://www.w3.org/1999/02/22-rdf-syntax-ns#'

I' trying to read the feed for the Washington Departmene of Fish and Wildlife, and keep etting this error:
The element with name 'RDF' and
namespace
'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
is not an allowed feed format.
Here's the code from RssController:
public virtual ActionResult Index()
{
string feedUrl = #"http://wdfw.wa.gov/news/newsrss.php";
using (XmlReader reader = XmlReader.Create(feedUrl))
{
**SyndicationFeed rss = SyndicationFeed.Load(reader);**
return View(rss);
}
}
I've done seveal RSS applications but nave never ran into this issue. Anyone got any concrete ways of being able to parse this in ASP.NET MVC 2? (the lines with asterics are where the exception happens.
There's no support for RSS 1.0. Example of how to roll your own support here : https://web.archive.org/web/20211020140320/https://www.4guysfromrolla.com/articles/031809-1.aspx
This may not be applicable to you as it sounds like you are only interested in RSS, but if you want RDF support for your application (RSS 1.0 uses RDF/XML to encode it's data) then you could try my library dotNetRDF.
I suspect that a full blown RDF API is probably overkill though judging from your question.

Resources