2011-02-26 44 views
1

我試圖在現有的ASP.NET webforms應用程序中使用Facebook C#SDK。我的意圖是允許用戶通過單擊Facebook鏈接繞過表單身份驗證。該鏈接的網址如下:..(400)調用Facebook C#SDK時的錯誤請求

<a href="https://www.facebook.com/dialog/oauth?client_id=0000000000000&redirect_uri=http://localhost:2708/fboauth.aspx&scope=offline_access">facebook</a> 

Facebook的處理請求,然後重定向回fboauth.aspx其背後都有下面的代碼...

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Facebook; 
using System.Net; 
using System.IO; 

namespace StorageByMail20 
{ 
    public partial class fboauth : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      string code; 
      code = Request.QueryString["code"]; 
      Label1.Text = code; 


     string token; 

     string url = "https://graph.facebook.com/oauth/access_token?client_id=000000000000000&redirect_uri=http://localhost:2708/fboauth.aspx&client_secret=000000000000000000000000&code=" + code; 

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


     Facebook.FacebookAPI api = new Facebook.FacebookAPI(decodedtoken); 

     JSONObject result = api.Get("/first_name"); 

     string name = result.Dictionary["first_name"].String; 


     Label2.Text = token; 


     Label3.Text = name; 

    } 
} 

}

我在本頁嘗試做的事情如下:

  1. 解析授權碼 Facebook的響應
  2. 使用授權碼來從Facebook
  3. 獲得 認證令牌
  4. 使用 認證碼中檢索有關用戶的信息 撥打電話JSON(姓名,電子郵件 等)
  5. 將該信息發送到頁面(最終 我會做一些更有趣的事情 的數據。現在我只是 試圖讓API交互 正常工作)。

一切都按照預期的方式工作到JSON調用。如果我註釋掉那條線,我很好。否則,Facebook返回(400)錯誤請求錯誤。有人能發現我出錯的地方嗎?

注意:有一個已知的錯誤報告在這裏https://github.com/facebook/csharp-sdk/issues,涉及訪問令牌的編碼問題。我認爲我已經解決了與我使用UrlDecode。另外請注意,我試圖使用的SDK是來自Facebook的官方SDK(目前在Alpha中)。還有一些其他的C#SDK是由社區創建的。

這是我在研究我的問題時發現的一些特別有用的文章。

onishimura.com/2010/08/11/facebook-c-and-asp-net-mvc-code-samples-for-friends-list-activities-list-and-wall-posts/

multitiered.wordpress.com/2010/08/05/getting-started-with-the-facebook-c-sharp-sdk/

*我從上面的鏈接中刪除了「http://」。很明顯,我需要更多的聲譽點才能讓我包含兩個以上的鏈接。對不起人!

回答

2

您在Github上鍊接的SDK不是Facebook C#SDK,它是一個非常古老且非常錯誤的提交,Facebook提交,然後單獨離開。有一個更多的最新和廣泛使用的項目稱爲Facebook C#SDK here

看來,你正在做大量的腿工作,可以很容易地處理無論是registration pluginjavascript sdk(這是開發Facebook連接的網站時的慣例。

如果你正在做任何更多的Facebook調用服務器端我強烈建議您使用鏈接的C#SDK。看看這個示例,看看你需要做些什麼編碼。

+0

感謝您的回覆。我通過Facebook的一位開發人員閱讀了一篇舊文章(http://developers.facebook.com/blog/post/395/),並認爲Github項目是最好的版本。我感謝你的清理。 – hughesdan

+0

你有什麼問題需要你幫助嗎?否則,如果我的答案幫助,我將不勝感激,如果你標記爲這樣! –

+0

關於您使用JavaScript SDK的建議,我避免出於安全問題。我的印象是,Javascript方法比完全在服務器端處理的方法更安全。你會同意嗎?還是我不必要地讓我的生活更加困難? – hughesdan