Hi,
I have integrated with JIRA using OAuth 1.0.
so i have access token ,consumer key,oauth signature in my hand.
For attachment API we have to send a request with file(type as multipart form data)
is it possible to add an attachment to an issue using oauth 1.0 authentication method.
if it is possible means please share the code snippets here.
Thanks in advance.
In Java:
Note that this code is just a very blunt example, you may have to adjust some values as per your further requirements.
package OAuth;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.UUID;
import com.atlassian.oauth.client.example.JiraOAuthGetAccessToken;
import com.atlassian.oauth.client.example.JiraOAuthTokenFactory;
import com.google.api.client.auth.oauth.OAuthParameters;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpMediaType;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.InputStreamContent;
import com.google.api.client.http.MultipartContent;
import com.google.api.client.http.javanet.NetHttpTransport;
public class SampleAttachmentUpload {
private static String endpointurl= "http://localhost/rest/api/2/issue/TEST-1/attachments";
public static final String CONSUMER_KEY = "ConsumerKeyValue";
public static final String PRIVATE_KEY = "PrivateKeyValue";
public static final String ACCESS_TOKEN = "AccessTokenValue";
public static final String SECRET = "SecretValue";
public static final String JIRA_HOME = "http://localhost";
public static void createAttachmentRequest() throws Exception {
String filepath= "C:\\development\\jiraadapter\\sample.zip";
File initialFile = new File(filepath);
String fileName= initialFile.getName();
System.out.println("filename is: " + fileName );
InputStream inputStream = new FileInputStream(initialFile);
MultipartContent.Part part = new MultipartContent.Part()
.setContent(new InputStreamContent("application/gzip", inputStream))
.setHeaders(new HttpHeaders().set(
"Content-Disposition",
String.format("form-data; name=\"file\"; filename=\"%s\"", fileName) // TODO: escape fileName?
));
MultipartContent content = new MultipartContent()
.setMediaType(new HttpMediaType("multipart/form-data").setParameter("boundary", UUID.randomUUID().toString()))
.addPart(part);
String jiraBaseURL= "http://localhost";
JiraOAuthTokenFactory oAuthGetAccessTokenFactory = new JiraOAuthTokenFactory(jiraBaseURL);
JiraOAuthGetAccessToken oAuthAccessToken = oAuthGetAccessTokenFactory.getJiraOAuthGetAccessToken(ACCESS_TOKEN, SECRET, CONSUMER_KEY, PRIVATE_KEY);
oAuthAccessToken.verifier = SECRET;
OAuthParameters parameters= oAuthAccessToken.createParameters();
GenericUrl jiraUrl= new GenericUrl(endpointurl);
HttpRequestFactory requestFactory = new NetHttpTransport().createRequestFactory(parameters);
HttpRequest request = requestFactory.buildPostRequest(jiraUrl, content);
request.getHeaders().put("X-Atlassian-Token", "nocheck");
request.execute();
}
}
Thank you, Peter!
You saved my time.
It works like a charm. :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
See https://confluence.atlassian.com/jirakb/how-to-attach-an-attachment-in-a-jira-issue-using-rest-api-699957734.html for the code you need to attach things, and https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis/jira-rest-api-tutorials/jira-rest-api-example-oauth-authentication for using oauth
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.