Skip to content

Commit dd254a0

Browse files
authored
Add example for app auth (#2240)
1 parent f334d81 commit dd254a0

File tree

4 files changed

+111
-1
lines changed

4 files changed

+111
-1
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ GitHub Apps authentication can be provided by the [ghinstallation](https://githu
121121
package.
122122

123123
```go
124-
import "github.com/bradleyfalzon/ghinstallation"
124+
import (
125+
"github.com/bradleyfalzon/ghinstallation/v2"
126+
"github.com/google/go-github/v41/github"
127+
)
125128

126129
func main() {
127130
// Wrap the shared transport for use with the integration ID 1 authenticating with installation ID 99.
@@ -137,6 +140,9 @@ func main() {
137140
}
138141
```
139142

143+
*Note*: In order to interact with certain APIs, for example writing a file to a repo, one must generate an installation token
144+
using the installation ID of the GitHub app and authenticate with the OAuth method mentioned above. See the examples.
145+
140146
### Rate Limiting ###
141147

142148
GitHub imposes a rate limit on all API clients. Unauthenticated clients are

example/newfilewithappauth/main.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright 2021 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+
// newfilewithappauth demonstrates the functionality of GitHub's app authentication
7+
// methods by fetching an installation access token and reauthenticating to GitHub
8+
// with OAuth configurations.
9+
package main
10+
11+
import (
12+
"context"
13+
"io/ioutil"
14+
"log"
15+
"net/http"
16+
"time"
17+
18+
"github.com/bradleyfalzon/ghinstallation/v2"
19+
"github.com/google/go-github/v41/github"
20+
"golang.org/x/oauth2"
21+
)
22+
23+
func main() {
24+
const gitHost = "https://git.api.com"
25+
26+
privatePem, err := ioutil.ReadFile("path/to/pem")
27+
if err != nil {
28+
log.Fatalf("failed to read pem: %v", err)
29+
}
30+
31+
itr, err := ghinstallation.NewAppsTransport(http.DefaultTransport, 10, privatePem)
32+
if err != nil {
33+
log.Fatalf("faild to create app transport: %v\n", err)
34+
}
35+
itr.BaseURL = gitHost
36+
37+
//create git client with app transport
38+
client, err := github.NewEnterpriseClient(
39+
gitHost,
40+
gitHost,
41+
&http.Client{
42+
Transport: itr,
43+
Timeout: time.Second * 30,
44+
})
45+
if err != nil {
46+
log.Fatalf("faild to create git client for app: %v\n", err)
47+
}
48+
49+
installations, _, err := client.Apps.ListInstallations(context.Background(), &github.ListOptions{})
50+
if err != nil {
51+
log.Fatalf("failed to list installations: %v\n", err)
52+
}
53+
54+
//capture our installationId for our app
55+
//we need this for the access token
56+
var installID int64
57+
for _, val := range installations {
58+
installID = val.GetID()
59+
}
60+
61+
token, _, err := client.Apps.CreateInstallationToken(
62+
context.Background(),
63+
installID,
64+
&github.InstallationTokenOptions{})
65+
if err != nil {
66+
log.Fatalf("failed to create installation token: %v\n", err)
67+
}
68+
69+
ts := oauth2.StaticTokenSource(
70+
&oauth2.Token{AccessToken: token.GetToken()},
71+
)
72+
oAuthClient := oauth2.NewClient(context.Background(), ts)
73+
74+
//create new git hub client with accessToken
75+
apiClient, err := github.NewEnterpriseClient(gitHost, gitHost, oAuthClient)
76+
if err != nil {
77+
log.Fatalf("failed to create new git client with token: %v\n", err)
78+
}
79+
80+
_, resp, err := apiClient.Repositories.CreateFile(
81+
context.Background(),
82+
"repoOwner",
83+
"sample-repo",
84+
"example/foo.txt",
85+
&github.RepositoryContentFileOptions{
86+
Content: []byte("foo"),
87+
Message: github.String("sample commit"),
88+
SHA: nil,
89+
})
90+
if err != nil {
91+
log.Fatalf("failed to create new file: %v\n", err)
92+
}
93+
94+
log.Printf("file written status code: %v", resp.StatusCode)
95+
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module github.com/google/go-github/v41
22

33
require (
4+
github.com/bradleyfalzon/ghinstallation/v2 v2.0.3
45
github.com/google/go-cmp v0.5.6
56
github.com/google/go-querystring v1.1.0
67
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
@@ -9,7 +10,9 @@ require (
910
)
1011

1112
require (
13+
github.com/golang-jwt/jwt/v4 v4.0.0 // indirect
1214
github.com/golang/protobuf v1.3.2 // indirect
15+
github.com/google/go-github/v39 v39.0.0 // indirect
1316
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
1417
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
1518
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
github.com/bradleyfalzon/ghinstallation/v2 v2.0.3 h1:ywF/8q+GVpvlsEuvRb1SGSDQDUxntW1d4kFu/9q/YAE=
2+
github.com/bradleyfalzon/ghinstallation/v2 v2.0.3/go.mod h1:tlgi+JWCXnKFx/Y4WtnDbZEINo31N5bcvnCoqieefmk=
3+
github.com/golang-jwt/jwt/v4 v4.0.0 h1:RAqyYixv1p7uEnocuy8P1nru5wprCh/MH2BIlW5z5/o=
4+
github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
15
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
26
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
37
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
48
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
59
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
610
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
11+
github.com/google/go-github/v39 v39.0.0 h1:pygGA5ySwxEez1N39GnDauD0PaWWuGgayudyZAc941s=
12+
github.com/google/go-github/v39 v39.0.0/go.mod h1:C1s8C5aCC9L+JXIYpJM5GYytdX52vC1bLvHEF1IhBrE=
713
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
814
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
915
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

0 commit comments

Comments
 (0)