-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Improve document about authenticating with GitHub Apps #1069
Comments
Hmmm... I'm not familiar with Also, the line |
I have the same issue. Was this ever resolved? |
@gauntface didn't we get this working? |
If it helps, I debugged the code and found that it was actually able to get an installation token (201 from GitHub). However, the actual response returned by the function is a 401. So I'm not sure what is happening internally. |
@dorneanu @willnorris I was running into this issue as well and found that you can only get certain endpoints when you authenticate as an installation, which is what the Edit: you can also use the |
I was able to solve using following code snippets: ...
// RefreshClient will refresh the github app client based on the github installation ID
func (a *GithubAppAuth) RefreshClient(githubInstID int64) {
itr, err := ghinstallation.New(http.DefaultTransport, a.githubAppID, githubInstID, a.githubAppKey)
if err != nil {
log.ErrorWithFields("Couldn't create new transporter", log.Fields{
"error": err,
"githubInstID": githubInstID,
})
}
// Get new token
client := github.NewClient(&http.Client{Transport: itr})
token, err := itr.Token(context.Background())
if err != nil {
log.ErrorWithFields("Couldn't refresh token", log.Fields{
"error": err,
"githubAppID": a.githubAppID,
"githubInstID": githubInstID,
})
}
... Then you can use the ...
// Clone will clone the repository using the repo service
func (i *githubAppInstallation) Clone(org, repoName, dstDir string, depth ...int) (*models.Repo, error) {
// Authenticating via GitHub App installation token
// The clone url has this syntax: https://x-access-token:<token>@github.com/<repo name>
if i.token == "" {
return nil, errors.New("No auth token was provided")
}
// Create URL for clone
url := fmt.Sprintf("https://x-access-token:%[email protected]/%s", i.token, repoName)
// If depth is not specified do a shallow clone
// Otherwise do a full clone
var gitCloneOpts *git.CloneOptions
if len(depth) == 0 {
gitCloneOpts = &git.CloneOptions{
URL: url,
// Create a shallow clone and therefore clone the repo
// with the history truncated only to the last commit
Depth: 10,
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
}
} else {
gitCloneOpts = &git.CloneOptions{
URL: url,
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
}
}
repo, err := git.PlainClone(dstDir, false, gitCloneOpts)
if err != nil {
return nil, err
}
.... I hope this helps. |
I ran into the same problem. The thing is that the client requires different authentication methods for different calls
I solved the problem by creating two separate clients - one for Apps calls, and the other for the rest: const gheBaseUrl = "https://my-ghe-installation-url/api/v3"
tr := http.DefaultTransport
appId := 99
installationId := 99v
jwtTransport, _ := ghinstallation.NewAppsTransportKeyFromFile(tr, appId, "path/to/key.pem")
installationTokenTransport := ghinstallation.NewFromAppsTransport(jwtTransport, installationId)
installationTokenTransport.BaseURL = gheBaseUrl
jwtTransport.BaseURL = gheBaseUrl
ghClient, _ := github.NewEnterpriseClient(gheBaseUrl, fmt.Sprintf("%s/upload", gheBaseUrl), &http.Client{Transport: installationTokenTransport})
appsGhClient, _ := github.NewEnterpriseClient(gheBaseUrl, fmt.Sprintf("%s/upload", gheBaseUrl),&http.Client{Transport: jwtTransport}) |
related to bradleyfalzon/ghinstallation#39 |
Closing inactive issue for lack of activity/interest. Feel free to reopen. |
Hi,
I'm currently trying to have a working example how to properly authenticate as a GitHub app in my organization. In the app settings I can find the
application ID
. Using:I can also get the
installation ID
.Here is my code:
And the output is:
Obviously I do get the
installation access token
however when I try to get the organization installation it failed with:I guess the
Authorization: Bearer $JWT_TOKEN
header is not properly set.Any ideas?
Thanks in advance.
The text was updated successfully, but these errors were encountered: