尽管curl有效,JerseyClient在JIRA API上返回401

时间:2020-05-13 16:07:50

标签: curl jersey basic-authentication jira-rest-api

我正在尝试通过其API访问JIRA。

不幸的是,当我尝试通过jersey-client(版本2.30.1)访问它们时,我收到401消息

Authentication error (401) for user user@my-company.com

这是我的代码:

import org.glassfish.jersey.client.ClientConfig
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature
import javax.ws.rs.client.Client
import javax.ws.rs.client.ClientBuilder
import javax.ws.rs.client.Invocation
import javax.ws.rs.client.WebTarget
import javax.ws.rs.core.MediaType

....

    val clientConfig = ClientConfig()

    val feature = HttpAuthenticationFeature.basic("user@my-company.com", "myS3cr3tApiToken")
    clientConfig.register(feature)

    val client: Client = ClientBuilder.newClient(clientConfig)
    val webTarget: WebTarget = client.target("https://my-company.atlassian.net").path("/rest/api/latest/search")

    val invocationBuilder: Invocation.Builder = webTarget.request(MediaType.APPLICATION_JSON)
    val response = invocationBuilder.get()

如果我对curl进行相同的操作,一切正常: curl --user user@my-company.com:myS3cr3tApiToken https://my-company.atlassian.net/rest/api/latest/search

这可能是什么原因?

1 个答案:

答案 0 :(得分:0)

我刚刚经历了使用非抢占式作品的经历。 所以我只使用了以下功能,而不是上面的功能:

val feature = HttpAuthenticationFeature.basicBuilder()
        .nonPreemptive()
        .credentials("user@my-company.com", "myS3cr3tApiToken")
        .build()
    clientConfig.register(feature)