返回facebook个人资料详细信息xamarin.forms

时间:2017-11-17 10:45:59

标签: xamarin xamarin.forms

我正在使用xamarin.forms为iOS和android创建facebook登录。它成功地允许用户登录,但我不知道如何返回用户的详细信息,因为我需要将详细信息传递给我的WebApi。如何返回用户的个人资料详细信息,例如姓名,姓氏和电子邮件。

代码

public partial class MainPage : ContentPage
    {
        private string ClientId = "";
        public MainPage()
        {
            InitializeComponent();

            var apiRequest = "https://www.facebook.com/dialog/oauth?client_id=" + ClientId + "&display=popup&response_type=token&redirect_uri=https://www.facebook.com/connect/login_success.html";

            var webView = new WebView
            {
                Source = apiRequest,
                HeightRequest=1
            };

            webView.Navigated += WebViewOnNavigated;

            Content = webView;
        }

        private async void WebViewOnNavigated(object sender, WebNavigatedEventArgs e)
        {
            var accessToken = ExtractAccessTokenFromUrl(e.Url);

            if(accessToken != "")
            {
                await GetFacebookProfileAsync(accessToken);
                await Navigation.PushAsync(new Success());
            }
        }

        private string ExtractAccessTokenFromUrl(string url)
        {
            if(url.Contains("access_token") && url.Contains("&expires_in"))
            {
                Navigation.PushAsync(new Success());
            }
            return string.Empty;
        }

        public async Task GetFacebookProfileAsync(string accessToken)
        {
            var requestUrl = "https://graph.facebook.com/me/" + "?fields=name,picture,cover,age_range,devices,email,gender,is_verified" + "&access_token=" + accessToken;

            var httpClient = new HttpClient();
            var userJson = await httpClient.GetStringAsync(requestUrl);

        }
    }

1 个答案:

答案 0 :(得分:1)

您需要发出GET:

"https://graph.facebook.com/me?fields=email,first_name,last_name,gender,picture.type(large)"

将您的facebook身份验证令牌插入标题:

client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);