Skip to content

Improve document about authenticating with GitHub Apps #1069

@dorneanu

Description

@dorneanu

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:

$ curl -i -H "Authorization: Bearer $JWT_TOKEN" -H "Accept: application/vnd.github.machine-man-preview+json" https://api.github.com/app/installations

I can also get the installation ID.

Here is my code:

package main

import (
    "context"
    "net/http"
                                                                                                                                                                                                                         "github.com/bradleyfalzon/ghinstallation"
    "github.com/google/go-github/github"
    "github.com/sirupsen/logrus"
)

func main() {
    ctx := context.Background()

    // Set here you application ID and installation id                                                                                                                                                                   
    appId := 111111                                                                                                                                                                                                       
    installationId := 22222       
                                                                                                                                                                                                                                                                                                                                                                                                           
    // Wrap the shared transport for use with defined application and installation IDs                                                                                                                                   
    itr, err := ghinstallation.NewKeyFromFile(http.DefaultTransport, appId, installationId, "/tmp/key.pem")                                                                                                              
    if err != nil {                                                                                                                                                                                                           
         logrus.Error(err)
    }
    access_token, _ := itr.Token()
    logrus.Infof("Installation access token: %s", access_token)

    // Use installation transport with client
    // NewClient returns a new GitHub API client.
    // If a nil httpClient is provided, http.DefaultClient will be used. To use API methods which require authentication,
    // provide an http.Client that will perform the authentication for you.                                                                                                                                              
    client := github.NewClient(&http.Client{Transport: itr})                                                                                                                                                                                                                                                                                                                                                                                  
 
    // Get org installation                                                                                                                                                                                               
    appService := client.Apps                                                                                                                                                                                            
    appInst, resp, err := appService.FindOrganizationInstallation(ctx, "Scout24")                                                                                                                                        
    if err != nil {                                                                                                                                                                                                          
       logrus.Infof("error: %s", err)
    }

    // installations, resp, err := appService.ListInstallations(ctx, nil)
    logrus.Info(resp)
    logrus.Info(appInst)
}

And the output is:

INFO[0000] Installation access token: v1.4933ed8b240cb8be4xxxxxxxxxxxxxxxxxxx
INFO[0001] error: GET https://api.github.com/orgs/Scout24/installation: 401 A JSON web token could not be decoded []
INFO[0001] &{0xc000130090 0 0 0 0 github.Rate{Limit:0, Remaining:0, Reset:github.Timestamp{0001-01-01 00:00:00 +0000 UTC}}}
INFO[0001] <nil>

Obviously I do get the installation access token however when I try to get the organization installation it failed with:

error: GET https://api.github.com/orgs/Scout24/installation: 401 A JSON web token could not be decoded []

I guess theAuthorization: Bearer $JWT_TOKEN header is not properly set.
Any ideas?

Thanks in advance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions