Google oAuth 2.0 with Xamarin.Android



我花了几天时间试图弄清楚如何使用Xamarin让我的应用程序对google进行身份验证。Android应用程序。

我尝试了OAuth2Authenticator (From Xamarin.Auth)。我得到错误,如"不允许的Url">

var auth = new OAuth2Authenticator(clientId: "aaaa-aaaaa.apps.googleusercontent.com",
scope: "https://www.googleapis.com/auth/userinfo.email",
authorizeUrl: new Uri("https://accounts.google.com/o/oauth2/auth"),
redirectUrl: new Uri("https://my-domain-test.com/api-v1/User/Welcome"),
getUsernameAsync: null);
auth.AllowCancel = allowCancel;
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);
//step:3 Google API Request to get Profile Information
if (await fnGetProfileInfoFromGoogle(access_token))
{
Toast.MakeText(this, "Authentcated successfully", ToastLength.Short).Show();
}
};
var intent = auth.GetUI(this);
StartActivity(intent);

然后我也尝试了Google的oAuth Library

UserCredential credential;

credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets() { ClientId= "aaaa-aaaa.apps.googleusercontent.com", ClientSecret= "xxx-xxxx-tlD" },
new[] {"email"},
"user", CancellationToken.None);

it Simple在GoogleWebAuthorizationBroker上失败。AuthorizeAsync, Instance不能创建

是否有人尝试将Google oAuth认证成功集成到他们的Android项目中?我没主意了。

Xamarin.Auth库已经很久没有更新了

用于Xamarin中的身份验证。表单,尝试使用Xamarin.Essentials库。

var authResult = await WebAuthenticator.AuthenticateAsync(
new Uri("https://example.com/mobileauth/"),
new Uri("myapp://"));
var accessToken = authResult?.AccessToken;

有关详细信息,请参阅MS文档:https://learn.microsoft.com/en-us/xamarin/essentials/web-authenticator?tabs=android

最新更新