与Facebook的身份验证移动服务在Azure中 [英] Authenticating with Facebook for Mobile Services in Azure
问题描述
我有天青移动服务Facebook验证的麻烦。
I am having trouble with facebook authentication for Mobile Services in Azure.
要更具体,我已经有一个使用的Facebook C#SDK 一个应用程序,它工作正常。我可以登录,获取我的朋友等名单。我想用这个SDK保留,但我也想为Azure的移动服务进行身份验证。
所以,我的计划是,登录与Facebook C#SDK(如我今天已经这样做),获得身份验证令牌,并将其传递给的 MobileServiceClient.LoginAsync() - 功能。这样的话,我仍然可以在Facebook的C#SDK所有不错的功能,并且还使用了内置的验证系统中的移动服务Azure上。
To be more specific, I already have an application that is using Facebook C# SDK and it works fine. I can log on, fetch list of my friends and so. I want to keep using this SDK, but I also want to authenticate for Azure Mobile Service. So, my plan was, log on with Facebook C# SDK (as I already do today), get the authentication token, and pass it to the MobileServiceClient.LoginAsync() - function. That way, I can still have all the nice features in Facebook C# SDK, and also use the built in authentication system in Mobile Services for Azure.
var client = new FacebookClient();
dynamic parameters = new ExpandoObject();
parameters.client_id = App.FacebookAppId;
parameters.redirect_uri = "https://www.facebook.com/connect/login_success.html";
parameters.response_type = "token";
parameters.display = "popup";
var loginUrl = client.GetLoginUrl(parameters);
WebView.Navigate(loginUrl);
当加载完成后,跟随着被执行:
When load is complete, followin is executed:
FacebookOAuthResult oauthResult;
if (client.TryParseOAuthCallbackUrl(e.Uri, out oauthResult) && oauthResult.IsSuccess)
{
var accessToken = oauthResult.AccessToken;
var json = JsonObject.Parse("{\"authenticationToken\" : \"" + accessToken + "\"}");
var user = await App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.Facebook, json);
}
不过,我当我打电话code以上的最后一行得到这个异常:
MobileServiceInvalidOperationException,错误:开机自检Facebook的登录请求必须指定在请求主体的访问令牌
However, I get this exception when I call the last line of code above: MobileServiceInvalidOperationException, "Error: The POST Facebook login request must specify the access token in the body of the request."
我无法找到如何格式化的accessToken任何信息,我已经尝试了很多不同的密钥(而不是authenticationToken为你我的样品中看到的)的。我也试图只是为了传递的accessToken字符串,但没有什么似乎工作。
另外,如果我使用MobileServiceClient.LoginAsync()制作一个全新的登录,它工作得很好,但它似乎愚蠢强制用户登录两次。
I cannot find any information on how to format the accesstoken, I have tried a lot of different keys (instead of "authenticationToken" as you see in my sample). I also have tried just to pass the accesstoken string, but nothing seem to work. Also, if I use the MobileServiceClient.LoginAsync() for making a brand new login, it works just fine, but it seem silly to force users to log on twice.
任何帮助是极大AP preciated!
Any help is greatly appreciated!
推荐答案
预计为对象的格式为{的access_token ,最实际的访问令牌}。一旦登录使用Facebook的SDK完成,令牌返回与该名称的片段,所以这是在Azure移动服务所期待的。
The format expected for the object is {"access_token", "the-actual-access-token"}. Once the login is completed using the Facebook SDK, the token is returned in the fragment with that name, so that's what the Azure Mobile Service expects.
顺便说一句,这是一个code这是我写的,根据您的片断,其工作方式。它应该处理好失败的情况下,虽然,但令牌格式,这应该是足够的。
BTW, this is a code which I wrote, based on your snippet, which works. It should handle failed cases better, though, but for the token format, this should be enough
private void btnLoginFacebookToken_Click_1(object sender, RoutedEventArgs e)
{
var client = new Facebook.FacebookClient();
dynamic parameters = new ExpandoObject();
parameters.client_id = "MY_APPLICATION_CLIENT_ID";
parameters.redirect_uri = "https://www.facebook.com/connect/login_success.html";
parameters.response_type = "token";
parameters.display = "popup";
var uri = client.GetLoginUrl(parameters);
this.webView.LoadCompleted += webView_LoadCompleted;
this.webView.Visibility = Windows.UI.Xaml.Visibility.Visible;
this.webView.Navigate(uri);
}
async void webView_LoadCompleted(object sender, NavigationEventArgs e)
{
AddToDebug("NavigationMode: {0}", e.NavigationMode);
AddToDebug("Uri: {0}", e.Uri);
string redirect_uri = "https://www.facebook.com/connect/login_success.html";
bool close = (e.Uri.ToString().StartsWith(redirect_uri));
if (close)
{
this.webView.LoadCompleted -= webView_LoadCompleted;
this.webView.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
string fragment = e.Uri.Fragment;
string accessToken = fragment.Substring("#access_token=".Length);
accessToken = accessToken.Substring(0, accessToken.IndexOf('&'));
JsonObject token = new JsonObject();
token.Add("access_token", JsonValue.CreateStringValue(accessToken));
try
{
var user = await MobileService.LoginAsync(MobileServiceAuthenticationProvider.Facebook, token);
AddToDebug("Logged in: {0}", user.UserId);
}
catch (Exception ex)
{
AddToDebug("Error: {0}", ex);
}
}
}
这篇关于与Facebook的身份验证移动服务在Azure中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!