Gerring ERROR System.HttpResponse[Status=Unauthorized, StatusCode=401]

  • 1
  • Problem
  • Updated 3 years ago
Hi ... I am getting following error in salesforce when I try to create a JIRA issue from salesforce when I click on button.

ERROR : 
[21]|System.HttpRequest[Endpoint=https://222222.atlassian.net/rest/customware/connector/1.0/2/Case/5002800000AsyOr/issue/create.json, Method=POST]
13:28:06.9 (774418702)|CALLOUT_RESPONSE|[21]|System.HttpResponse[Status=Unauthorized, StatusCode=401]

My code below: 
JIRA Class -

public with sharing class JIRA {
 
    // Change values in this class according to you JIRA/Salesforce coordinates.
 
    public static String baseUrl = 'https://222222.atlassian.net'; // Base URL of your JIRA instance
    public static String systemId = '2'; // Salesforce Connector System ID in JIRA
    public static String username = 'xxxxxxxxxxx@gmail.com';  // JIRA username
    public static String password = 'xxxxxxxxxx'; // JIRA password
 
    public static String agentProfileName = 'System Administrator'; // Jira agent profile name in Salesforce
 
    // Constructs Basic Http Authentication header from provided credentials
    public static String authHeader(String u, String p) {
        Blob headerValue = Blob.valueOf(u + ':' + p);
        return 'Basic ' + EncodingUtil.base64Encode(headerValue);
    }
 
    // Sends a request and returns the response
    public static HttpResponse sendRequest(HttpRequest req) {
        Http http = new Http();
        return http.send(req);
    }
 
    // Detects whether current user is not JIRA agent. By calling this you can make sure that
    // infinite loops won't happen in triggers (for instance when synchronizing an issue with JIRA)
    public static Boolean currentUserIsNotJiraAgent() {
        Boolean allow = false;
        List<Profile> jiraAgentProfile = [SELECT Id FROM Profile WHERE Name=:JIRA.agentProfileName];
        if (!jiraAgentProfile.isEmpty()) {
           system.debug('ADMIN');
            String jiraProfileAgentId = String.valueOf(jiraAgentProfile[0].id);
            allow = UserInfo.getProfileId() != jiraProfileAgentId;
        }
        return allow || Test.isRunningTest();
    }
 
} --------------------------- global class JIRAConnectorWebserviceCalloutCreate {
 
    @future (callout=true) 
    WebService static void createIssue(String baseUrl, String systemId, String objectType, String objectId, String projectKey, String issueType) {
        try {
            HttpRequest req = buildRequest(baseUrl, JIRA.username, JIRA.password, systemId, objectType, objectId, projectKey, issueType);
            JIRA.sendRequest(req);
        } catch(System.CalloutException e) {
            System.debug(e);
        }
    }
 
    // Constructs request needed to create a JIRA issue from provided parameters.
    @testVisible private static HttpRequest buildRequest(String baseUrl, String username, String password,
                                           String systemId, String objectType, String objectId,
                                           String projectKey, String issueType) {
        HttpRequest req = new HttpRequest();
        String basicAuthHeader = JIRA.authHeader(username, password);
        String endpoint = getEndpoint(baseUrl, systemId, objectType, objectId);
        req.setHeader('Authorization', basicAuthHeader);
        req.setHeader('Content-Type','application/json');
        req.setMethod('POST');
        req.setEndpoint(endpoint);
        req.setBody('{"project":"' + projectKey + '", "issueType":"' + issueType + '"}');
        return req;
    }
 
    // Creates the endpoint to create the issue from provided parameters.
    private static String getEndpoint(String baseUrl, String systemId, String objectType, String objectId) {
        return baseUrl + '/rest/customware/connector/1.0/' + systemId + '/' + objectType + '/' + objectId + '/issue/create.json';
     // return baseUrl + 'rest/' + systemId + '/' + objectType + '/' + objectId + '/issue/create.json';

    }
 
} --------------------------------------- TRIGGER :
trigger CreateIssue on Case (after insert) {
 
    // Check whether current user is not JIRA agent so that we don't create an infinite loop.
    if (JIRA.currentUserIsNotJiraAgent()) {
        for (Case c : Trigger.new) {
            system.debug('TRIGGER ' );
            // Define parameters to be used in calling Apex Class
            String objectType ='CASE';  // Please change this according to the object type
            String objectId = c.id;
            String projectKey = 'SAL'; //Please change this according to the JIRA project key
            String issueType = '2';     //Please change this according to the JIRA issue type ID
            // Calls the actual callout to create the JIRA issue.
            JIRAConnectorWebserviceCalloutCreate.createIssue(JIRA.baseUrl, JIRA.systemId, objectType, objectId, projectKey, issueType);
        }
    }
 
} ------------------------- Button : {!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} {!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")} sforce.apex.execute("JIRAConnectorWebserviceCalloutCreate","createIssue", {baseUrl:"https://222222.atlassian.net", systemId: "2", objectType: "Case", objectId:"{!Case.Id}", projectKey:"SAL", issueType:"2"}); window.alert("JIRA Issue should be created.");
JIRA END POINT:
Photo of rajib nandi

rajib nandi

  • 17 Posts
  • 0 Reply Likes

Posted 3 years ago

  • 1
Photo of Victor Law

Victor Law, Official Rep

  • 413 Posts
  • 22 Reply Likes
Hi Rajib,

It seems like you have entered the wrong credential for the Connector.

Can you please double confirm that you are using a valid credential in the Apex Class?

Besides that, can you please entered the JIRA username instead of the email for the following variable?

public static String username = 'xxxxxxxxxxx@gmail.com';  // JIRA username

For your information, JIRA Cloud allow user to authenticate using email address, but Remote API or Apex Class/Trigger only can authenticate the user account using the username.

In addition, can you please ensure that the JIRA user account has the permission as stated in this documentation?

Thank you.

Regards,
Victor Law
Photo of rajib nandi

rajib nandi

  • 17 Posts
  • 0 Reply Likes
Yes..My username is my email.That is correct.Also in JIRA how do I check for user name.
Photo of rajib nandi

rajib nandi

  • 17 Posts
  • 0 Reply Likes
I see the option "TROUBLESHOOTING AND SUPPORT" but its not allowing me to click on it.
Photo of rajib nandi

rajib nandi

  • 17 Posts
  • 0 Reply Likes
Can I plz share my screen if possible
Photo of rajib nandi

rajib nandi

  • 17 Posts
  • 0 Reply Likes
Hi,
I have dropped mail to " support@servicerocket.com" but no response.
Photo of Victor Law

Victor Law, Official Rep

  • 413 Posts
  • 22 Reply Likes
Hi Rajib,

We have responded to you via email, can you please provide us with the information as per requested in the email thread.

Thank you.

Regards,
Victor Law
Photo of rajib nandi

rajib nandi

  • 17 Posts
  • 0 Reply Likes
I have replied to the mail. Please do let me know if you need any other information.