【问题标题】:C# equivalent of file_get_contents (PHP)C# 相当于 file_get_contents (PHP)
【发布时间】:2011-08-27 14:22:03
【问题描述】:

作为(OAuthException) (#15) The method you are calling must be called with an app secret signed session 的后续行动,我想知道file_get_contents() 的等价物是什么。我尝试了以下方法,但出现illegal characters in path 错误。

    public ActionResult About()
    {
        var fb = new FacebookWebClient(FacebookWebContext.Current);

        var tokenUrl = "https://graph.facebook.com/oauth/access_token?client_id=" + FacebookWebContext.Current.Settings.AppId + "&client_secret=" + FacebookWebContext.Current.Settings.AppSecret + "&grant_type=client_credentials";
        var objReader = new StreamReader(tokenUrl);
        string sLine = "";
        var arrayList = new ArrayList();

        while (sLine != null)
        {
            sLine = objReader.ReadLine();
            if (sLine != null)
                arrayList.Add(sLine);
        }
        objReader.Close();
        var appToken = arrayList.ToString();

        dynamic result = fb.Post(string.Format("{0}/accounts/test-users", FacebookWebContext.Current.Settings.AppId),
            new { installed = false, permissions = "read_stream", access_token = appToken });
        return Content(result.ToString());
    }

我也尝试了System.IO.File.ReadAllText(tokenUrl),但我得到了同样的错误。有什么我可以做的吗?

我什至不确定它是否会起作用,但至少我可以尝试......

【问题讨论】:

  • 那个方法已经严重过时了
  • @Tim,该方法声称使用 URL,但仅检查 http:。所以 https 或 ftp 不起作用。
  • 您应该使用List<string> 而不是ArrayList
  • @svick:是的,你对 https 和 ftp 的看法是正确的,实际上我自己添加了这些,因为 Facebook 现在使用 https。如果您知道更智能的实现,请分享:)

标签: c# facebook facebook-c#-sdk inputstream


【解决方案1】:

您可以使用WebClient.DownloadString 从 URL 下载文本。 WebClient 也是supports authentication

此外,要将字符串拆分为可以使用的行:

string test;
string[] lines = test.Split('\n');

【讨论】:

  • 你链接到的页面中的实现多么糟糕——只支持 HTTP,假设其他所有内容都是本地文件,并假设存在最古老和最无用的编码(即 ASCII)!
  • 原发帖人使用的是http(s),使用的是网站返回的编码。这是一个非常易于使用的实现。
【解决方案2】:

要使用 oauth/access_token 或任何与 oauth 相关的方法,请使用 FacebookOAuthClient 而不是 FacebookClient 或 FacebookClient。

FacebookOAuthClient.GetApplicationAccessToken(..)
FacebookOAuthClient.ExchangeCodeForAccessToken(..)

【讨论】:

  • 感谢您的回复。我很感激。顺便说一句,你们打算在 codeplex 页面上放更多文档吗?我相信有些文档已经过时了?伟大的工作仍然:)
猜你喜欢
  • 2013-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多