Atlassian Connect-Express:JIRA 插件中的 JIRA REST API 身份验证



我正在使用atlassian-connect-express工具包通过Node.js创建基于Atlassian Connect的附加组件。

它提供入站请求的自动 JWT 身份验证以及返回主机的出站请求的 JWT 签名。

当我在 JIRA 仪表板中安装它并返回以下付费负载时,该附加组件已通过身份验证:

{ key: 'my-add-on',
  clientKey: '*****',
  publicKey: '********'
  sharedSecret: '*****'
  serverVersion: '100082',
  pluginsVersion: '1.3.491',
  baseUrl: 'https://myaccount.atlassian.net',
  productType: 'jira',
  description: 'Atlassian JIRA at https://myaccount.atlassian.net ',
  eventType: 'installed' }

但是我无法使用框架生成的 JWT 令牌对 JIRA Rest API 进行身份验证。它抛出以下错误消息。

404 '{"errorMessages":["Issue does not exist or you do not have permission to see it."],"errors":{}}'

以下是我发送 GET 请求时的代码:

 app.get('/getissue', addon.authenticate(), function(req, res){
 var request = require('request');
      request({
           url: 'https://myaccount.atlassian.net/rest/api/2/issue/ABC-1', 
           method: 'GET',   
    }, function(error, response, body){
        if(error){
            console.log("error!");
         }else{
            console.log(response.statusCode, body);
          }
    }); 
    res.render('getissue');
});

下面是我的应用描述符文件的代码:

{
"key": "my-add-on",
"name": "Ping Pong",
"description": "My very first add-on",
"vendor": {
    "name": "Ping Pong",
    "url": "https://www.example.com"
},
"baseUrl": "{{localBaseUrl}}",
"links": {
    "self": "{{localBaseUrl}}/atlassian-connect.json",
    "homepage": "{{localBaseUrl}}/atlassian-connect.json"
},
"authentication": {
    "type": "jwt"
},
"lifecycle": {
    "installed": "/installed"
},
"scopes": [
    "READ",
    "WRITE"
],
"modules": {
    "generalPages": [
        {
            "key": "hello-world-page-jira",
            "location": "system.top.navigation.bar",
            "name": {
                "value": "Hello World"
            },
            "url": "/hello-world",
            "conditions": [{
                "condition": "user_is_logged_in"
            }]
        },
            {
                "key": "getissue-jira",
                "location": "system.top.navigation.bar",
                "name": {
                    "value": "Get Issue"
                },
                "url": "/getissue",
                "conditions": [{
                    "condition": "user_is_logged_in"
                    }]                
        }
    ]
}

}

我很确定这不是我正在做的正确方法,要么我应该使用 OAuth。但我想让用于身份验证的 JWT 方法在这里工作。

通过在此处签入 Atlassian Connect for Node.js Express Docs在JIRA附加组件中,签名的HTTP请求的工作方式如下。获取和发布两者。

获取:

app.get('/getissue', addon.authenticate(), function(req, res){
        var httpClient = addon.httpClient(req);
        httpClient.get('rest/api/2/issue/ABC-1',
        function(err, resp, body) {
                Response = JSON.parse(body);                              
                 if(err){
                    console.log(err);
                 }else {
                   console.log('Sucessful')
                   }
             });
            res.send(response);
         });

发布:

 var httpClient = addon.httpClient(req);
            var postdata = {
        "fields": {
           "project":
           { 
              "key": "MYW"
           },
           "summary": "My Story Name", 
            "description":"My Story Description", 
           "issuetype": {
              "name": "Story"
           }
          }
        }
        httpClient.post({
           url: '/rest/api/2/issue/' ,
           headers: {
                    'X-Atlassian-Token': 'nocheck'
                   },
           json: postdata
           },function (err, httpResponse, body) {
                if (err) {
                    return console.error('Error', err);
                      }
               console.log('Response',+httpResponse)
              });

您应该使用由 JIRA 初始化的全局变量"AP"以及您的附加组件执行。 您可以使用Chrome/Firefox Debug来探索它。

你试过打电话吗?

AP.request(..,...);

而不是"var request = require('request'(;"您可以在脚本的顶部设置以传递JS提示器和IDE验证:

/* global AP */

使用 AP 时,URL 应如下所示:

  url: /rest/api/2/issue/ABC-1  

而不是:

  url: https://myaccount.atlassian.net/rest/api/2/issue/ABC-1

我的假设是验证 ABC-1 问题和用户凭据,并且用户能够通过 JIRA UI 访问 ABC-1。

这是参考文档:https://developer.atlassian.com/cloud/jira/software/jsapi/request/

最新更新