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