How to generate jwt token for JIRA API

僤鯓⒐⒋嵵緔 提交于 2019-12-06 12:59:25

I used SoapUI Groovy script to generate JWT token for ZAPI.

Note :

  1. Library version should be 'commons-lang-2.6.jar' and 'commons-codec-1.11.jar' in lib folder.

  2. Add 'jwt-generator.jar' and 'zfj-cloud-rest-client-3.0.jar' in the bin -> ext folder. jwt generator

  3. JWT token will be created specific to API url entered. e.g :

    String createCycleUri = zephyrBaseUrl + "/public/rest/api/1.0/cycle";

  4. Mention api HTTP method type to before generate jwt token.

    String jwt = jwtGenerator.generateJWT("POST", uri, expirationInSec);

  5. Site always fixed for any ZAPI api. This will take JWT token.

https://prod-api.zephyr4jiracloud.com/connect

Which is mentioned in document.

https://zfjcloud.docs.apiary.io/#

  1. Your created Site url will be used if you use JIRA api mentioned in below document. This will take basic Auth.

https://getzephyr.docs.apiary.io/#

e.g :

https://newapi.atlassian.net

Groovy :

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import com.thed.zephyr.cloud.rest.ZFJCloudRestClient;
import com.thed.zephyr.cloud.rest.client.JwtGenerator;

def site = context.expand( '${#Project#Site}' )
def zapiAccessKey = context.expand( '${#Project#zapiAccessKey}' )
def secretID = context.expand( '${#Project#secretID}' )
def user = context.expand( '${#Project#user}' )

    // Replace Zephyr BaseUrl with the <ZAPI_CLOUD_URL> shared with ZAPI Cloud Installation
    String zephyrBaseUrl = site;
    // zephyr accessKey , we can get from Addons >> zapi section
    String accessKey = zapiAccessKey;
    // zephyr secretKey , we can get from Addons >> zapi section
    String secretKey = secretID;
    // Jira UserName
    String userName = user;

    ZFJCloudRestClient client = ZFJCloudRestClient.restBuilder(zephyrBaseUrl, accessKey, secretKey, userName).build();
    JwtGenerator jwtGenerator = client.getJwtGenerator();

    // API to which the JWT token has to be generated
    String createCycleUri = zephyrBaseUrl + "/public/rest/api/1.0/cycle";

    URI uri = new URI(createCycleUri);

    int expirationInSec = 3600;
    String jwt = jwtGenerator.generateJWT("POST", uri, expirationInSec);

  log.info "JWT Token : " + jwt

  //Store token in property.
  context.testCase.testSuite.project.setPropertyValue('JWT_Token', jwt)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!