Membaca File Dari Foto Google Menggunakan API Foto Google, REST Dan VB.NET

  • Whatsapp
Membaca File Dari Foto Google Menggunakan API Foto Google, REST Dan VB.NET

Ini adalah versi VB.NET dari posting Membaca File Dari Foto Google Menggunakan API Foto Google, REST dan C # .NET. Dalam proyek VB.NET Anda, Anda perlu mereferensikan paket Google.Apis.Auth melalui NuGet dan melampirkan file JSON yang berisi kredensial Google API.

Model yang digunakan untuk mengonversi respons JSON ke objek .NET ditampilkan di bawah ini.

Public Class clsResponseRootObject
Public Property mediaItems As List(Of MediaItem)
Public Property nextPageToken As String
End Class

Public Class MediaItem
Public Property id As String
Public Property productUrl As String
Public Property baseUrl As String
Public Property mimeType As String
Public Property mediaMetadata As MediaMetadata
Public Property filename As String
End Class

Public Class MediaMetadata
Public Property creationTime As DateTime
Public Property width As String
Public Property height As String
Public Property photo As Photo
End Class


Public Class Photo
Public Property cameraMake As String
Public Property cameraModel As String
Public Property focalLength As Double
Public Property apertureFNumber As Double
Public Property isoEquivalent As Integer
End Class

Dan inilah seluruh kode aplikasi konsol untuk membaca file dari Perpustakaan Foto Google.

Imports System.Net
Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Util.Store
Imports System.IO
Imports System.Threading
Imports Newtonsoft.Json

Module Module1

Sub Main()
Dim credPath As String = "your_filedatastore_path"
Dim responseObject As clsResponseRootObject = New clsResponseRootObject()
Dim credential As UserCredential
Dim scopes As String() = {"https://www.googleapis.com/auth/photoslibrary.sharing", "https://www.googleapis.com/auth/photoslibrary.readonly"}
Dim UserName As String = "your_gmail_address"
Dim ClientID As String = "your_client_id"
Dim ClientSecret As String = "your_client_secret"

Using stream As New FileStream("credentials.json", FileMode.Open, FileAccess.Read)

credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
scopes,
UserName,
CancellationToken.None,
New FileDataStore(credPath, True)).Result

End Using

Try
Dim httpWebRequest As HttpWebRequest = CType(WebRequest.Create("https://photoslibrary.googleapis.com/v1/mediaItems"), HttpWebRequest)
httpWebRequest.ContentType = "application/json"
httpWebRequest.Headers.Add("client_id", ClientID)
httpWebRequest.Headers.Add("client_secret", ClientSecret)
httpWebRequest.Headers.Add("Authorization:" & credential.Token.TokenType & " " + credential.Token.AccessToken)
httpWebRequest.Method = "GET"

Dim response As HttpWebResponse = CType(httpWebRequest.GetResponse(), HttpWebResponse)

Using responseStream = CType(response.GetResponseStream, Stream)

Dim reader As New StreamReader(responseStream, Text.Encoding.UTF8)
responseObject = JsonConvert.DeserializeObject(Of clsResponseRootObject)(reader.ReadToEnd())

If responseObject IsNot Nothing Then

If responseObject.mediaItems IsNot Nothing And responseObject.mediaItems.Count > 0 Then
Console.WriteLine("------------------------Retrieving media files--------------------------------")

For Each item As MediaItem In responseObject.mediaItems
Console.WriteLine(String.Format("ID:{0}, Filename:{1}, MimeType:{2}", item.id, item.filename, item.mimeType))
Next
End If
End If

End Using
Catch ex As Exception
Console.WriteLine("Error occured: " + ex.Message)
End Try

Console.ReadLine()

End Sub

End Module

Pos terkait

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *