C#等价于file_get_contents(PHP)

时间:2011-05-17 21:26:50

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

作为(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),我也遇到了同样的错误。我有什么可以做的吗?

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

2 个答案:

答案 0 :(得分:4)

您可以使用WebClient.DownloadString从网址下载文字。 WebClient也supports authentication

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

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

答案 1 :(得分:1)

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

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