如何使用 xamarin.auth 登录 Facebook 并以 xamarin 形式获取用户的个人资料信息
How to facebook login and get user's profile info in xamarin forms using xamarin.auth
我使用 Xamarin 表单开发了一个跨平台应用程序。我被困在一个 facebook 问题上。所以我的问题是如何获取 facebook 用户的个人资料信息。我只能获得 "Id" 和 "Name" 的用户。但是要获取用户的完整详细信息(即电子邮件、个人资料照片、性别、出生日期等...)
这是我的代码:
protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
{
base.OnElementChanged(e);
// this is a ViewGroup - so should be able to load an AXML file and FindView<>
var activity = this.Context as Activity;
var auth = new OAuth2Authenticator (
clientId: App.Instance.OAuthSettings.ClientId, // your OAuth2 client id
scope: App.Instance.OAuthSettings.Scope, // The scopes for the particular API you're accessing. The format for this will vary by API.
authorizeUrl: new Uri (App.Instance.OAuthSettings.AuthorizeUrl), // the auth URL for the service
redirectUrl: new Uri (App.Instance.OAuthSettings.RedirectUrl)); // the redirect URL for the service
auth.AllowCancel = true;
auth.Completed += LoginComplete;
activity.StartActivity (auth.GetUI(activity));
}
public async void LoginComplete(object sender, AuthenticatorCompletedEventArgs e)
{
// We presented the UI, so it's up to us to dismiss it.
// DismissViewController(true, null);
if (!e.IsAuthenticated)
{
Console.WriteLine("Not Authorised");
return;
}
var accessToken = e.Account.Properties["access_token"].ToString();
var expiresIn = Convert.ToDouble(e.Account.Properties["expires_in"]);
var expiryDate = DateTime.Now + TimeSpan.FromSeconds(expiresIn);
// Now that we're logged in, make a OAuth2 request to get the user's id.
var request = new OAuth2Request("GET", new Uri("https://graph.facebook.com/me"), null, e.Account);
var response = await request.GetResponseAsync();
var obj = JObject.Parse(response.GetResponseText());
var id = obj["id"].ToString().Replace("\"", ""); // Id has extraneous quotation marks
// var user = await ParseFacebookUtils.LogInAsync(id, accessToken, expiryDate);
}
最后我通过在请求中添加字段得到了解决方案 URL。
var request = new OAuth2Request("GET", new Uri("https://graph.facebook.com/me?fields=email,first_name,last_name,gender,picture"), null, e.Account);
我使用 Xamarin 表单开发了一个跨平台应用程序。我被困在一个 facebook 问题上。所以我的问题是如何获取 facebook 用户的个人资料信息。我只能获得 "Id" 和 "Name" 的用户。但是要获取用户的完整详细信息(即电子邮件、个人资料照片、性别、出生日期等...)
这是我的代码:
protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
{
base.OnElementChanged(e);
// this is a ViewGroup - so should be able to load an AXML file and FindView<>
var activity = this.Context as Activity;
var auth = new OAuth2Authenticator (
clientId: App.Instance.OAuthSettings.ClientId, // your OAuth2 client id
scope: App.Instance.OAuthSettings.Scope, // The scopes for the particular API you're accessing. The format for this will vary by API.
authorizeUrl: new Uri (App.Instance.OAuthSettings.AuthorizeUrl), // the auth URL for the service
redirectUrl: new Uri (App.Instance.OAuthSettings.RedirectUrl)); // the redirect URL for the service
auth.AllowCancel = true;
auth.Completed += LoginComplete;
activity.StartActivity (auth.GetUI(activity));
}
public async void LoginComplete(object sender, AuthenticatorCompletedEventArgs e)
{
// We presented the UI, so it's up to us to dismiss it.
// DismissViewController(true, null);
if (!e.IsAuthenticated)
{
Console.WriteLine("Not Authorised");
return;
}
var accessToken = e.Account.Properties["access_token"].ToString();
var expiresIn = Convert.ToDouble(e.Account.Properties["expires_in"]);
var expiryDate = DateTime.Now + TimeSpan.FromSeconds(expiresIn);
// Now that we're logged in, make a OAuth2 request to get the user's id.
var request = new OAuth2Request("GET", new Uri("https://graph.facebook.com/me"), null, e.Account);
var response = await request.GetResponseAsync();
var obj = JObject.Parse(response.GetResponseText());
var id = obj["id"].ToString().Replace("\"", ""); // Id has extraneous quotation marks
// var user = await ParseFacebookUtils.LogInAsync(id, accessToken, expiryDate);
}
最后我通过在请求中添加字段得到了解决方案 URL。
var request = new OAuth2Request("GET", new Uri("https://graph.facebook.com/me?fields=email,first_name,last_name,gender,picture"), null, e.Account);