Click here to Skip to main content
14,391,598 members
Rate this:
Please Sign up or sign in to vote.
See more:
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
Posted
Comments
Member 10853717 26-Nov-15 6:08am
   
i am also facing same problem..have you solved the above issue?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100