GoogleWebAuthorizationBroker.AuthorizeAsync se bloquea

Nuestro sitio web necesita subir videos a youtube desde el código subyacente (asp.net mvc application). Intento obtener las credenciales de google, pero cuando llamo a AuthorizeAsync, la aplicación simplemente se cuelga. He buscado una solución y ninguna parece ayudarme. Ya he buscado lo obvio en google y el desbordamiento de stack. la mayoría de lo que encontré mencionó que la aplicación podría no tener acceso a la carpeta appdata, así que intenté cambiar la carpeta para que estuviera en la unidad c, d drive y en la ubicación real de inetpub. Probé y descubrí que podía hacer que la aplicación escribiera en esos lugares.

para ser más específico, el usuario es nuestro administrador, los clientes nos suben videos y el administrador los aprueba. cuando el administrador los aprueba, se publica en nuestra cuenta de youtube. el administrador no debería tener que hacer nada más que hacer clic en el botón de aprobación.

Para hacer de esto una pregunta real, ¿qué puedo hacer para pasar el AuthorizeAsync? Hágame saber si necesita más información

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; } 

Encontré una manera de pasar esto.

Usé GoogleAuthorizationCodeFlow en su lugar. así es como se veía:

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

Es posible partir si esto, por ejemplo, utilizando un tiempo de espera, porque el método AuthorizeAsync tiene la implementación de un CancellationToken .

  • Crea un TokenSource para obtener el Token en sí mismo.
  • Establezca la cancelación de TokenSource en, por ejemplo, 20 segundos
  • Extraiga el token para pasar al método AuthorizeAsync .

 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. } 

