1 // Copyright 2023 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package auth_test 16 17 import ( 18 "log" 19 20 "cloud.google.com/go/auth" 21 "cloud.google.com/go/auth/httptransport" 22 ) 23 24 func ExampleNew2LOTokenProvider() { 25 // Your credentials should be obtained from the Google 26 // Developer Console (https://console.developers.google.com). 27 opts := &auth.Options2LO{ 28 Email: "xxx@developer.gserviceaccount.com", 29 // The contents of your RSA private key or your PEM file 30 // that contains a private key. 31 // If you have a p12 file instead, you 32 // can use `openssl` to export the private key into a pem file. 33 // 34 // $ openssl pkcs12 -in key.p12 -passin pass:notasecret -out key.pem -nodes 35 // 36 // The field only supports PEM containers with no passphrase. 37 // The openssl command will convert p12 keys to passphrase-less PEM containers. 38 PrivateKey: []byte("-----BEGIN RSA PRIVATE KEY-----..."), 39 Scopes: []string{ 40 "https://www.googleapis.com/auth/bigquery", 41 "https://www.googleapis.com/auth/blogger", 42 }, 43 TokenURL: "https://oauth2.googleapis.com/token", 44 // If you would like to impersonate a user, you can 45 // create a transport with a subject. The following GET 46 // request will be made on the behalf of user@example.com. 47 // Optional. 48 Subject: "user@example.com", 49 } 50 51 tp, err := auth.New2LOTokenProvider(opts) 52 if err != nil { 53 log.Fatal(err) 54 } 55 client, err := httptransport.NewClient(&httptransport.Options{ 56 Credentials: auth.NewCredentials(&auth.CredentialsOptions{ 57 TokenProvider: tp, 58 }), 59 }) 60 if err != nil { 61 log.Fatal(err) 62 } 63 client.Get("...") 64 _ = tp 65 } 66