Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GoogleWebAuthorizationBroker.AuthorizeAsync Hangs

Our website needs to upload videos to youtube from the code behind (asp.net mvc application). I'm trying to get the google credentials, but when i call the AuthorizeAsync, the application just hangs. I've looked all over for a solution and none seem to help out. I've already searched for the obvious on google and stack overflow. most of what i found mentioned that the application might not have access the the appdata folder, so i tried changing the folder to be in the c drive, d drive and in the actual inetpub location. i tested and found i was able to have the application write to those locations.

to be more specific, the user is our admin, customers upload videos to us, and the admin approves them. when the admin approves them, it is posted on our youtube account. the admin should not have to do anything but click the approve button.

To make this an actual question, what can i do to get past the AuthorizeAsync? Let me know if you need more info

        UserCredential credential;
        GoogleWebAuthorizationBroker.Folder = "YouTube.Auth.Store";
        using (var stream = new FileStream(CredentialsPath, FileMode.Open,
                             FileAccess.Read))
        {
            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                // This OAuth 2.0 access scope allows an application to upload files to the
                // authenticated user's YouTube channel, but doesn't allow other types of access.
                new[] { YouTubeService.Scope.YoutubeUpload },
                "user",
                CancellationToken.None,
                new FileDataStore("YouTube.Auth.Store")
            ).Result;
        }
like image 370
iedoc Avatar asked Dec 19 '14 20:12

iedoc


2 Answers

Found a way to get passed this.

I used GoogleAuthorizationCodeFlow instead. this is what it turned out to look like:

        ClientSecrets secrets = new ClientSecrets()
        {
            ClientId = CLIENT_ID,
            ClientSecret = CLIENT_SECRET
        };

        var token = new TokenResponse { RefreshToken = REFRESH_TOKEN }; 
        var credentials = new UserCredential(new GoogleAuthorizationCodeFlow(
            new GoogleAuthorizationCodeFlow.Initializer 
            {
                ClientSecrets = secrets
            }), 
            "user", 
            token);

        var service = new YouTubeService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credentials,
            ApplicationName = "TestProject"
        });
like image 63
iedoc Avatar answered Nov 10 '22 23:11

iedoc


It is possible to break out if this, for example using a timeout, because the AuthorizeAsync method has the implementation of a CancellationToken.

  • Create a TokenSource to obtain the Token itself.
  • Set the Cancellation of the TokenSource to i.e. 20 Seconds
  • Extract the token to pass to the AuthorizeAsync method.

public async void RunAsync()
{
    UserCredential credential;
    var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read);

    CancellationTokenSource cts = new CancellationTokenSource();
    cts.CancelAfter(TimeSpan.FromSeconds(20));
    CancellationToken ct = cts.Token;

    credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
    GoogleClientSecrets.Load(stream).Secrets,
    new[] { YouTubeService.Scope.YoutubeUpload },
    "user",
    ct
    );

    if (ct.IsCancellationRequested) return;

    // do more stuff when came back authorized.
}

Now, after 20 seconds, when the AuthorizeAsync was not completed, the caller of this method RunAsync will receive an Exception.

try
{
    await RunAsync();
}
catch(Exception)
{
     // "Timeout" of the RunAsync(); 
}
like image 30
Markus Zeller Avatar answered Nov 11 '22 01:11

Markus Zeller