99 "encoding/json"
1010 "errors"
1111 "fmt"
12+ "net/url"
1213 "strings"
1314 "time"
1415
@@ -151,14 +152,16 @@ func (f *credentialsFile) tokenSource(ctx context.Context, scopes []string) (oau
151152// from Google Compute Engine (GCE)'s metadata server. It's only valid to use
152153// this token source if your program is running on a GCE instance.
153154// If no account is specified, "default" is used.
155+ // If no scopes are specified, a set of default scopes are automatically granted.
154156// Further information about retrieving access tokens from the GCE metadata
155157// server can be found at https://cloud.google.com/compute/docs/authentication.
156- func ComputeTokenSource (account string ) oauth2.TokenSource {
157- return oauth2 .ReuseTokenSource (nil , computeSource {account : account })
158+ func ComputeTokenSource (account string , scope ... string ) oauth2.TokenSource {
159+ return oauth2 .ReuseTokenSource (nil , computeSource {account : account , scopes : scope })
158160}
159161
160162type computeSource struct {
161163 account string
164+ scopes []string
162165}
163166
164167func (cs computeSource ) Token () (* oauth2.Token , error ) {
@@ -169,7 +172,13 @@ func (cs computeSource) Token() (*oauth2.Token, error) {
169172 if acct == "" {
170173 acct = "default"
171174 }
172- tokenJSON , err := metadata .Get ("instance/service-accounts/" + acct + "/token" )
175+ tokenURI := "instance/service-accounts/" + acct + "/token"
176+ if len (cs .scopes ) > 0 {
177+ v := url.Values {}
178+ v .Set ("scopes" , strings .Join (cs .scopes , "," ))
179+ tokenURI = tokenURI + "?" + v .Encode ()
180+ }
181+ tokenJSON , err := metadata .Get (tokenURI )
173182 if err != nil {
174183 return nil , err
175184 }
0 commit comments