2011-04-15 70 views
0

經過大約一週的搜索並嘗試失敗。我終於從代碼中生成了正確的Facebook訪問令牌。連接到具有訪問令牌的新Facebook客戶端

但是現在我又遇到了另一個問題。使用該訪問令牌來獲取用戶數據。每次我使用令牌來獲取數據,將其與異常返回!String.IsNullOrEmpty(accessToken).

完整堆跟蹤:


Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Diagnostics.Contracts.__ContractsRuntime+ContractException: Precondition failed: !string.IsNullOrEmpty(accessToken) 

源錯誤:

的執行過程中生成了未處理的異常的當前網絡請求。關於異常的來源和位置的信息可以使用下面的異常堆棧跟蹤來標識。

堆棧跟蹤:

[ContractException: Precondition failed: !string.IsNullOrEmpty(accessToken)] 
    Facebook.Web.FacebookWebClient..ctor(String accessToken) +137 
    Facebook_Photo_App._Default.Button1_Click(Object sender, EventArgs e) in Default.aspx.cs:105 
    System.EventHandler.Invoke(Object sender, EventArgs e) +0 
    System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118 
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112 
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563 

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1 

我的代碼:

要生成的頁面加載訪問托克:

string code; 
      code = Request.QueryString["code"]; 
     //TextBox1.Text = code; 

     string token; 

     string url = "https://graph.facebook.com/oauth/access_token?client_id=XXX&redirect_uri=http://apps.facebook.com/dutch-vegas/&client_secret=XXX&code=" + code; 

     WebRequest request = WebRequest.Create(url); 
     WebResponse response = request.GetResponse(); 
     StreamReader reader = new StreamReader(response.GetResponseStream()); 
     token = reader.ReadToEnd(); 
     string decodedtoken = token; 

     TextBox1.Text = decodedtoken; 

     decodedtoken = decodedtoken.Replace("access_token=", ""); 

     int start = decodedtoken.IndexOf("&"); 
     int count = decodedtoken.Length - decodedtoken.IndexOf("&"); 
     decodedtoken = decodedtoken.Remove(start, count); 

在按鈕實現訪問令牌取用戶數據

var client = new FacebookClient(decodedtoken); 


     dynamic me = client.Get("me"); 
     string email = me.email; 
     string firstname = me.first_name; 
     string lastname = me.last_name; 
     string birthday = me.birthday; 

     TextBox1.Text = email; 

一切似乎都正確生成。即使在獲取了錯誤的訪問令牌(這比它假設的短得多)之後,我已經整理了一個星期。現在我卡住了。

請幫幫我。我搜索了無數個小時來嘗試解決這個問題。

我也注意到在C#SDK的Facebook您可以爲

FacebookWebClient, FacebookApp, FacebookClient創建多個實例。

哪一個我可以使用,這可能是我的問題的一部分?

非常感謝提前。 J

回答