c# - Xamarin 表单中的 Google 身份验证
问题描述
我正在尝试在 Xamarin Forms 应用程序中对用户进行身份验证。我相信每个平台必须以不同的方式完成,所以我使用 MainActivity 中的一个函数来验证应用程序何时启动。我在他们的网站上使用了 Google .NET 文档,但遇到了一些错误。
我直接复制了 Google 代码,并通过嵌入式资源获取了 credentials.json。当我尝试使用 GoogleWebAuthorizationBroker.AuthorizeAsync 设置凭据时会出现问题。在 google 代码中,credPath 设置为 token.json。我收到一个我没有权限的错误。然后我尝试使用 Android.OS.Environment.ExternalStorageDirectory ,现在出现多个错误。我看不到个别错误,只有 System.AggregateException:发生了一个或多个错误。
UserCredential credential;
var assembly = typeof(MainActivity).GetTypeInfo().Assembly;
using (var stream =
assembly.GetManifestResourceStream("Briefing.Droid.credentials.json"))
{
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
string credPath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, "token.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
我希望打开一个窗口,允许用户使用他们的谷歌帐户登录。我得到 System.AggregateException:发生了一个或多个错误。
解决方案
我最终使用了 Xamarin Auth。请参阅此链接:https ://docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/authentication/oauth
推荐阅读
- html - 你如何只为 1 个元素添加 css
- javascript - 为什么将变量列为依赖项“解决”过时的闭包问题?
- string - 使用不相关表的 PowerBi 字符串搜索
- flutter - 转到另一个有状态小部件后,如何在我的颤振应用程序中保留汉堡包图标?
- python - 气流创建循环任务以多次运行
- java - 无法找到失败的 CompileTestGroovy gradle 任务的循环依赖项
- java - 使用来自 sqlLite Blob 的图像并在 Android Studio 的 setImageResource 中使用它们
- javascript - vuetify 中的组合框不像输入那样工作
- javascript - 如何使用 node express 监听连接的外围设备,例如指纹 usb?
- ios - OpenVPN 服务器不转发流量