首页 > 解决方案 > 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:发生了一个或多个错误。

标签: c#xamarin.formsxamarin.androidgoogle-authentication

解决方案



推荐阅读