Does the Facebook .NET client SDK support universal apps / apps generated via AppStudio?

前端 未结 1 897
面向向阳花
面向向阳花 2021-01-07 07:09

I created a universal app via Microsoft\'s AppStudio. I tried adding Facebook authentication to the app by following the \'Scrumptious tutorial\' (http://facebooksdk.net/doc

相关标签:
1条回答
  • 2021-01-07 07:43

    It's not implemented for 8.1 yet. If you want to use Facebook authentication in 8.1 you can use the following approach:

    In your App class:

    private const string RedirectUrl = "https://www.facebook.com/connect/login_success.html";
    private static readonly IReadOnlyCollection<string> Permissions = new[] { "email", "offline_access" };
    
    protected override void OnActivated(IActivatedEventArgs args)
    {
        base.OnActivated(args);
        var continuationActivatedEventArgs = args as IContinuationActivatedEventArgs;
        if (continuationActivatedEventArgs == null)
            return;
        var webAuthenticationResult = ((WebAuthenticationBrokerContinuationEventArgs)continuationActivatedEventArgs).WebAuthenticationResult;
        if (webAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success)
        {
            var facebookClient = new FacebookClient();
            var result = facebookClient.ParseOAuthCallbackUrl(new Uri(webAuthenticationResult.ResponseData));
            if (!result.IsSuccess)
            {
                // Process unsuccessful authentication
            }
            else
            {
                // Process successful authentication
                var accessToken = result.AccessToken;
            }
        }
    }
    
    // Authentication method, this method should be invoked when you click Facebook authentication button
    public void AuthenticateAndContinue()
    {
        var loginUrl = GetLoginUrl();
        WebAuthenticationBroker.AuthenticateAndContinue(loginUrl, new Uri(RedirectUrl));
    }
    
    private Uri GetLoginUrl()
    {
        var parameters = new Dictionary<string, object>();
        parameters["client_id"] = "YourFacebookApplicationId";
        parameters["redirect_uri"] = RedirectUrl;
        parameters["response_type"] = "token";
        parameters["display"] = "touch";
        parameters["mobile"] = true;
        parameters["scope"] = String.Join(",", Permissions);
    
        var facebookClient = new FacebookClient();
    
        return facebookClient.GetLoginUrl(parameters);
    }
    

    I put everything in one place just for example purposes, it's better to separate fb authentication logic. You can find this approach here MSDN Windows Phone 8.1 Web Authentication samples

    0 讨论(0)
提交回复
热议问题