我试图通过他们的API来访问JIRA,不幸的是,当我试图通过jersey-client(版本2.30.1)访问他们时,我收到了401。
不幸的是,当我试图通过jersey-client(版本2.30.1)访问他们时,我收到了401。
Authentication error (401) for user [email protected]
下面是我的代码。
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("[email protected]", "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 [email protected]:myS3cr3tApiToken https://my-company.atlassian.net/rest/api/latest/search
那是什么原因呢?
我刚刚经历了,使用非抢先的工作对我来说,所以我只是使用以下功能,而不是上面的一个。
val feature = HttpAuthenticationFeature.basicBuilder()
.nonPreemptive()
.credentials("[email protected]", "myS3cr3tApiToken")
.build()
clientConfig.register(feature)