Skip to content

Commit

Permalink
Add tokenauth example (#1551)
Browse files Browse the repository at this point in the history
  • Loading branch information
tteeoo authored Jun 17, 2020
1 parent 3312662 commit 57406a8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions example/basicauth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
// 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 (
Expand Down
40 changes: 40 additions & 0 deletions example/tokenauth/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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.
package main

import (
"context"
"fmt"
"syscall"

"github.com/google/go-github/v32/github"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/oauth2"
)

func main() {
fmt.Print("GitHub Token: ")
byteToken, _ := terminal.ReadPassword(int(syscall.Stdin))
token := string(byteToken)

ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)

client := github.NewClient(tc)

user, _, err := client.Users.Get(ctx, "")

if err != nil {
fmt.Printf("\nerror: %v\n", err)
return
}

fmt.Printf("\n%v\n", github.Stringify(user))
}

0 comments on commit 57406a8

Please sign in to comment.