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 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)