Standard library · Web and services

google

Google service account authentication for the Google APIs.

import google

Server programs reach the Google APIs through a "service account". Get the account JSON file from the Google console, read it with ParseServiceAccount, and get a token for the scope you need with AccessToken; then send it in your net/http requests with Authorization. Keep the service account file as carefully as a password.

Types

ServiceAccount

struct ServiceAccount
A Google service account.
NameTypeDescription
client_emailstr = ""The account email.
private_keystr = ""The PEM private key.
token_uristr = "https://oauth2.googleapis.com/token"The address the token comes from.
project_idstr = ""The project id.
okbool = falseIt was read correctly.
google-Account.salam
import google

func main:
    document := `{"type":"service_account","project_id":"salam-demo","client_email":"bot@salam-demo.iam.gserviceaccount.com","private_key":"(the private key goes here)"}`
    account := google.ParseServiceAccount(document)
    println account.ok, account.project_id, account.client_email
    println account.token_uri
    println account.private_key.len() > 0
    println google.ParseServiceAccount(`{"project_id":"x"}`).ok
    println google.Authorization("ya29.abc")
    println google.SCOPE_FCM
    println google.SCOPE_STORAGE
    println google.SCOPE_CLOUD
end
Outputtrue salam-demo bot@salam-demo.iam.gserviceaccount.com https://oauth2.googleapis.com/token true false Bearer ya29.abc https://www.googleapis.com/auth/firebase.messaging https://www.googleapis.com/auth/devstorage.read_write https://www.googleapis.com/auth/cloud-platform

Constants and variables

SCOPE_FCM

const SCOPE_FCM: str = "https://www.googleapis.com/auth/firebase.messaging"
The access scope for sending notifications with Firebase Cloud Messaging.

The example for this item is shown with the ones above: google-Account.salam

SCOPE_STORAGE

const SCOPE_STORAGE: str = "https://www.googleapis.com/auth/devstorage.read_write"
The scope for reading and writing in Cloud Storage.

The example for this item is shown with the ones above: google-Account.salam

SCOPE_CLOUD

const SCOPE_CLOUD: str = "https://www.googleapis.com/auth/cloud-platform"
The full Google Cloud scope.

The example for this item is shown with the ones above: google-Account.salam

Functions

ParseServiceAccount

func ParseServiceAccount(document: str): ServiceAccount
Reads a service account JSON file.

The example for this item is shown with the ones above: google-Account.salam

AccessToken

func AccessToken(sa: ServiceAccount, scope: str, ok &: bool): str
Gets an OAuth access token for the given scope; the token is kept and reused until it expires.
google-Token.salam
import google
import io
import net.http

func main:
    account := google.ParseServiceAccount(io.ReadFile("service-account.json"))
    if not account.ok:
        println "the service account file is not valid"
        ret
    end
    mut good := false
    token := google.AccessToken(account, google.SCOPE_STORAGE, good)
    if not good:
        println "the token was not granted"
        ret
    end
    mut headers := http.NewHeaders()
    headers.insert("Authorization", google.Authorization(token))
    r := http.GetWith("https://storage.googleapis.com/storage/v1/b?project=" + account.project_id, headers)
    println http.Status(r)
    google.Forget(account)
end

Forget

func Forget(sa: ServiceAccount)
Throws away the kept token of the account, so the next one is fetched fresh.

The example for this item is shown with the ones above: google-Token.salam

Authorization

func Authorization(token: str): str
The value of the Authorization header with the token.

The example for this item is shown with the ones above: google-Account.salam