Hello, i'm trying to access to JIRA ressource using 'com.atlassian.jira:jira-rest-java-client-core:5.0.4' and OAUth indentification protocol.
I configure the following dependencies
dependencies {
// Dependencies to jira-rest-java-client-core
compile 'com.atlassian.jira:jira-rest-java-client-core:5.0.4'
compile 'org.apache.httpcomponents:httpcore:4.4.8'
compile 'org.apache.httpcomponents:httpcore-nio:4.4.8'
compile 'com.atlassian.fugue:fugue:2.7.0'
// Bellow is the a simple binding to the "standard" error output stream
compile 'org.slf4j:slf4j-simple:1.7.10'
// Dependencies to net.oauth.core
compile 'net.oauth.core:oauth:20100527'
compile 'net.oauth.core:oauth-consumer:20100527'
compile 'net.oauth.core:oauth-httpclient4:20090913'
// JUNIT
testCompile 'junit:junit:4.12'
}
My JIRAis already configure for an OAuth access, and i have a consumer key, a private key and an access_token.
I try to retrieve an issue using following code.
import java.net.URI;
import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.atlassian.httpclient.api.Request.Builder;
import com.atlassian.jira.rest.client.api.AuthenticationHandler;
import com.atlassian.jira.rest.client.api.JiraRestClient;
import com.atlassian.jira.rest.client.api.JiraRestClientFactory;
import com.atlassian.jira.rest.client.api.domain.Issue;
import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory;
import com.thalesgroup.atms.jira.internal.JiraAccessServicesTest;
import net.oauth.OAuth;
import net.oauth.OAuthAccessor;
import net.oauth.OAuthConsumer;
import net.oauth.OAuthMessage;
import net.oauth.OAuthServiceProvider;
import net.oauth.ParameterStyle;
import net.oauth.client.OAuthClient;
import net.oauth.http.HttpMessage;
import net.oauth.signature.RSA_SHA1;
/**
* Test for OAuth authentification
*/
public class OAuthTest {
/** A Logger. */
private static final Logger LOG = LoggerFactory.getLogger(JiraAccessServicesTest.class.getName());
/** SERVLET_BASE_URL */
private static final String SERVLET_BASE_URL = "/plugins/servlet";
/** A Jira Consumer key. */
private String consumerKey = "";
/** A Jira access token. */
String access_token;
/** A Jira private key. */
private String privateKey;
/** JIRA URL */
String baseUrl;
/** OAuth accessor to JIRA. */
private OAuthAccessor accessorSingleton;
/**
* Declare the JIRA OAuth.
*/
@Before
public void oAuth() {
access_token = "_______thisIsAnAccessToken______";
consumerKey = "__consumer__";
privateKey = "ThisIsAPrivateKeyWithUpperAndLowerCaseAndNumber123AndSpecialChar\r\n"
+ "ThisIsAPrivateKeyWithUpperAndLowerCaseAndNumber123AndSpecialChar\r\n"
+ "ThisIsAPrivateKeyWithUpperAndLowerCaseAndNumber123AndSpecialChar\r\n"
+ "ThisIsAPrivateKeyWithUpperAndLowerCaseAndNumber123AndSpecialChar\r\n"
+ "ThisIsAPrivateKeyWithUpperAndLowerCaseAndNumber123AndSpecialChar\r\n"
+ "ThisIsAPrivateKeyWithUpperAndLowerCaseAndNumber123AndSpecialChar\r\n"
+ "ThisIsAPrivateKeyWithUpperAndLowerCaseAndNumber123AndSpecialChar\r\n"
+ "ThisIsAPrivateKeyWithUpperAndLowerCaseAndNumber123AndSpecialChar\r\n"
+ "ThisIsAPrivateKeyWithUpperAndLowerCaseAndNumber123AndSpecialChar\r\n"
+ "ThisIsAPrivateKeyWithUpperAndLowerCaseAndNumber123AndSpecialChar\r\n"
+ "ThisIsAPrivateKeyWithUpperAndLowerCaseAndNumber123AndSpecialChar\r\n"
+ "ThisIsAPrivateKeyWithUpperAndLowerCaseAndNumber123AndSpecialChar\r\n"
+ "ThisIsAPrivateKeyWithUpperAndLowerCaseAndNumber123AndSpecialChar\r\n"
+ "ThisIsAPrivate==";
baseUrl = "http://my.jira.instance:8070";
}
/**
*
* @throws Exception if occured.
*/
@Test
public void test() throws Exception {
final JiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
final URI jiraServerUri = new URI(baseUrl);
final JiraRestClient restClient = factory.create(jiraServerUri, new AuthenticationHandler() {
@Override
public void configure(final Builder builder) {
try {
final OAuthAccessor accessor = getAccessor();
accessor.accessToken = access_token;
final OAuthMessage request2 = accessor.newRequestMessage(null,
/* request.getUri().toString() */ baseUrl, Collections.<Map.Entry<?, ?>> emptySet());
final Object accepted = accessor.consumer.getProperty(OAuthConsumer.ACCEPT_ENCODING);
if (accepted != null) {
request2.getHeaders().add(
new OAuth.Parameter(HttpMessage.ACCEPT_ENCODING, accepted.toString()));
}
final Object ps = accessor.consumer.getProperty(OAuthClient.PARAMETER_STYLE);
final ParameterStyle style =
(ps == null) ? ParameterStyle.BODY : Enum.valueOf(ParameterStyle.class, ps.toString());
final HttpMessage httpRequest = HttpMessage.newRequest(request2, style);
for (final Entry<String, String> ap : httpRequest.headers) {
builder.setHeader(ap.getKey(), ap.getValue());
}
builder.setUri(httpRequest.url.toURI());
} catch (final Exception e) {
e.printStackTrace();
}
}
});
final Issue issue = restClient.getIssueClient().getIssue("ISSUEID-1234").claim();
LOG.info(issue.getSummary());
restClient.close();
}
/**
* OAuthAccessor
*
* @return OAuthAccessor
*/
final OAuthAccessor getAccessor() {
if (accessorSingleton == null) {
final OAuthServiceProvider serviceProvider =
new OAuthServiceProvider(getRequestTokenUrl(), getAuthorizeUrl(), getAccessTokenUrl());
final OAuthConsumer consumer = new OAuthConsumer(null, consumerKey, null, serviceProvider);
consumer.setProperty(RSA_SHA1.PRIVATE_KEY, privateKey);
consumer.setProperty(OAuth.OAUTH_SIGNATURE_METHOD, OAuth.RSA_SHA1);
accessorSingleton = new OAuthAccessor(consumer);
}
return accessorSingleton;
}
/**
* AccessTokenUrl
*
* @return AccessTokenUrl
*/
private String getAccessTokenUrl() {
return baseUrl + SERVLET_BASE_URL + "/oauth/access-token";
}
/**
* AuthorizeUrl
*
* @return AuthorizeUrl
*/
private String getAuthorizeUrl() {
return baseUrl + SERVLET_BASE_URL + "/oauth/authorize";
}
/**
* RequestTokenUrl
*
* @return RequestTokenUrl
*/
private String getRequestTokenUrl() {
return baseUrl + SERVLET_BASE_URL + "/oauth/request-token";
}
}
But it doesn't work ?
Which URL should I put in the newRequestMessage ?
What is the callbackURL of the OAuthConsumer ?
Thanks a lot for your help.