本文介绍了如何为JIRA API生成jwt标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

错误:

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


推荐答案

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

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

注意:


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

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

添加'jwt-generator.jar'和'zfj-cloud -rest-client-3.0.jar'在bin - > ext文件夹中。

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

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

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


  • 在生成jwt令牌之前提及api HTTP方法类型。

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


  • 网站始终固定对于任何ZAPI api。这将采用JWT令牌。

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



    文件中提到了这一点。

    Which is mentioned in document.




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



    例如:

    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)
    

    这篇关于如何为JIRA API生成jwt标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 10-20 18:51