如何使用xamarin.auth登录并使用xamarin表单获取用户的个人资料信息

时间:2016-04-12 12:55:42

标签: facebook xamarin.forms xamarin.auth

我使用Xamarin表单开发了一个跨平台应用程序。我被困在一个facebook问题上。所以我的问题是如何获取Facebook用户的个人资料信息。我只能得到" Id"和"姓名"用户但是要获得用户的完整详细信息(即电子邮件,个人资料图片,性别,出生日期等...)

enter image description here

这是我的代码:

 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);
    }

1 个答案:

答案 0 :(得分:2)

最后,我通过在请求网址中添加字段来获得解决方案。

var request = new OAuth2Request("GET", new Uri("https://graph.facebook.com/me?fields=email,first_name,last_name,gender,picture"), null, e.Account);