dongxianrang9269 2012-04-17 15:27
浏览 65
已采纳

使用ASP.NET的FQL - 将PHP示例转换为.NET

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>';
?>
  • 写回答

1条回答 默认 最新

  • dqys98341 2012-04-17 18:02
    关注

    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>
    ");
        str.Append("Query Results: " + fql_query_obj);
        str.Append("</pre>
    ");
    
        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>
    ");
        str.Append("multi query results: " + fql_multiquery_obj);
        str.Append("</pre>
    ");
    
        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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?