无法使用Xamarin Android获得Google Oauth2的刷新令牌 [英] Unable to get refresh token for google Oauth2 with xamarin android

查看:72
本文介绍了无法使用Xamarin Android获得Google Oauth2的刷新令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的xamarin android应用中实现google身份验证.我正在使用OAuth2Authenticator对用户进行身份验证,身份验证后仅返回access_token.如何获取refresh_token,以便用户每次打开应用程序时都不必登录?

I am trying to implement google authentication in my xamarin android app. I am using OAuth2Authenticator to authenticate my user and after authentication it only returns access_token.How do i get refresh_token so that the user dont have to login each time he opens the app?

我一直在使用以下代码对用户进行身份验证

I have been using the following code to authenticate the user

 var auth = new OAuth2Authenticator(
            clientId: "***************************.apps.googleusercontent.com",
            scope: "https://www.googleapis.com/auth/userinfo.profile",
            authorizeUrl: new Uri("https://accounts.google.com/o/oauth2/auth"),
            redirectUrl: new Uri("https://www.googleapis.com/plus/v1/people/me"),
            accessTokenUrl: newUri("https://www.googleapis.com/oauth2/v4/token"),
            clientSecret: "*********************",
            getUsernameAsync: null);


auth.Completed += async (sender, e) =>
            {
                if (!e.IsAuthenticated)
                {
                    Toast.MakeText(this, "Fail to authenticate!", ToastLength.Short).Show();
                    return;
                }
                string access_token;
                e.Account.Properties.TryGetValue("access_token", out access_token);
}

推荐答案

由于Google不再建议使用该应用程序内置的WebView进行登录,因此Xamarin.Auth和Google似乎存在一些问题,而应该使用这些设备浏览器.请参阅Google关于此的文章:

It seems there are some issues with Xamarin.Auth and Google due to Google no longer recommending using the app's built in WebView for login and instead one should use the devices browser. See Google's article on this:

https://developers.googleblog.com/2016/08/modernizing-oauth-interactions-in-native-apps.html

如果您确实使用了应用程序内置的WebView(如Xamarin.Auth的默认设置一样),则您将不会获得刷新令牌,如下所示(根据上面的链接):

If you do use a WebView built in to the app, as Xamarin.Auth does by default, you will not get a refresh token as (per the article linked above):

相比之下,使用嵌入式浏览器进行OAuth的过时方法意味着用户每次必须登录Google,而不是使用设备中现有的登录会话.

In contrast, the outdated method of using embedded browsers for OAuth means a user must sign-in to Google each time, instead of using the existing logged-in session from the device.

这篇关于无法使用Xamarin Android获得Google Oauth2的刷新令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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