import jwt
import time
# Set the JWT claims
payload = {
"iss": "your-app-id",
"iat": int(time.time()),
"exp": int(time.time()) + 300, # expire in 5 minutes
"sub": "your-username"
}
# Load the private key
with open("/path/to/private/key.pem", "r") as private_key_file:
private_key = private_key_file.read()
# Sign the JWT to generate the access token
access_token = jwt.encode(payload, private_key, algorithm="RS256")
print(access_token)
import requests
access_token = "your-generated-jwt-access-token"
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json"
}
response = requests.post("https://api.bitbucket.org/2.0/snippets", headers=headers)
print(response.content)
您可以将上面的代码替换为适合您自己的代码,并将位于JWT和API端点之间的代码插入到其中。 您应该能够成功地从Bitbucket API获取访问令牌。