成功在Android上登录Facebook后,Xamarin.Essentials.WebAuthenticator.AuthenticateAsync不关闭弹出窗口 [英] Xamarin.Essentials.WebAuthenticator.AuthenticateAsync does not close popup window after success login to Facebook on Android

查看:89
本文介绍了成功在Android上登录Facebook后,Xamarin.Essentials.WebAuthenticator.AuthenticateAsync不关闭弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Xamarin中创建了一个Facebook登录服务.在UWP应用程序中一切正常.我还实现了Android的必需选项(带有Intent的CallbackActivity).正确显示Facebook登录页面.登录成功,并且重定向到配置的成功页面.问题在于,在出现成功页面的Android上,登录窗口未关闭,并且Xamarin代码无法捕获access_token.在UWP中一切正常.在该服务中,我正在调用方法 RequestFacebookToken 来打开登录窗口并请求访问令牌.

I created a facebook login service in Xamarin. Everything is working fine in an UWP application. I also implemented the required options for Android (CallbackActivity with Intent). The Facebook login page properly appears. The login is succcess, and it redirects to the configured success page. The problem is that on Android after the success page appears, the login windows is not closing, and Xamarin code does not catch the access_token. Everything works fine properly in UWP. In the service I am calling method RequestFacebookToken for opening the login window and requesting access token.

这是我的Facebook服务:

Here is my facebook service:

public class FacebookService
{
    public const string CALLBACK_SCHEME = "https";

    private readonly HttpClient _httpClient;
    private const string _fbOauthEndpoint = "https://www.facebook.com/v7.0/dialog/oauth?";
    private const string CALLBACK_URI = "https://www.facebook.com/connect/login_success.html";
    private const string _fbAPI_URI = "https://graph.facebook.com/v2.8/";
    private string _fbAccessToken;
    private readonly string _AppId;

    public FacebookService(string AppID)
    {
        this._AppId = AppID;

        _httpClient = new HttpClient
        {
            BaseAddress = new Uri(_fbAPI_URI)
        };
        _httpClient.DefaultRequestHeaders
            .Accept
            .Add(new MediaTypeWithQualityHeaderValue("application/json"));
    }

    public async Task RequestFacebookToken()
    {
        WebAuthenticatorResult res = null;
        try
        {
            string state = Convert.ToBase64String(Encoding.Unicode.GetBytes(DateTime.Now.Ticks.ToString()));
            //  if (Device.RuntimePlatform == Device.UWP)
            //  {
            res = await WebAuthenticator.AuthenticateAsync(new Uri($"{_fbOauthEndpoint}client_id={this._AppId}&display=popup&response_type=token&redirect_uri={CALLBACK_URI}&state={state}"), new Uri($"{CALLBACK_URI}"));
            /*      }
                    else
                    {
                        res = await WebAuthenticator.AuthenticateAsync(new Uri($"{_fbOauthEndpoint}client_id={this._AppId}&response_type=token&redirect_uri={CALLBACK_URI}&state={state}"), new Uri($"{CALLBACK_URI}"));
                    }*/
        }
        catch (Exception e)
        {

        }

        this._fbAccessToken = res?.AccessToken;
    }

    public async Task<FacebookProfile> GetUserFromFacebookAsync()
    {
        var result = await GetAsync<dynamic>("me", "fields=first_name,last_name,email,gender,picture.width(100).height(100)");
        if (result == null)
        {
            throw new Exception("User from this token not exist");
        }

        var account = new FacebookProfile()
        {
            Email = result.email,
            FirstName = result.first_name,
            LastName = result.last_name,
            Picture = result.picture.data.url,
            Gender = result.gender
        };

        return account;
    }

    private async Task<T> GetAsync<T>(string endpoint, string args = null)
    {
        var response = await _httpClient.GetAsync($"{endpoint}?access_token={this._fbAccessToken}&{args}");
        var result = await response.Content.ReadAsStringAsync();
        if (!response.IsSuccessStatusCode)
            return default(T);
        return JsonConvert.DeserializeObject<T>(result);
    }
}

您有什么主意,为什么登录成功后登录窗口没有关闭?

Do you have any idea, what could be the issue why the login window is not closing after the login is success?

提前谢谢!

推荐答案

您可以使用Plugin.FacebookClient登录到Facebook.

You could use Plugin.FacebookClient to login into the facebook.

 public async Task LoginAsync()
        {
        FacebookResponse<bool> response = await CrossFacebookClient.Current.LoginAsync(permisions);
            switch(response.Status)
        {
            case FacebookActionStatus.Completed:
                IsLoggedIn = true;
                OnLoadDataCommand.Execute(null);
                break;
            case FacebookActionStatus.Canceled:
            
                break;
            case FacebookActionStatus.Unauthorized:
                await App.Current.MainPage.DisplayAlert("Unauthorized", response.Message, "Ok");
                break;
            case FacebookActionStatus.Error:
                await App.Current.MainPage.DisplayAlert("Error", response.Message,"Ok");
                break;
        }

        }

您可以从下面的链接下载源文件: https://github.com/WendyZang/FacebookClientPlugin

You could download the source file from the link below:https://github.com/WendyZang/FacebookClientPlugin

这篇关于成功在Android上登录Facebook后,Xamarin.Essentials.WebAuthenticator.AuthenticateAsync不关闭弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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