Debian Codegraph @

example/tokenauth/main.go

raw
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 a Personal Access Token (PAT) to
7 // authenticate with GitHub.
8 // You can test out a GitHub Personal Access Token using this simple example.
9 // You can generate them here: https://github.com/settings/tokens
10 package main
11
12 import (
13 "context"
14 "fmt"
15 "log"
16 "os"
17
18 "github.com/google/go-github/v60/github"
19 "golang.org/x/term"
20 )
21
22 func main() {
23 fmt.Print("GitHub Token: ")
24 byteToken, _ := term.ReadPassword(int(os.Stdin.Fd()))
25 println()
26 token := string(byteToken)
27
28 ctx := context.Background()
29 client := github.NewClient(nil).WithAuthToken(token)
30
31 user, resp, err := client.Users.Get(ctx, "")
32 if err != nil {
33 fmt.Printf("\nerror: %v\n", err)
34 return
35 }
36
37 // Rate.Limit should most likely be 5000 when authorized.
38 log.Printf("Rate: %#v\n", resp.Rate)
39
40 // If a Token Expiration has been set, it will be displayed.
41 if !resp.TokenExpiration.IsZero() {
42 log.Printf("Token Expiration: %v\n", resp.TokenExpiration)
43 }
44
45 fmt.Printf("\n%v\n", github.Stringify(user))
46 }