1

I have a new Blazor application I'm trying to stitch together and I want to read out all my recent Google drive files. Calling the auth code flow, getting a response (including the code) and so forth is all working fine. I have the correct approve-list of redirect URIs but calling flow.ExchangeCodeForTokenAsync always fails with a response property saying the call is in a faulted state.

When initially calling my 'signin' page, the code redirects as expected, with the appropriate state and code string included in the URL. However, when my page sees a Google response code and falls into the second part of my if/then conditional, the Task always fails, token.IsFaulted is always true and WASM shows an exception: "WASM: [System.AggregateException] One or more errors occurred. (Cannot invoke method because it was wiped"

I could really do with a nudge in the right direction here.

@using Google.Apis.Drive.v3
@using Google.Apis.Auth.OAuth2
@using Google.Apis.Auth.OAuth2.Flows
@using Google.Apis.Auth.OAuth2.Responses
@using Google.Apis.Auth.OAuth2.Web
@using Google.Apis.Services
@using Google.Apis.Util.Store
@using System.IO
@using System.Threading
@page "/signin"
@inject HttpClient Http
@inject Microsoft.AspNetCore.Blazor.Services.IUriHelper UriHelper


@functions {

    private GoogleAuthorizationCodeFlow flow;
    private string[] _scopes = new string[] 
    {
        DriveService.Scope.Drive, 
        DriveService.Scope.DriveReadonly, 
        DriveService.Scope.DriveMetadataReadonly, 
        DriveService.Scope.DriveAppdata, 
        DriveService.Scope.DriveFile 
    };

    ClientSecrets secrets = new ClientSecrets() 
    { 
        ClientId = "a-very-long-string", 
        ClientSecret = "a-slightly-shorter-string"
    };

    protected override async Task OnInitAsync()
    {
        flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer(){
            DataStore = new FileDataStore("My.Education.Store", false),
            ClientSecrets = secrets,
            Scopes = _scopes
        });

        Uri uri = new Uri(UriHelper.GetAbsoluteUri());
        string code = uri.Query.IndexOf("code") > 0 ? uri.QueryParam()["code"] : String.Empty; // QueryParam is just an extension method

        if (String.IsNullOrEmpty(code))
        {
            AuthorizationCodeWebApp.AuthResult result = (new AuthorizationCodeWebApp(flow, $"{uri.Scheme}://{uri.Host}:{uri.Port}/signin", uri.ToString())).AuthorizeAsync("me", CancellationToken.None).Result;
            Console.WriteLine($"Redirecting to {result.RedirectUri}");

            //UriHelper.NavigateTo(result.RedirectUri);
        }
        else
        {
            var token = flow.ExchangeCodeForTokenAsync("me", code, "http://locahost:5000", CancellationToken.None);
            Console.WriteLine(token.IsCompleted);
            Console.WriteLine($"Faulted: {token.IsFaulted}"); // always true

        }

    }

}
Vibeeshan Mahadeva
  • 7,147
  • 8
  • 52
  • 102
Phil.Wheeler
  • 16,748
  • 10
  • 99
  • 155
  • strongly recommend that you ask in the Blazor gitter room - https://gitter.im/aspnet/Blazor – Rich Bryant Dec 31 '18 at 13:52
  • 1
    I made a server-side Blazor version of your code so I could easily set break points and it also returns null on this call: flow.ExchangeCodeForTokenAsync - - My suggestion is to not use the Google components and call Google 'manually' using a method such as: https://stackoverflow.com/questions/25845420/mvc-5-application-implement-oauth-authorization-code-flow – Michael Washington Jan 06 '19 at 19:43

0 Answers0