rAJ
rAJ

Reputation: 1433

How to generate jwt token for JIRA API

I am trying to generate jwt token for authorizing the API. Firstly i can't find any token generator api from JIRA. After searching a lot, i found a java code to generate jwt token but it gives error while importing zephyr libraries.

Error :

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script4.groovy: 18: unable to resolve class com.thed.zephyr.cloud.rest.ZFJCloudRestClient @ line 18, column 1. import com.thed.zephyr.cloud.rest.ZFJCloudRestClient;

Java Code :

https://github.com/zephyrdeveloper/zapi-cloud/blob/master/Samples/src/com/thed/zapi/cloud/sample/sampleJwtGenerator.java

Upvotes: 2

Views: 4337

Answers (2)

Den Kos
Den Kos

Reputation: 1

Marked answer is correct, I think, for Cloud Jira. However, server ZAPI works as internal Jira API. So you need only basic auth into Jira.

your_jira_base_url/rest/zapi/latest/zql/executeSearch?zqlQuery=(project='25221')&maxRecords=3654

That's as example of request for getting ZQL.

Upvotes: 0

rAJ
rAJ

Reputation: 1433

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)

Upvotes: 2

Related Questions