1

有人知道 VB 或 C# .NET 的等效代码是什么吗?

下面的代码是在 PHP 中运行 fql 查询的示例,但需要 C# 或 VB 中 .NET 的示例或等效代码。

任何帮助表示赞赏。

<?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>';
?>
4

1 回答 1

1

我将您提供的 php 代码转换为 c#,但由于 api 的可用性而未进行测试。希望这会帮助你。

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;
}
于 2012-04-17T18:02:52.933 回答