Forum Discussion

hellinodreman's avatar
hellinodreman
New Contributor
4 years ago

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

    • hellinodreman's avatar
      hellinodreman
      New Contributor

      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'

      • JagadeshMadhura's avatar
        JagadeshMadhura
        Staff
         

        Can you try installing  below version and then try again

        pip3 install pyjwt==1.5.3