Home > OS >  GoogleWebAuthorizationBroker doesn't work on Xamarin Android
GoogleWebAuthorizationBroker doesn't work on Xamarin Android

Time:04-10

I would like to use google oauth authorization in my mobile app. I had already implemented this feature in test Console app before. But something went wrong after I rebased my code in Xamarin Android. Code :

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:");
}

This is dirty code but well executing in Console App. When I try to launch method in Xamarin I catch exception :

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://127.0.0.1:40031/authorize/&scope=https://www.googleapis.com/auth/youtube.readonly" for authorization.

enter image description here

I'm not sure that problem in mobile app permissions, because this code executed fine.

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://127.0.0.1:40031/authorize/&scope=https://www.googleapis.com/auth/youtube.readonly");
    await Browser.OpenAsync(uri, BrowserLaunchMode.SystemPreferred);
}

CodePudding user response:

The enter image description here

There have been a number of issues over the years requesting support #984 a decision was made not to attempt to support 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.

As the library is currently in Maintenance mode and no addional features are currently in the works. I would not expect this to be added.

That being said i was able to get this working for a client a few years ago it required that I create my own authorization method for it. which populated the credentials then create your own implementation of idatastore which will read your credentials. This may give you an idea of how you can do it.

  • Related