Facebook .NET客户端SDK是否支持通过AppStudio生成的通用应用程序/应用程序? [英] Does the Facebook .NET client SDK support universal apps / apps generated via AppStudio?

查看:151
本文介绍了Facebook .NET客户端SDK是否支持通过AppStudio生成的通用应用程序/应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过Microsoft的AppStudio创建了一个通用的应用程序。
我尝试通过遵循Scrumptious教程向应用程序添加Facebook身份验证( http:// facebooksdk.net/docs/phone/tutorial/ )。



当我在手机上运行应用程序时,我无法访问facebook登录页面,因为以下行:等待App.FacebookSessionClient.LoginAsync(user_about_me,read_stream);



总是导致以下异常:



System.NotImplementedException:未实现

在Windows.Security.Authentication.Web.WebAuthenticationBroker.AuthenticateAsync (WebAuthenticationOptions选项,Uri requestUri,Uri callbackUri)
在Facebook.Client.FacebookSessionClient.d__24.MoveNext()



源这是在 FacebookSessionClient.cs facebook-client包)中的这个调用:
var result = await WebAuthenticationBro ker.AuthenticateAsync(options,startUri,endUri);



似乎这个功能没有为手机实现。我仍然想知道这个可能性,这个引用完全相同的代码的turial是可行的。

解决方案

没有实现为8.1。如果要在8.1中使用Facebook身份验证,您可以使用以下方法:



在您的App类中:

  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)
{
//处理不成功的身份验证
}
else
{
//进程成功身份验证
var accessToken = result.AccessToken;
}
}
}

//验证方法,当您点击Facebook身份验证按钮
时应调用此方法public void AuthenticateAndContinue()
{
var loginUrl = GetLoginUrl();
WebAuthenticationBroker.AuthenticateAndContinue(loginUrl,new Uri(RedirectUrl));
}

private Uri GetLoginUrl()
{
var parameters = new Dictionary< string,object>();
参数[client_id] =YourFacebookApplicationId;
参数[redirect_uri] = RedirectUrl;
参数[response_type] =令牌;
参数[display] =touch;
参数[mobile] = true;
参数[scope] = String.Join(,,Permissions);

var facebookClient = new FacebookClient();

return facebookClient.GetLoginUrl(parameters);
}

我把所有东西放在一个地方只是为了比较目的,最好分开fb认证逻辑。
您可以在这里找到此方法 MSDN Windows Phone 8.1 Web验证示例< a>


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/docs/phone/tutorial/).

When I run the app on my phone, I can never get to the facebook login page, because the following line: await App.FacebookSessionClient.LoginAsync("user_about_me,read_stream");

always results in the following exception:

System.NotImplementedException: Not implemented
at Windows.Security.Authentication.Web.WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions options, Uri requestUri, Uri callbackUri) at Facebook.Client.FacebookSessionClient.d__24.MoveNext()

The source of the exception is this call in the FacebookSessionClient.cs (facebook-client package): var result = await WebAuthenticationBroker.AuthenticateAsync(options, startUri, endUri);

It seems that this function is not implemented for the phone. I am still wondering how it is possible that the turial, which refers to the exact same code would work.

解决方案

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

这篇关于Facebook .NET客户端SDK是否支持通过AppStudio生成的通用应用程序/应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