cancel
Showing results for 
Search instead for 
Did you mean: 

JWT generation

hellinodreman
New Contributor

JWT generation

I am trying to generate a JWT using python. I followed the given example in the documentation, but I always get error 400. 

 

I am using the following code:

# BASE URL for Zephyr for Jira Cloud
BASE_URL = 'https://prod-api.zephyr4jiracloud.com/connect'

# RELATIVE PATH for token generation and make request to api
RELATIVE_PATH = '/public/rest/api/1.0/cycle'

# CANONICAL PATH (Http Method & Relative Path & Query String)
CANONICAL_PATH = 'POST&'+RELATIVE_PATH+'&'


# TOKEN HEADER: to generate jwt token
payload_token = {
'sub': ACCOUNT_ID,
'qsh': hashlib.sha256(CANONICAL_PATH.encode('utf-8')).hexdigest(),
'iss': ACCESS_KEY,
'exp': int(time.time())+JWT_EXPIRE,
'iat': int(time.time())
}


# GENERATE TOKEN
token = jwt.encode(payload_token, SECRET_KEY, algorithm='HS256').strip()


headers = {
'Content-Type': 'application/json',
'Authorization': 'JWT '+token,
'zapiAccessKey': ACCESS_KEY
}

# REQUEST PAYLOAD: to create cycle
cycle = {
'name': 'Sample Cycle',
'projectId': '10200',
'versionId': '5.X'
}

raw_result = requests.post(BASE_URL + RELATIVE_PATH, headers=headers, json=cycle)

Could anyone tell me what I'm doing wrong? 

4 REPLIES 4

Re: JWT generation

 

Hi 

 

In your code  "decode('utf-8')" missing in jwt generation. Attaching the sample working code, you can replace the access, secret, and account id with proper details and rename the attached file with .py extension and try again

Also sample code for both Java and Python you can get from: https://github.com/zephyrdeveloper/zapi-cloud

 

Thanks,

Jagadeesh

hellinodreman
New Contributor

Re: JWT generation

Hi, thanks. I had the decode function, but I don't know if don't have the right jwt library, because when using it I get this error:

 

token = jwt.encode(payload_token, SECRET_KEY, algorithm='HS256').strip().decode('utf-8')
AttributeError: 'str' object has no attribute 'decode'

Re: JWT generation

 

Can you try installing  below version and then try again

pip3 install pyjwt==1.5.3

hellinodreman
New Contributor

Re: JWT generation

It works!!! Thank you!!

New Here?
Join us and watch the welcome video: