+
Skip to content

Add tokenauth example #1551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载