Skip to content

Commit 9d409b9

Browse files
committed
Merge branch 'jsonkeyfile'
2 parents 948919c + 3314367 commit 9d409b9

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

google/example_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,34 @@ func Example_webServer() {
4848
client.Get("...")
4949
}
5050

51+
func Example_serviceAccountsJSON() {
52+
// Your credentials should be obtained from the Google
53+
// Developer Console (https://console.developers.google.com).
54+
// Navigate to your project, then see the "Credentials" page
55+
// under "APIs & Auth".
56+
// To create a service account client, click "Create new Client ID",
57+
// select "Service Account", and click "Create Client ID". A JSON
58+
// key file will then be downloaded to your computer.
59+
config, err := google.NewServiceAccountJSONConfig(
60+
"/path/to/your-project-key.json",
61+
"https://www.googleapis.com/auth/bigquery",
62+
)
63+
if err != nil {
64+
log.Fatal(err)
65+
}
66+
// Initiate an http.Client. The following GET request will be
67+
// authorized and authenticated on the behalf of
68+
// your service account.
69+
client := http.Client{Transport: config.NewTransport()}
70+
client.Get("...")
71+
72+
// If you would like to impersonate a user, you can
73+
// create a transport with a subject. The following GET
74+
// request will be made on the behalf of user@example.com.
75+
client = http.Client{Transport: config.NewTransportWithUser("user@example.com")}
76+
client.Get("...")
77+
}
78+
5179
func Example_serviceAccounts() {
5280
// Your credentials should be obtained from the Google
5381
// Developer Console (https://console.developers.google.com).

google/google.go

+25
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package google
1515

1616
import (
1717
"encoding/json"
18+
"io/ioutil"
1819
"net/http"
1920
"path"
2021
"time"
@@ -61,6 +62,30 @@ func NewServiceAccountConfig(opts *oauth2.JWTOptions) (*oauth2.JWTConfig, error)
6162
return oauth2.NewJWTConfig(opts, uriGoogleToken)
6263
}
6364

65+
// NewServiceAccountJSONConfig creates a new JWT config from a
66+
// JSON key file downloaded from the Google Developers Console.
67+
// See the "Credentials" page under "APIs & Auth" for your project
68+
// at https://console.developers.google.com.
69+
func NewServiceAccountJSONConfig(filename string, scopes ...string) (*oauth2.JWTConfig, error) {
70+
b, err := ioutil.ReadFile(filename)
71+
if err != nil {
72+
return nil, err
73+
}
74+
var key struct {
75+
Email string `json:"client_email"`
76+
PrivateKey string `json:"private_key"`
77+
}
78+
if err := json.Unmarshal(b, &key); err != nil {
79+
return nil, err
80+
}
81+
opts := &oauth2.JWTOptions{
82+
Email: key.Email,
83+
PrivateKey: []byte(key.PrivateKey),
84+
Scopes: scopes,
85+
}
86+
return NewServiceAccountConfig(opts)
87+
}
88+
6489
// NewComputeEngineConfig creates a new config that can fetch tokens
6590
// from Google Compute Engine instance's metaserver. If no account is
6691
// provided, default is used.

0 commit comments

Comments
 (0)