Last active
August 2, 2019 02:38
-
-
Save mdgreenfield/0410347d7d42a0f2104fba5abe72c061 to your computer and use it in GitHub Desktop.
Test the G Suite directory API for 401 Issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"golang.org/x/oauth2/jwt" | |
directory "google.golang.org/api/admin/directory/v1" | |
"google.golang.org/api/googleapi" | |
"log" | |
"os" | |
) | |
const ( | |
tokenURI string = "https://accounts.google.com/o/oauth2/token" | |
) | |
var ( | |
// Docs: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority | |
// Permissions: https://www.googleapis.com/auth/admin.directory.user.readonly,https://www.googleapis.com/auth/admin.directory.group.readonly | |
scopes = []string{ | |
directory.AdminDirectoryUserReadonlyScope, | |
directory.AdminDirectoryGroupReadonlyScope, | |
} | |
) | |
func main() { | |
serviceaccount := "xyz" | |
privatekeyid := "REDACTED" | |
privatekey := "REDACTED" | |
email := "xyz" | |
config := &jwt.Config{ | |
Email: serviceaccount, | |
PrivateKey: []byte(privatekey), | |
PrivateKeyID: privatekeyid, | |
Scopes: scopes, | |
Subject: email, | |
TokenURL: tokenURI, | |
} | |
svc, err := directory.New(config.Client(context.Background())) | |
if err != nil { | |
log.Printf("failed to create directory service client", err) | |
os.Exit(1) | |
} | |
traceToken := "REDACTED" | |
_, err = svc.Users.Get(email).Projection("full").Do(googleapi.Trace(traceToken)) | |
if err != nil { | |
log.Printf("err: %s", err) | |
os.Exit(1) | |
} | |
log.Print("Request successful") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment