Skip to content

Instantly share code, notes, and snippets.

@ferryzhou
Created November 30, 2015 04:15
Show Gist options
  • Save ferryzhou/c8e4437ab4367e224be6 to your computer and use it in GitHub Desktop.
Save ferryzhou/c8e4437ab4367e224be6 to your computer and use it in GitHub Desktop.
google api go client initialize
// https://github.com/google/google-api-go-client
import (
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
"google.golang.org/api/compute/v1"
)
func main() {
// Use oauth2.NoContext if there isn't a good context to pass in.
ctx := context.Background()
client, err := google.DefaultClient(ctx, compute.ComputeScope)
if err != nil {
//...
}
computeService, err := compute.New(client)
if err != nil {
//...
}
}
// token
ts, err := google.DefaultTokenSource(ctx, scope1, scope2, ...)
if err != nil {
//...
}
client := oauth2.NewClient(ctx, ts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment