Reputation: 25
Does anyone what the equivalent code would be for VB or C# .NET?
Code below is an example to run a fql query in PHP, but need an example or equivalent for .NET in either C# or VB.
Any help appreciated.
<?php
$app_id = 'YOUR_APP_ID';
$app_secret = 'YOUR_APP_SECRET';
$my_url = 'POST_AUTH_URL';
$code = $_REQUEST["code"];
//auth user
if(empty($code)) {
$dialog_url = 'https://www.facebook.com/dialog/oauth?client_id='
. $app_id . '&redirect_uri=' . urlencode($my_url) ;
echo("<script>top.location.href='" . $dialog_url . "'</script>");
}
//get user access_token
$token_url = 'https://graph.facebook.com/oauth/access_token?client_id='
. $app_id . '&redirect_uri=' . urlencode($my_url)
. '&client_secret=' . $app_secret
. '&code=' . $code;
$access_token = file_get_contents($token_url);
// Run fql query
$fql_query_url = 'https://graph.facebook.com/'
. '/fql?q=SELECT+uid2+FROM+friend+WHERE+uid1=me()'
. '&' . $access_token;
$fql_query_result = file_get_contents($fql_query_url);
$fql_query_obj = json_decode($fql_query_result, true);
//display results of fql query
echo '<pre>';
print_r("query results:");
print_r($fql_query_obj);
echo '</pre>';
// Run fql multiquery
$fql_multiquery_url = 'https://graph.facebook.com/'
. 'fql?q={"all+friends":"SELECT+uid2+FROM+friend+WHERE+uid1=me()",'
. '"my+name":"SELECT+name+FROM+user+WHERE+uid=me()"}'
. '&' . $access_token;
$fql_multiquery_result = file_get_contents($fql_multiquery_url);
$fql_multiquery_obj = json_decode($fql_multiquery_result, true);
//display results of fql multiquery
echo '<pre>';
print_r("multi query results:");
print_r($fql_multiquery_obj);
echo '</pre>';
?>
Upvotes: 1
Views: 1114
Reputation: 6571
I converted your provided php code to c# but not tested due to availability of api. Hope this will help you.
private string TestApp()
{
StringBuilder str = new StringBuilder();
string app_id = "YOUR_APP_ID";
string app_secrete = "YOUR_APP_SECRET";
string my_url = "POST_AUTH_URL";
string code="";
if(Request.Params["code"]!=null)
code = Request.Params["code"].ToString();
string dialog_url = "";
if(code !="")
{
dialog_url = "https://www.facebook.com/dialog/oauth?client_id=" + app_id + "&redirect_uri=" + Server.UrlEncode(my_url);
str.Append("<script>top.location.href=" + dialog_url + "'</script>");
}
string token_url = "https://graph.facebook.com/oauth/access_token?client_id=" + app_id + "&redirect_uri=" + Server.UrlEncode(my_url) + "&client_secret=" + app_secrete + "&code=" + code;
string access_token = file_get_contents(token_url);
string fql_query_url = "https://graph.facebook.com/fql?q=SELECT+uid2+FROM+friend+WHERE+uid1=me()&" + access_token;
string fql_query_result = file_get_contents(fql_query_url);
string fql_query_obj = Newtonsoft.Json.JsonConvert.SerializeObject(fql_query_result, Newtonsoft.Json.Formatting.Indented);
str.Append("<pre>\n");
str.Append("Query Results: " + fql_query_obj);
str.Append("</pre>\n");
string fql_multiquery_url = "https://graph.facebook.com/fql?q={\"all+friends\":\"SELECT+uid2+FROM+friend+WHERE+uid1=me()\",my+name\":\"SELECT+name+FROM+user+WHERE+uid=me()\"}&" + access_token;
string fql_multiquery_result = file_get_contents(fql_multiquery_url);
string fql_multiquery_obj = Newtonsoft.Json.JsonConvert.SerializeObject(fql_multiquery_result, Newtonsoft.Json.Formatting.Indented);
str.Append("<pre>\n");
str.Append("multi query results: " + fql_multiquery_obj);
str.Append("</pre>\n");
return str.ToString();
}
protected string file_get_contents(string fileName)
{
string sContents = string.Empty;
string me = string.Empty;
try
{
if (fileName.ToLower().IndexOf("http:") > -1)
{ // URL
System.Net.WebClient wc = new System.Net.WebClient();
byte[] response = wc.DownloadData(fileName);
sContents = System.Text.Encoding.ASCII.GetString(response);
}
else
{ // Regular Filename
System.IO.StreamReader sr = new System.IO.StreamReader(fileName);
sContents = sr.ReadToEnd();
sr.Close();
}
}
catch { sContents = "unable to connect to server "; }
return sContents;
}
Upvotes: 1