2013-04-26 43 views
-1

我是新來的Facebook C#sdk。我遵循this link的教程。使用Facebook錯誤使用WPF Web瀏覽器的C#sdk

我創建的顯示在登錄後的用戶名的應用程序下面是我的代碼:

public partial class MainWindow : Window 
{ 
    private string appId = "appid"; 
    private string extenededPermissions = "offline_access,publish_stream"; 
    private Uri loginUrl = null; 
    private string accessToken = null; 
    private string userName = null; 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    /// <summary> 
    /// Function to get the login url 
    /// with the requested permissions 
    /// </summary> 
    private void GetLoginUrl() 
    { 
     dynamic parameters = new ExpandoObject(); 
     // add the client id 
     parameters.client_id = appId; 
     // add the redirect uri 
     parameters.redirect_uri = "https://www.facebook.com/connect/login_success.html"; 
     // requested response 
     parameters.response_type = "token"; 
     // type of display 
     parameters.display = "popup"; 
     // If extended permissions are present 
     if (!string.IsNullOrWhiteSpace(extenededPermissions)) 
      parameters.scope = extenededPermissions; 
     // Create the login url 
     Facebook fc = new FacebookClient(); 
     loginUrl = fc.GetLoginUrl(parameters); 
    } 

    private void WindowLoaded(object sender, RoutedEventArgs e) 
    { 
     // get the login url 
     GetLoginUrl(); 
     // Navigate to that page 
     webBrowser.Navigate(loginUrl); 
    } 

    private void webBrowser_Navigated(object sender, NavigationEventArgs e) 
    { 
     var fc = new FacebookClient(); 
     FacebookOAuthResult fr; 
     // Check the returned url 
     if (fc.TryParseOAuthCallbackUrl(e.Uri, out fr)) 
     { 
      // check if authentication is success or not 
      if (fr.IsSuccess) 
      { 
       getUserName(out userName); 
      } 
      else 
      { 
       var errorDes = fr.ErrorDescription; 
       var errorReason = fr.ErrorReason; 
      } 
     } 
     else 
     { 

     } 
    } 
    private void getUserName(out string name) 
    { 
     var fb = new FacebookClient(accessToken); 
     // Get the user details 
     dynamic result = fb.Get("me"); 
     // Get the user name 
     name = result.name; 
     MessageBox.Show("Hai " + name + ",Welcome to my App"); 
    } 

} 

我的問題是與FacebookOAuthResult

private void webBrowser_Navigated(object sender, NavigationEventArgs e) 
    { 
     var fc = new FacebookClient(); 
     FacebookOAuthResult fr; 
     // Check the returned url 
     if (fc.TryParseOAuthCallbackUrl(e.Uri, out fr)) 
     { 
      // check if authentication is success or not 
      if (fr.IsSuccess) 
      { 
       getUserName(out userName); 
      } 
      else 
      { 
       var errorDes = fr.ErrorDescription; 
       var errorReason = fr.ErrorReason; 
      } 
     } 
     else 
     { 

     } 
    } 

我登錄後重定向到redirect_uri。但fc.TryParseOAuthCallbackUrl(e.Uri, out fr)失敗,雖然webbrowser重定向到認證成功頁面。

所以我無法獲得訪問令牌。我的代碼中存在什麼問題?

回答

0

沒關係我找到了解決方案..謝謝question的答案!

我已經加入了的WinForms Web瀏覽器控件到WPF和認證working.The問題是WPF Web瀏覽器。它簡單地在#標記之後省略了url所以parseurl將無法驗證它。

下面是修改後的代碼..

 private void WindowLoaded(object sender, RoutedEventArgs e) 
     { 
      // create the windows form host 
     System.Windows.Forms.Integration.WindowsFormsHost sample = 
         new System.Windows.Forms.Integration.WindowsFormsHost(); 
     // create a new web browser 
     webBrowser = new System.Windows.Forms.WebBrowser(); 
     // add it to winforms 
     sample.Child = webBrowser; 
     // add it to wpf 
     canvas1.Children.Add(sample); 

     webBrowser.Navigated += webBrowser_Navigated; 
     webBrowser.Navigate(loginURL); 
     } 

    void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e) 
     { 
     // do the authentication 
     var fc = new FacebookClient(); 
     FacebookOAuthResult fr; 
     // Check the returned url 
     if (fc.TryParseOAuthCallbackUrl(e.Url, out fr)) 
     { 
      // check if authentication is success or not 
      if (fr.IsSuccess) 
      { 
       accessToken = fr.AccessToken; 
       // Actions to do 

      } 
      else 
      { 
       var errordes = fr.ErrorDescription; 
       var errorreason = fr.ErrorReason; 
      } 
     } 
     else 
     { 
      //Not a valid url 
     } 
     } 

問題是解決了!

1

這不回答這個問題,但我看到您要求提供offline_access權限。 Facebook在某個時間之前刪除了offline_access。相反,你需要一個擴展訪問令牌。你可以通過交換你試圖獲得的訪問令牌來獲得它,以獲得更長的訪問令牌。他們持續約2-3個月之後,你必須得到一個新的。

+0

但即使在刪除offline_access之後,問題仍然存在。 – Naren 2013-04-26 08:57:17