Ahora, después de 20 segundos, cuando AuthorizeAsync no se completó, la persona que llama de este método RunAsync recibirá una excepción.

 try { await RunAsync(); } catch(Exception) { // "Timeout" of the RunAsync(); } 

El siguiente código le permite usar sus credenciales de la API de Google Drive para solicitar información de inicio de sesión al usuario y descargar el archivo a su servidor para realizar alguna solicitud:

 Option Strict On Option Explicit On Imports System.Net Imports System.IO Imports System.Linq Imports System.Threading Imports System.Web Imports System.Web.UI.WebControls Imports Google Imports Google.Apis.Auth.OAuth2 Imports Google.Apis.Auth.OAuth2.Flows Imports Google.Apis.Auth.OAuth2.Web Imports Google.Apis.Services 'for drive Imports Google.Apis.Drive.v2 Imports Google.Apis.Drive.v2.Data Imports Google.Apis.Util.Store 'for AuthorizationCodeRequestUrl Imports Google.Apis.Auth.OAuth2.Requests Imports System.Drawing Public Class _Default Inherits Page Private redirect_uri As String = "" Private client_id As String = "" Private client_secret As String = "" Private service As DriveService 'Application logic should manage users authentication. 'This sample works with only one user. You can change it by retrieving 'data from the session. 'You use the word user because you are authorized by the user to access 'their drive contents. Private UserId As String = "user" Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load Dim SCOPES As String() = New [String]() {"https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/drive.install"} Dim flow As GoogleAuthorizationCodeFlow 'use extended class to create google authorization code flow 'use custom own datastore flow = New ForceOfflineGoogleAuthorizationCodeFlow(New GoogleAuthorizationCodeFlow.Initializer() With { .DataStore = New FileDataStore("D:\inetpub\wwwroot\go\filedatastore", True), .ClientSecrets = New ClientSecrets() With { .ClientId = client_id, .ClientSecret = client_secret }, .Scopes = SCOPES }) Dim uri = Request.Url.ToString() Dim redirecturi As String = redirect_uri Dim code = Request("code") If code IsNot Nothing Then output.Text &= "
Code: " & code & "
" output.Text &= "
UserID: " & UserId & "
" output.Text &= "
URI: " & uri & "
" output.Text &= "
RedirectURI: " & redirecturi & "
" output.Text &= "
uri-substring-thing: " & uri.Substring(0, uri.IndexOf("?")) & "
" Dim token = flow.ExchangeCodeForTokenAsync(UserId, code, redirecturi, CancellationToken.None).Result ' Extract the right state. Dim oauthState = AuthWebUtility.ExtracRedirectFromState(flow.DataStore, UserId, Request("state")).Result Response.Redirect(oauthState) Else Dim result = New AuthorizationCodeWebApp(flow, redirecturi, uri).AuthorizeAsync(UserId, CancellationToken.None).Result If result.RedirectUri IsNot Nothing Then ' Redirect the user to the authorization server. Response.Redirect(result.RedirectUri) Else ' The data store contains the user credential, so the user has been already authenticated. service = New DriveService(New BaseClientService.Initializer() With { .HttpClientInitializer = result.Credential, .ApplicationName = "Drive API Sample" }) End If End If End Sub ''' ''' Download a file ''' Documentation: https://developers.google.com/drive/v2/reference/files/get ''' ''' a Valid authenticated DriveService ''' File resource of the file to download ''' location of where to save the file including the file name to save it as. ''' Public Shared Function downloadFile(_service As DriveService, _fileResource As Data.File, _saveTo As String) As [Boolean] If Not [String].IsNullOrEmpty(_fileResource.DownloadUrl) Then Try Dim x = _service.HttpClient.GetByteArrayAsync(_fileResource.DownloadUrl) Dim arrBytes As Byte() = x.Result System.IO.File.WriteAllBytes(_saveTo, arrBytes) Return True Catch e As Exception Console.WriteLine("An error occurred: " + e.Message) Return False End Try Else ' The file doesn't have any content stored on Drive. Return False End If End Function Public Async Function GetFile(IncomingFileID As String) As Tasks.Task Try Dim request As FilesResource.GetRequest = service.Files.Get(IncomingFileID) Dim response As Data.File = Await request.ExecuteAsync(CancellationToken.None) downloadFile(service, response, "D:/inetpub/wwwroot/go/" & Guid.NewGuid.ToString) Catch ex As Exception Dim str As String = "
" & ex.ToString() str = str.Replace(Environment.NewLine, Environment.NewLine + "
") str = str.Replace(" ", "  ") output.Text &= String.Format("{0}", str) End Try End Function '''Gets the File Lists of the user. Public Async Function FetchFilelists() As System.Threading.Tasks.Task Try ' Get all files of the user asynchronously. Dim request As FilesResource.ListRequest = service.Files.List() 'get only my files request.Q = "mimeType contains 'image/'" Dim response As FileList = Await request.ExecuteAsync() ShowFilelists(response) Catch ex As Exception Dim str = ex.ToString() str = str.Replace(Environment.NewLine, Environment.NewLine + "
") str = str.Replace(" ", "  ") output.Text = String.Format("{0}", str) End Try End Function Private Sub ShowFilelists(response As FileList) If response.Items Is Nothing Then output.Text &= "You have no files!
" Return End If output.Text &= "Showing files...
" Dim ctr As Integer = 0 For Each file As Google.Apis.Drive.v2.Data.File In response.Items ctr += 1 Dim listPanel As New Panel() With { .BorderWidth = Unit.Pixel(1), .BorderColor = Color.Black } listPanel.Controls.Add(New Label() With { .Text = file.Title + "--" + file.MimeType }) listPanel.Controls.Add(New Label() With { .Text = "
" }) If file.MimeType.ToString().Contains("folder") = False Then listPanel.Controls.Add(New HyperLink() With { .Text = "Download", .NavigateUrl = file.WebContentLink }) listPanel.Controls.Add(New Button() With { .Text = "Use This Image", .OnClientClick = "document.getElementById('currentImageId').value = '" & file.Id & "';return false;"}) End If lists.Controls.Add(listPanel) Next output.Text &= "Total Files: " & ctr.ToString End Sub Protected Async Sub listButton_Click(sender As Object, e As EventArgs) Handles listbutton.Click Await FetchFilelists() End Sub Protected Async Sub DownloadImageButton_Click(sender As Object, e As EventArgs) Handles DownloadImageButton.Click Dim idToUse As String = Page.Request.Form(currentImageId.UniqueID) output.Text &= "
Attempting to download id: " & idToUse & "
" Await GetFile(idToUse) output.Text &= "
Download Completed
" End Sub 'overwrite. force offline. Friend Class ForceOfflineGoogleAuthorizationCodeFlow Inherits GoogleAuthorizationCodeFlow 'source: //gotoanswer.stanford.edu/google_analytics_oauth_with_accesstype__offline_in_c-4478263/ Public Sub New(initializer As GoogleAuthorizationCodeFlow.Initializer) MyBase.New(initializer) End Sub Public Overrides Function CreateAuthorizationCodeRequest(redirectUri As String) As AuthorizationCodeRequestUrl Dim ss = New Google.Apis.Auth.OAuth2.Requests.GoogleAuthorizationCodeRequestUrl(New Uri(AuthorizationServerUrl)) ss.AccessType = "offline" ss.ApprovalPrompt = "force" ss.ClientId = ClientSecrets.ClientId ss.Scope = String.Join(" ", Scopes) ss.RedirectUri = redirectUri Return ss End Function End Class End Class

Aquí está la página .aspx de WebForm:

 <%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="PictureMyCard._Default" Async="True" %>