如何为JIRA API生成jwt令牌

时间:2018-01-17 10:30:46

标签: java jira jira-zephyr

我正在尝试生成用于授权API的jwt令牌。首先,我无法从JIRA找到任何令牌生成器api。经过大量搜索后,我找到了一个生成jwt令牌的java代码,但在导入zephyr库时出错。

错误:

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代码:

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

2 个答案:

答案 0 :(得分:0)

我使用SoapUI Groovy脚本为ZAPI生成JWT令牌。

注意:

  1. 图书馆版本应该是' commons-lang-2.6.jar'和' commons-codec-1.11.jar'在lib文件夹中。

  2. 添加&jwt-generator.jar'和' zfj-cloud-rest-client-3.0.jar'在垃圾箱里 - > ext文件夹。 jwt generator

  3. 将创建特定于输入的API网址的JWT令牌。 例如:

      

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

  4. 在生成jwt标记之前提及api HTTP方法类型。

      

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

  5. 网站始终针对任何ZAPI API进行修复。这将采用JWT令牌。

  6.   

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

    文件中提到了这一点。

      

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

    1. 如果您使用下面文档中提到的JIRA api,将使用您创建的网站网址。这将采用基本的Auth。
    2.   

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

      例如:

        

      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)
      

答案 1 :(得分:0)

我认为,对于Cloud Jira,标记的答案是正确的。但是,服务器ZAPI用作内部Jira API。因此,您只需要对Jira进行基本身份验证即可。

  

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

这是获取ZQL请求的示例。