-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
76 lines (68 loc) · 2.38 KB
/
example_test.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright 2020 The gg Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package ghdevice_test
import (
"context"
"fmt"
"os"
"gg-scm.io/pkg/ghdevice"
"github.com/google/go-github/v32/github"
"golang.org/x/oauth2"
)
func ExampleFlow() {
// Change this to identify you and/or your application to GitHub.
// See https://docs.github.com/en/free-pro-team@latest/rest/overview/resources-in-the-rest-api#user-agent-required
// for guidance.
const userAgent = "myapplicationname"
// Cancelling or adding a deadline to the context will interrupt the flow.
ctx := context.Background()
// Run the device flow, waiting for GitHub to return an access token
// after the user has finished accepting the permissions.
token, err := ghdevice.Flow(ctx, ghdevice.Options{
UserAgent: userAgent,
// Change this to your OAuth application client ID found in the
// GitHub web interface.
ClientID: "replacewithactualclientid",
// Change these to use the appropriate OAuth scopes for
// your application.
Scopes: []string{"public_repo", "read:user"},
// Prompter is a function to display login instructions to the user.
Prompter: func(ctx context.Context, p ghdevice.Prompt) error {
fmt.Fprintf(os.Stderr, "Visit %s in your browser and enter the code %s\n",
p.VerificationURL, p.UserCode)
fmt.Fprintf(os.Stderr, "Waiting...\n")
return nil
},
})
if err != nil {
// Handle error. For example:
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
// Use the access token to make GitHub API requests.
ts := oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: token,
})
ghClient := github.NewClient(oauth2.NewClient(ctx, ts))
ghClient.UserAgent = userAgent
repos, _, err := ghClient.Repositories.List(ctx, "", nil)
if err != nil {
// Handle error. For example:
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
_ = repos
}