Skip to content

Commit d59b00a

Browse files
authored
Merge pull request github#23145 from alik42/patch-1
update to use python3.9 with jwt==1.3.1
2 parents f407d61 + bf4de1b commit d59b00a

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

content/developers/apps/building-github-apps/authenticating-with-github-apps.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,36 +98,45 @@ puts jwt
9898

9999
#### Using Python
100100

101-
Here is a similar script for generating a JWT in Python. Note you will have to use `pip install jwt` in order to use this script. This script will prompt you for the location of your PEM file, or you can pass it as an inline argument when you execute the script. Replace `YOUR_APP_ID` with the ID of your app. Make sure to enclose the value in single quotes.
101+
Here is a similar script for generating a JWT in Python. Note you will have to use `pip install jwt` in order to use this script. This script will prompt you for the location of your PEM file and your app's ID, or you can pass them as inline arguments when you execute the script.
102102
103103
```python{:copy}
104+
#!/usr/bin/env python3
104105
import jwt
105106
import time
106107
import sys
107108
109+
108110
# Get PEM file path
109111
if len(sys.argv) > 1:
110112
pem = sys.argv[1]
111113
else:
112114
pem = input("Enter path of private PEM file: ")
113115
116+
# Get the App ID
117+
if len(sys.argv) > 2:
118+
app_id = sys.argv[2]
119+
else:
120+
app_id = input("Enter your APP ID: ")
121+
114122
# Open PEM
115-
with open(pem, 'r') as pem_file:
116-
signing_key = pem_file.read()
123+
with open(pem, 'rb') as pem_file:
124+
signing_key = jwt.jwk_from_pem(pem_file.read())
117125
118126
payload = {
119127
# Issued at time
120128
'iat': int(time.time()),
121129
# JWT expiration time (10 minutes maximum)
122130
'exp': int(time.time()) + 600,
123-
# {% data variables.product.prodname_github_app %}'s identifier
124-
'iss': 'YOUR_APP_ID'
131+
# GitHub App's identifier
132+
'iss': app_id
125133
}
126134

127135
# Create JWT
128-
encoded_jwt = jwt.encode(payload, signing_key, algorithm='RS256')
136+
jwt_instance = jwt.JWT()
137+
encoded_jwt = jwt_instance.encode(payload, signing_key, alg='RS256')
129138

130-
print(f"JWT: ", encoded_jwt())
139+
print(f"JWT: ", encoded_jwt)
131140
```
132141
133142
Use your {% data variables.product.prodname_github_app %}'s identifier (`YOUR_APP_ID`) as the value for the JWT [iss](https://tools.ietf.org/html/rfc7519#section-4.1.1) (issuer) claim. You can obtain the {% data variables.product.prodname_github_app %} identifier via the initial webhook ping after [creating the app](/apps/building-github-apps/creating-a-github-app/), or at any time from the app settings page in the GitHub.com UI.

0 commit comments

Comments
 (0)