Groogle Intro

Groogle is a DSL (Domain Specific Language) oriented to interact with Google Cloud services in an easy way. It provides a concise language so you can create scripts or integrate into your application and consume Google Cloud services as Drive, Sheet or Gmail

In this post series I’ll (try) to explain the origin, aim and how to use it with several real examples

Requirement

  • A Google account

  • Java 11+ and Google 4.x installed

  • A Google credentials file (more details above)

Example

With Groogle you can create a script, for example, to list all the files in your Drive:

groogle = GroogleBuilder.build {
    withOAuthCredentials {
        applicationName 'test'
        scopes DriveScopes.DRIVE
        usingCredentials "client_secret.json"
        storeCredentials true
    }
}

groogle.with {
    service(DriveService).with {
        findFiles {
            eachFile {
                println "$id = $file.name"
            }
        }
    }
}

Origin

A few years ago I was fascinated about how easily you can write your own DSL using ApacheGroovy so I started a project called Groogle (Groovy + Google)

I started it using the com.puravida-software.groogle maven coordinates but as PuraVida Software company run out, I’ve decided to rewrite/improve it under the new es.edn.groogle coordinates

First steps

Before to start you need to have a Google Cloud account and a project created, say QuickStart for example

Create an Oauth 2.0 credential (service credentials will be covered in another post):

oyv3nvx16j574k884a54
b5qlgt48duwxgznh63h0

Download the .json file but don’t share them nor store in your repo. For our examples this file will be called client_secret.json

First script

In the same folder you downloaded the json create a example.groovy file:

import com.google.api.services.sheets.v4.SheetsScopes
import com.google.api.services.drive.DriveScopes
import es.edn.groogle.*

@Grab("es.edn:groogle:4.0.0-rc4")
@GrabConfig(systemClassLoader=true)

groogle = GroogleBuilder.build {
    withOAuthCredentials {
        applicationName 'test'
        scopes DriveScopes.DRIVE, SheetsScopes.SPREADSHEETS
        usingCredentials "client_secret.json"
        storeCredentials true
    }
}

groogle.with {
    service(DriveService).with {
        findFiles {
            eachFile {
                println "$id = $file.name"
            }
        }
    }
}

in a terminal console execute:

groovy example.groovy

if all goes well a browser will be opened and you need to indicate which Google account you want to use

wl1k1kjl22dkit2ghzfz

and allow access to the application

01cj5vk58mnks9wc1iwa

Now you can close the browser and see how the script was able to iterate over your files

2snq47fbrcf1l68ujrp7

Next

In the following posts we’ll see how to create or download files from your Drive, create and modify Sheets or send emails

Este texto ha sido escrito por un humano

This post was written by a human

2019 - 2024 | Mixed with Bootstrap | Baked with JBake v2.6.7