Skip to content

GitHub App

  • Recommended one for services/bots
  • A GitHub App is its own identity, it acts as itself (e.g., @my-bot[bot]), not as a user.
  • Installation token

Install vs. Authorize GithubApps

  • Installed Github Apps: https://github.com/settings/installations
  • The app has access to your repos/org. It can act on its own (as a bot) using installation tokens. You (or an org admin) chose which repos it can see. This is the server-to-server side.

  • Authorized Github Apps: https://github.com/settings/apps/authorizations

  • You personally granted the app permission to act as you via OAuth. This is the user-to-server side - the app used the client ID + client secret flow to get a token that represents you.
  • Do things as you, e.g., know who you are, access resources scoped to your identity

Step 0: Create App

Step 1: Install App

Step 2: Generate JWT

  • Generate a JWT from the app's private key (valid for up to 10 minutes)
# requires: openssl
APP_ID="3363276"
pem_file="private-key.pem"

b64url() { openssl base64 -e -A | tr '+/' '-_' | tr -d '='; }

header=$(echo -n '{"alg":"RS256","typ":"JWT"}' | b64url)
now=$(date +%s)
payload=$(printf '{"iat":%d,"exp":%d,"iss":"%s"}' $((now - 60)) $((now + 600)) "$APP_ID" | b64url)
signature=$(echo -n "${header}.${payload}" | openssl dgst -sha256 -sign "$pem_file" | b64url)

jwt="${header}.${payload}.${signature}"

Step 3: Generate access token (installation token)

  • Find the installation ID and exchange the JWT for an installation access token
  • You can use the App Access Token just like a PAT (however it expires in 1 hour)
# list app installations to get the installation ID
curl -Lsf \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer $jwt" \
  -H "X-GitHub-Api-Version: 2022-11-28" \
  https://api.github.com/app/installations


# create an installation token
INSTALLATION_ID=123587880
curl -Lsf -X POST \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer $jwt" \
  -H "X-GitHub-Api-Version: 2022-11-28" \
  https://api.github.com/app/installations/$INSTALLATION_ID/access_tokens

# returns: {"token":"ghs_xxxx","expires_at":"...","permissions":{...}}

GH_ACCESS_TOKEN= # ghs_xxxx

Step 4: Use the access token

  • Use the access token (installation token) to call the API (acts as the app bot)

-

curl -Lsf \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer $GH_ACCESS_TOKEN" \
  -H "X-GitHub-Api-Version: 2022-11-28" \
  https://api.github.com/repos/hvitoi/tech-docs/pulls