2017-08-15 66 views
0

我想要得到的圖片的URL在谷歌存在的驅動,並顯示它在形式,我試試這個方法,但總是在沒有 這裏是我的代碼獲取谷歌圖片URL開車

..... 
CreateService() 
    Dim tab() As String = fichier.Split(".") 
    Dim list = Service.Files.List() 
    list.Q = "trashed=false" 
    list.Fields = "nextPageToken, items(id, title)" 
    Dim count = list.Execute() 
    For Each fich In count.Items 
     If (fich.Title) = fichier Then 

      Dim path As String = fich.WebContentLink 
      Dim MyWebClient As New System.Net.WebClient 
      Dim ImageInBytes() As Byte = MyWebClient.DownloadData(path) 
      Dim ImageStream As New IO.MemoryStream(ImageInBytes) 
       affich_image.image.Image = New 
      System.Drawing.Bitmap(ImageStream) 
     end if 

fich.WebContentLink他的價值是什麼 fichier是字符串,文件的 名在GridView控件

+0

你不能使用谷歌API,下載圖片到一個臨時文件夾,並從那裏加載到您的圖片框? https://developers.google.com/drive/v3/web/quickstart/dotnet – Werdna

回答

0

這裏存在從SO post用於驅動V2 &的OAuth 2.0工作vb.net代碼:

Imports System.Threading 
    Imports System.Threading.Tasks 

    Imports Google 
    Imports Google.Apis.Auth.OAuth2 
    Imports Google.Apis.Drive.v2 
    Imports Google.Apis.Drive.v2.Data 
    Imports Google.Apis.Services 

    Imports Google.Apis.Auth 
    Imports Google.Apis.Download 

    'Dev Console: 
    'https://console.developers.google.com/ 

    'Nuget command: 
    'Install-Package Google.Apis.Drive.v2 

    Private Service As DriveService = New DriveService 

    Private Sub CreateService() 
    If Not BeGreen Then 
     Dim ClientId = "your client ID" 
     Dim ClientSecret = "your client secret" 
     Dim MyUserCredential As UserCredential = GoogleWebAuthorizationBroker.AuthorizeAsync(New ClientSecrets() With {.ClientId = ClientId, .ClientSecret = ClientSecret}, {DriveService.Scope.Drive}, "user", CancellationToken.None).Result 
     Service = New DriveService(New BaseClientService.Initializer() With {.HttpClientInitializer = MyUserCredential, .ApplicationName = "Google Drive VB Dot Net"}) 
    End If 
End Sub 


    Private Sub UploadFile(FilePath As String) 
    Me.Cursor = Cursors.WaitCursor 
    If Service.ApplicationName <> "Google Drive VB Dot Net" Then CreateService() 

    Dim TheFile As New File() 
    TheFile.Title = "My document" 
    TheFile.Description = "A test document" 
    TheFile.MimeType = "text/plain" 

    Dim ByteArray As Byte() = System.IO.File.ReadAllBytes(FilePath) 
    Dim Stream As New System.IO.MemoryStream(ByteArray) 

    Dim UploadRequest As FilesResource.InsertMediaUpload = Service.Files.Insert(TheFile, Stream, TheFile.MimeType) 

    Me.Cursor = Cursors.Default 
    MsgBox("Upload Finished") 
End Sub 

    Private Sub DownloadFile(url As String, DownloadDir As String) 
    Me.Cursor = Cursors.WaitCursor 
    If Service.ApplicationName <> "Google Drive VB Dot Net" Then CreateService() 

    Dim Downloader = New MediaDownloader(Service) 
    Downloader.ChunkSize = 256 * 1024 '256 KB 

    ' figure out the right file type base on UploadFileName extension 
    Dim Filename = DownloadDir & "NewDoc.txt" 
    Using FileStream = New System.IO.FileStream(Filename, System.IO.FileMode.Create, System.IO.FileAccess.Write) 
     Dim Progress = Downloader.DownloadAsync(url, FileStream) 
     Threading.Thread.Sleep(1000) 
     Do While Progress.Status = TaskStatus.Running 
     Loop 
     If Progress.Status = TaskStatus.RanToCompletion Then 
      MsgBox("Downloaded!") 
     Else 
      MsgBox("Not Downloaded :(") 
     End If 
    End Using 
    Me.Cursor = Cursors.Default 
End Sub 

要獲取網址,這裏是代碼:

Dim Request = Service.Files.List() 
    Request.Q = "mimeType != 'application/vnd.google-apps.folder' and trashed = false" 
    Request.MaxResults = 2 
    Dim Results = Request.Execute 
    For Each Result In Results.Items 
     MsgBox(Result.DownloadUrl & vbCrLf & Result.Title & vbCrLf & Result.OriginalFilename) 
    Next 

對於你需要什麼,看看這個documentation進一步瞭解。