github.com/google/go-github/v42@v42.0.0/example/tokenauth/main.go (about) 1 // Copyright 2020 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 // The tokenauth command demonstrates using the oauth2.StaticTokenSource. 7 // You can test out a GitHub Personal Access Token using this simple example. 8 // You can generate them here: https://github.com/settings/tokens 9 package main 10 11 import ( 12 "context" 13 "fmt" 14 "log" 15 "syscall" 16 17 "github.com/google/go-github/v42/github" 18 "golang.org/x/crypto/ssh/terminal" 19 "golang.org/x/oauth2" 20 ) 21 22 func main() { 23 fmt.Print("GitHub Token: ") 24 byteToken, _ := terminal.ReadPassword(int(syscall.Stdin)) 25 println() 26 token := string(byteToken) 27 28 ctx := context.Background() 29 ts := oauth2.StaticTokenSource( 30 &oauth2.Token{AccessToken: token}, 31 ) 32 tc := oauth2.NewClient(ctx, ts) 33 34 client := github.NewClient(tc) 35 36 user, resp, err := client.Users.Get(ctx, "") 37 if err != nil { 38 fmt.Printf("\nerror: %v\n", err) 39 return 40 } 41 42 // Rate.Limit should most likely be 5000 when authorized. 43 log.Printf("Rate: %#v\n", resp.Rate) 44 45 // If a Token Expiration has been set, it will be displayed. 46 if !resp.TokenExpiration.IsZero() { 47 log.Printf("Token Expiration: %v\n", resp.TokenExpiration) 48 } 49 50 fmt.Printf("\n%v\n", github.Stringify(user)) 51 }