Example #1
0
func Example_jWTConfig() {
	conf, err := oauth2.NewJWTConfig(&oauth2.JWTOptions{
		Email: "*****@*****.**",
		// The contents of your RSA private key or your PEM file
		// that contains a private key.
		// If you have a p12 file instead, you
		// can use `openssl` to export the private key into a pem file.
		//
		//    $ openssl pkcs12 -in key.p12 -out key.pem -nodes
		//
		// It only supports PEM containers with no passphrase.
		PrivateKey: []byte("PRIVATE KEY CONTENTS"),
		Scopes:     []string{"SCOPE1", "SCOPE2"},
	},
		"https://provider.com/o/oauth2/token")
	if err != nil {
		log.Fatal(err)
	}

	// Initiate an http.Client, the following GET request will be
	// authorized and authenticated on the behalf of
	// [email protected].
	client := http.Client{Transport: conf.NewTransport()}
	client.Get("...")

	// If you would like to impersonate a user, you can
	// create a transport with a subject. The following GET
	// request will be made on the behalf of [email protected].
	client = http.Client{Transport: conf.NewTransportWithUser("*****@*****.**")}
	client.Get("...")
}
Example #2
0
func Example_jWTConfig() {
	conf, err := oauth2.NewJWTConfig(&oauth2.JWTOptions{
		Email: "*****@*****.**",
		// The path to the pem file. If you have a p12 file instead, you
		// can use `openssl` to export the private key into a pem file.
		// $ openssl pkcs12 -in key.p12 -out key.pem -nodes
		PEMFilename: "/path/to/pem/file.pem",
		Scopes:      []string{"SCOPE1", "SCOPE2"},
	},
		"https://provider.com/o/oauth2/token")
	if err != nil {
		log.Fatal(err)
	}

	// Initiate an http.Client, the following GET request will be
	// authorized and authenticated on the behalf of
	// [email protected].
	client := http.Client{Transport: conf.NewTransport()}
	client.Get("...")

	// If you would like to impersonate a user, you can
	// create a transport with a subject. The following GET
	// request will be made on the behalf of [email protected].
	client = http.Client{Transport: conf.NewTransportWithUser("*****@*****.**")}
	client.Get("...")
}
Example #3
0
// NewServiceAccountConfig creates a new JWT config that can
// fetch Bearer JWT tokens from Google endpoints.
func NewServiceAccountConfig(opts *oauth2.JWTOptions) (*oauth2.JWTConfig, error) {
	return oauth2.NewJWTConfig(opts, uriGoogleToken)
}