gopi

package module
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 18, 2022 License: MIT Imports: 16 Imported by: 1

README

gopi

Golang Google Api. To access Google API, please download client_secret_file.json from Credentials in APIs & Services Menu in Your Google Cloud Console Project.

The flow

getClient + NewService + [body] = Do()

[] means optional

For Contributor

Environtment Variables

GOPROXY=proxy.golang.org

Testing before commit and push

go test
git tag

Release Version

git tag v0.1.2
git push origin --tags
go list -m github.com/awangga/gopi@v0.1.2

getClient

Open google api service with json credentials file and tokenfile, please run in localhost first to generate token.json with user confirmation. after that you may put token.json in your server.

NewService

Select service for docs,mail,drive etc.

Body

Generate json or dictionary for data post to Google API

DO()

Sending request with or without body into Google API and get response

Example

First thing is import google api module and others helpers you need, after that please define apiscope,jsonsecfile and tokenfile

var apiscope = []string{"https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/documents", "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/blogger", "https://www.googleapis.com/auth/gmail.send", "https://www.googleapis.com/auth/gmail.readonly"}

const jsonsecfile = "credentials.json"
const tokenfile = "token.json"
Reading google docs

First import library :

import "github.com/awangga/gopi"

After that use in your main package

func main() {
	client := gopi.GetClient(jsonsecfile, tokenfile, apiscope...)
	docssrv := gopi.GetService(client, "docs").(*docs.Service)
	doc := getDocsTitle(docssrv)
	fmt.Printf("The title of the doc is: %s\n", doc.Title)
}

func getDocsTitle(srv *docs.Service) *docs.Document {
	// Prints the title of the requested doc:
	// https://docs.google.com/document/d/195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE/edit
	docId := "195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE"
	doc, err := srv.Documents.Get(docId).Do()
	if err != nil {
		log.Fatalf("Unable to retrieve data from document: %v", err)
	}
	return doc
}

Thats all. If u want to catch response from google API just use doc (json format).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateTokenFile

func GenerateTokenFile(jsonsecfile string, tokFile string, apiscope ...string)

Requests a token from the web, then returns the retrieved token.

func GetClient

func GetClient(jsonsecfile string, tokFile string, apiscope ...string) *http.Client

Retrieves a token, saves the token, then returns the generated client.

func GetService added in v0.0.3

func GetService(client *http.Client, srvtype string) interface{}

retrive service from api

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL