@@ -135,6 +135,7 @@ func parse(input string) map[string]string {
135135}
136136
137137func main () {
138+ ctx := context .Background ()
138139 flag .BoolVar (& verbose , "verbose" , false , "log debug information to stderr" )
139140 var device bool
140141 flag .BoolVar (& device , "device" , false , "instead of opening a web browser locally, print a code to enter on another device" )
@@ -259,7 +260,7 @@ func main() {
259260 if verbose {
260261 fmt .Fprintln (os .Stderr , "refreshing token..." )
261262 }
262- token , err = c .TokenSource (context . Background () , & oauth2.Token {RefreshToken : pairs ["oauth_refresh_token" ]}).Token ()
263+ token , err = c .TokenSource (ctx , & oauth2.Token {RefreshToken : pairs ["oauth_refresh_token" ]}).Token ()
263264 if err != nil {
264265 fmt .Fprintln (os .Stderr , "error during OAuth token refresh" , err )
265266 }
@@ -268,9 +269,9 @@ func main() {
268269 if token == nil {
269270 // Generate new token (opens browser, may require user input)
270271 if device {
271- token , err = getDeviceToken (c )
272+ token , err = getDeviceToken (ctx , c )
272273 } else {
273- token , err = getToken (c )
274+ token , err = getToken (ctx , c )
274275 }
275276 if err != nil {
276277 log .Fatalln (err )
@@ -356,7 +357,7 @@ var template string = `<!DOCTYPE html>
356357</body>
357358</html>`
358359
359- func getToken (c oauth2.Config ) (* oauth2.Token , error ) {
360+ func getToken (ctx context. Context , c oauth2.Config ) (* oauth2.Token , error ) {
360361 state := oauth2 .GenerateVerifier ()
361362 queries := make (chan url.Values )
362363 handler := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
@@ -425,19 +426,19 @@ func getToken(c oauth2.Config) (*oauth2.Token, error) {
425426 return nil , fmt .Errorf ("state mismatch" )
426427 }
427428 code := query .Get ("code" )
428- return c .Exchange (context . Background () , code , oauth2 .VerifierOption (verifier ))
429+ return c .Exchange (ctx , code , oauth2 .VerifierOption (verifier ))
429430}
430431
431- func getDeviceToken (c oauth2.Config ) (* oauth2.Token , error ) {
432- deviceAuth , err := c .DeviceAuth (context . Background () )
432+ func getDeviceToken (ctx context. Context , c oauth2.Config ) (* oauth2.Token , error ) {
433+ deviceAuth , err := c .DeviceAuth (ctx )
433434 if err != nil {
434435 log .Fatalln (err )
435436 }
436437 if verbose {
437438 fmt .Fprintf (os .Stderr , "%+v\n " , deviceAuth )
438439 }
439440 fmt .Fprintf (os .Stderr , "Please enter code %s at %s\n " , deviceAuth .UserCode , deviceAuth .VerificationURI )
440- return c .DeviceAccessToken (context . Background () , deviceAuth )
441+ return c .DeviceAccessToken (ctx , deviceAuth )
441442}
442443
443444func replaceHost (e oauth2.Endpoint , host string ) oauth2.Endpoint {
0 commit comments