...

Source file src/github.com/google/go-github/v33/example/tokenauth/main.go

Documentation: github.com/google/go-github/v33/example/tokenauth

     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 the oauth2.StaticTokenSource.
     7  package main
     8  
     9  import (
    10  	"context"
    11  	"fmt"
    12  	"syscall"
    13  
    14  	"github.com/google/go-github/v33/github"
    15  	"golang.org/x/crypto/ssh/terminal"
    16  	"golang.org/x/oauth2"
    17  )
    18  
    19  func main() {
    20  	fmt.Print("GitHub Token: ")
    21  	byteToken, _ := terminal.ReadPassword(int(syscall.Stdin))
    22  	token := string(byteToken)
    23  
    24  	ctx := context.Background()
    25  	ts := oauth2.StaticTokenSource(
    26  		&oauth2.Token{AccessToken: token},
    27  	)
    28  	tc := oauth2.NewClient(ctx, ts)
    29  
    30  	client := github.NewClient(tc)
    31  
    32  	user, _, err := client.Users.Get(ctx, "")
    33  
    34  	if err != nil {
    35  		fmt.Printf("\nerror: %v\n", err)
    36  		return
    37  	}
    38  
    39  	fmt.Printf("\n%v\n", github.Stringify(user))
    40  }
    41  

View as plain text