【问题标题】:Http 401 error while invoking api in Jenkins pipeline在 Jenkins 管道中调用 api 时出现 Http 401 错误
【发布时间】:2022-07-29 21:27:38
【问题描述】:

我在从 Jenkins 管道中访问声纳 api 时收到 401 错误,但从邮递员那里我得到了具有相同凭据的相同 URL 的正确响应,但是在 Jenkins 中我得到以下错误

Response Code: HTTP/1.1 401 
20:26:38  Response: 

这是我的网址

http://www.sonarqube.com/api/measures/component?componentKey=projectKey&metricKeys=major_violations,info_violations,blocker_violations,minor_violations,critical_violations&includetrends=true&resolved=false

我已经尝试了这两种方法(如下),但没有什么对我有用。

def getJSONResp(url,credentials) {
echo(credentials)
def content
    String auth = credentials.bytes.encodeBase64().toString()
    def json = httpRequest  consoleLogResponseBody: true, 
                 httpMode: 'GET',
                 responseHandle: 'NONE', 
                 url: url,                       
                 customHeaders:[[name:'Authorization', value:"Basic ${auth}"]]
    content = readJSON text: json.content
    return content
}
def getJSONResp(url,credentials) {
echo(credentials)
def content
    def json = httpRequest  consoleLogResponseBody: true, 
                 httpMode: 'GET',
                 responseHandle: 'NONE', 
                 url: url,                       
                 customHeaders:[[name:'Authorization', value:"Basic ${credentials}"]]
    content = readJSON text: json.content
    return content
}

谁能帮我解决这个问题。

【问题讨论】:

    标签: api jenkins jenkins-pipeline jenkins-plugins sonarqube-api


    【解决方案1】:

    将冒号(:) 附加到身份验证令牌的末尾,假设实际身份验证令牌是“a26xxxxxxxxxxxxxx”,同时将凭据传递给基本身份验证在令牌末尾附加冒号作为“a26xxxxxxxxxxxxxx:”并调用方法

    def getJSONResp(url,credentials) {
    echo(credentials)
    def content
        def json = httpRequest  consoleLogResponseBody: true, 
                     httpMode: 'GET',
                     responseHandle: 'NONE', 
                     url: url,                       
                     customHeaders:[[name:'Authorization', value:"Basic ${credentials}"]]
        content = readJSON text: json.content
        return content
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-12
      • 1970-01-01
      相关资源
      最近更新 更多