-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathmain.go
59 lines (49 loc) · 1.71 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
52
53
54
55
56
57
58
59
// Copyright 2015 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 basicauth command demonstrates using the github.BasicAuthTransport,
// including handling two-factor authentication. This won't currently work for
// accounts that use SMS to receive one-time passwords.
//
// Deprecation Notice: GitHub will discontinue password authentication to the API.
// You must now authenticate to the GitHub API with an API token, such as an OAuth access token,
// GitHub App installation access token, or personal access token, depending on what you need to do with the token.
// Password authentication to the API will be removed on November 13, 2020.
// See the tokenauth example for details.
package main
import (
"bufio"
"context"
"fmt"
"os"
"strings"
"github.com/google/go-github/v71/github"
"golang.org/x/term"
)
func main() {
r := bufio.NewReader(os.Stdin)
fmt.Print("GitHub Username: ")
username, _ := r.ReadString('\n')
fmt.Print("GitHub Password: ")
password, _ := term.ReadPassword(int(os.Stdin.Fd()))
tp := github.BasicAuthTransport{
Username: strings.TrimSpace(username),
Password: strings.TrimSpace(string(password)),
}
client := github.NewClient(tp.Client())
ctx := context.Background()
user, _, err := client.Users.Get(ctx, "")
// Is this a two-factor auth error? If so, prompt for OTP and try again.
if _, ok := err.(*github.TwoFactorAuthError); ok {
fmt.Print("\nGitHub OTP: ")
otp, _ := r.ReadString('\n')
tp.OTP = strings.TrimSpace(otp)
user, _, err = client.Users.Get(ctx, "")
}
if err != nil {
fmt.Printf("\nerror: %v\n", err)
return
}
fmt.Printf("\n%v\n", github.Stringify(user))
}