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).