GoogleWebAuthorizationBroker 不适用于 Xamarin Android

GoogleWebAuthorizationBroker doesn't work on Xamarin Android

我想在我的移动应用程序中使用 google oauth 授权。我之前已经在测试控制台应用程序中实现了这个功能。但是在我在 Xamarin Android 中重新设置我的代码后出现了问题。代码:

private void _youtubeOAuth() 
{
    string[] scopes =
        {
        "https://www.googleapis.com/auth/youtube.readonly",
    };
    var clientSecrets = new ClientSecrets
    {
        ClientId = "00000v.apps.googleusercontent.com",
        ClientSecret = "GOCS****Jo8EMYq"
    };

    UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
        clientSecrets,
        scopes,
        "user_id",
        CancellationToken.None,
        new FileDataStore(""))
    .Result;

    var youtubeService = new YouTubeService(
        new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "Desktop client 1"
        });

    var channelsListRequest = youtubeService.Channels.List("contentDetails, snippet");
    channelsListRequest.Mine = true;
    ChannelListResponse channelsListResponse = channelsListRequest.Execute();
    string chanellTitle = channelsListResponse.Items[0].Snippet.Title;
    Console.WriteLine(chanellTitle + " Subscriptions:");
}

这是脏代码,但在控制台应用程序中执行良好。当我尝试在 Xamarin 中启动方法时,我发现异常:

Failed to launch browser with "https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&response_type=code&client_id=****.apps.googleusercontent.com&redirect_uri=http%3A%2F%2F127.0.0.1%3A40031%2Fauthorize%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.readonly" for authorization.

我不确定移动应用程序权限是否存在问题,因为这段代码执行得很好。

private async void _openBrowser() 
{
    Uri uri = new Uri("https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&response_type=code&client_id=****.apps.googleusercontent.com&redirect_uri=http%3A%2F%2F127.0.0.1%3A40031%2Fauthorize%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.readonly");
    await Browser.OpenAsync(uri, BrowserLaunchMode.SystemPreferred);
}

google .net client library 不支持 Xamarin.此信息可在图书馆的 Github 页面上找到。

多年来有许多问题请求支持#984决定不尝试支持 Xamarin。

At a team discussion in October 2018 we made the decision to not proceed with support for Xamarin. We don't see evidence that there would be enough usage to justify the technical work and infrastructure required for us to fully support the Xamarin platform.

We will revisit this decision on a regular basis, in case the situation changes.

由于库目前处于维护模式,目前没有其他功能正在开发中。我不希望添加这个。

话虽这么说,几年前我就能够为客户使用它,但我需要为它创建自己的授权方法。填充凭据然后创建您自己的 idatastore 实现,它将读取您的凭据。这可能会让您了解如何做到这一点。