-1

我是 facebook 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)尽管网络浏览器重定向到身份验证成功页面,但失败了。

所以我无法获得访问令牌。我的代码中的问题可能是什么?

4

2 回答 2

1

这不能回答问题,但我看到您要求获得offline_access 权限。Facebook 前段时间删除了 offline_access。相反,您需要一个扩展访问令牌。您可以通过将尝试获取的访问令牌交换为扩展令牌来获得它。它们持续大约 2-3 个月,之后您必须获得一个新的。

于 2013-04-26T07:58:55.360 回答
0

没关系,我找到了解决方案..感谢问题的答案

我已将Winforms Web 浏览器控件添加到 wpf 并且身份验证正在工作。问题出在WPF Web 浏览器上。它只是在 # token 之后省略了 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
         }
      }

问题已经解决了!!

于 2013-04-27T11:07:07.833 回答