-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
51 lines (41 loc) · 1.33 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"strings"
"golang.org/x/oauth2/google"
)
func main() {
var email, credentialsFile, scopeString string
flag.StringVar(&email, "email", "", "Superadmin email")
flag.StringVar(&credentialsFile, "file", "", "Path to the credentials.json file")
flag.StringVar(&scopeString, "scopes", "", "Comma-separated list of scopes")
flag.Parse()
if email == "" || credentialsFile == "" || scopeString == "" {
log.Fatal("Please provide email, credentials file, and scopes using -email, -file, and -scopes flags.")
}
scopes := strings.Split(scopeString, ",")
// Read the credentials file
credentialsJSON, err := ioutil.ReadFile(credentialsFile)
if err != nil {
log.Fatalf("Error reading credentials file: %v", err)
}
// Parse the JSON credentials to a Config object
conf, err := google.JWTConfigFromJSON(credentialsJSON, scopes...)
if err != nil {
log.Fatalf("Error parsing credentials: %v", err)
}
// Impersonate the superadmin email if needed
conf.Subject = email
// You can use the 'conf' to create an HTTP client with the OAuth2 token
// For this example, we'll just print the token.
tokenSource := conf.TokenSource(context.Background())
token, err := tokenSource.Token()
if err != nil {
log.Fatalf("Error fetching token: %v", err)
}
fmt.Printf(token.AccessToken)
}