Click here to Skip to main content
15,891,033 members

Google Drive Help. Unable to call ApplyAuthenticationToRequest

Member 12103299 asked:

Open original thread
I am not a professional programmer. I want to download files from Google Drive. Collected the code from google developers but it is not working.
I want to download the "test document.txt" but I am unable to make the call because of the authenticator as IAuthenticator. Have a look here:

Shared Scopes As String() = {DriveService.Scope.DriveReadonly}
  Shared ApplicationName As String = "Drive API .NET"

  Public Shared Sub Main()
      Dim credential As UserCredential

      Using stream = New IO.FileStream("clients.json", IO.FileMode.Open, IO.FileAccess.Read)
          Dim credPath As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)
          credPath = IO.Path.Combine(credPath, ".credentials/drive-dotnet-quickstart")

          credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, Scopes, "user", CancellationToken.None, New FileDataStore(credPath, True)).Result
          Console.WriteLine(Convert.ToString("Credential file saved to: ") & credPath)
      End Using

      ' Create Drive API service.
      Dim service = New DriveService(New BaseClientService.Initializer() With { _
      .HttpClientInitializer = credential, _
       .ApplicationName = ApplicationName _
      })

      ' Define parameters of request.
      Dim listRequest As FilesResource.ListRequest = service.Files.List()
      listRequest.MaxResults = 1000

      ' List files.
      Dim files As IList(Of File) = listRequest.Execute().Items
      Console.WriteLine("Files:")
      If files IsNot Nothing AndAlso files.Count > 0 Then
          For Each file In files
              Console.WriteLine("{0} ({1})", file.Title, file.Id)
              If file.Title = "Test document.txt" Then
                  'DownloadFile(, file)
              End If
          Next
      Else
          Console.WriteLine("No files found.")
      End If
      Console.Read()

  End Sub

  Public Shared Function DownloadFile(authenticator As IAuthenticator, file As File) As System.IO.Stream
      If Not [String].IsNullOrEmpty(file.DownloadUrl) Then
          Try
              Dim request As HttpWebRequest = DirectCast(WebRequest.Create(New Uri(file.DownloadUrl)), HttpWebRequest)
              authenticator.ApplyAuthenticationToRequest(request)
              Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
              If response.StatusCode = HttpStatusCode.OK Then
                  Return response.GetResponseStream()
              Else
                  Console.WriteLine("An error occurred: " + response.StatusDescription)
                  Return Nothing
              End If
          Catch e As Exception
              Console.WriteLine("An error occurred: " + e.Message)
              Return Nothing
          End Try
      Else
          ' The file doesn't have any content stored on Drive.
          Return Nothing
      End If
  End Function
Tags: Visual Basic, Google

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900