2011-05-17 127 views
2

作爲(OAuthException) (#15) The method you are calling must be called with an app secret signed session的後續操作,我想知道file_get_contents()的等價物是什麼。我嘗試了以下,但我得到illegal characters in path錯誤。C#等價於file_get_contents(PHP)

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

http://www.devprise.com/2006/07/14/c-method-to-mimic-php-file_get_contents/ – 2011-05-17 21:29:09

+0

該方法在很大程度上過時 – Bas 2011-05-17 21:32:07

+1

@Tim,該方法宣稱與網址,但只檢查'http:'。所以HTTPS或FTP不起作用。 – svick 2011-05-17 22:56:02

回答

1

要使用OAuth /或的access_token有關OAuth的東西任何方法使用FacebookOAuthClient不FacebookClient或FacebookClient。

FacebookOAuthClient.GetApplicationAccessToken(..) 
FacebookOAuthClient.ExchangeCodeForAccessToken(..) 
+0

感謝您的回覆。我很感激。順便說一下,你們是否打算在Codeplex頁面上放置更多文檔?我相信一些文檔已經過時了。不錯的工作:) – Kassem 2011-05-18 10:15:23

4

您可以使用WebClient.DownloadString從URL下載的文本。 WebClient也supports authentication

此外,您字符串分割成線,你可以使用:

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

鏈接到頁面的廢話實現 - 僅支持HTTP,假定其他所有內容都是本地文件,並且假定存在最古老和無用的編碼(即ASCII )! – Timwi 2011-05-18 00:05:15

+1

原始海報正在使用http(s),並使用網站返回的編碼。這是一個非常易於使用的實現。 – Bas 2011-05-18 12:04:07