forked from remote/oauth2
Adding support for Google Developers Console JSON key files.
This commit is contained in:
@@ -15,6 +15,7 @@ package google
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"path"
|
||||
"time"
|
||||
@@ -61,6 +62,30 @@ func NewServiceAccountConfig(opts *oauth2.JWTOptions) (*oauth2.JWTConfig, error)
|
||||
return oauth2.NewJWTConfig(opts, uriGoogleToken)
|
||||
}
|
||||
|
||||
// NewServiceAccountJSONConfig creates a new JWT config from a
|
||||
// JSON key file downloaded from the Google Developers Console.
|
||||
// See the "Credentials" page under "APIs & Auth" for your project
|
||||
// at https://console.developers.google.com.
|
||||
func NewServiceAccountJSONConfig(filename string, scopes ...string) (*oauth2.JWTConfig, error) {
|
||||
b, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var key struct {
|
||||
Email string `json:"client_email"`
|
||||
PrivateKey string `json:"private_key"`
|
||||
}
|
||||
if err := json.Unmarshal(b, &key); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
opts := &oauth2.JWTOptions{
|
||||
Email: key.Email,
|
||||
PrivateKey: []byte(key.PrivateKey),
|
||||
Scopes: scopes,
|
||||
}
|
||||
return NewServiceAccountConfig(opts)
|
||||
}
|
||||
|
||||
// NewComputeEngineConfig creates a new config that can fetch tokens
|
||||
// from Google Compute Engine instance's metaserver. If no account is
|
||||
// provided, default is used.
|
||||
|
||||
Reference in New Issue
Block a user