Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import (
"fmt"
"golang.org/x/net/context"
cc "golang.org/x/oauth2/clientcredentials"
"io/ioutil"
"net/http"
"os"
)
type SpecialClient struct {
*http.Client
}
func main() {
client := NewClient(
os.Getenv("CLIENT_ID"),
os.Getenv("CLIENT_SECRET"),
)
// the client will update its token if it's expired
resp, err := client.Get("http://some.remote.resource/path/to/resource")
if err != nil {
panic(err)
}
body, _ := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
// If response code is 200 it was successful
if resp.StatusCode == 200 {
fmt.Println("The request was successful. Response below:")
fmt.Println(string(body))
} else {
fmt.Println("Could not perform request to the endpoind. Response below:")
fmt.Println(string(body))
}
}
func NewClient(cid, csec string) *SpecialClient {
// this should match whatever service has given you
// client credential access
config := &cc.Config{
ClientID: cid,
ClientSecret: csec,
TokenURL: "http://some.remote.resource/token",
Scopes: []string{"scope:name"},
}
// you can modify the client (for example ignoring bad certs or otherwise)
// by modifying the context
ctx := context.Background()
client := config.Client(ctx)
return &SpecialClient{client}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.