Donate

Read Files From Google Photos Using Google Photos API, REST And VB.NET

This is the VB.NET version of the post Read Files From Google Photos Using Google Photos API, REST and C#.NET. In your VB.NET project, you need to reference the Google.Apis.Auth package via NuGet and attach the JSON file that contains the Google API credentials.
alt=
The models used for converting a JSON response to .NET object are shown below.
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
And here's the entire console app code to read the files from Google Photos Library.
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

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

Bootstrap Modal In ASP.NET MVC With CRUD Operations