Authorize API call using OAuth access token and AJAX

Virtamove IT November 16, 2020

How am I supposed to authorize an API call to create a new session with OAuth? I have gone through the steps to generate an access token, but I am always returned 401 ( "You are not authenticated. Authentication required to perform this operation." )


my call:

$.ajax ({    
url: "https://myjira.com/rest/auth/1/session",

type: 'GET',

beforeSend: function(xhr) {

xhr.setRequestHeader("Authorization", "Bearer <accesstoken>");
xhr.withCredentials = true;
},

success: function(response) {

alert("success");

},

error: function(error) {

alert("error");

}
});

 

Doing this same call using basic authentication works fine, such as:

var username = $("#username").val();
var password = $("#password").val();
$.ajax ({

url: "https://myjira.com/rest/auth/1/session",

type: 'GET',

beforeSend: function(xhr) {

var base64 = btoa(username + ':' + password);
xhr.setRequestHeader("Authorization", "Basic " + base64);
xhr.withCredentials = true;
},

success: function(response) {

alert("success");

},

error: function(error) {

alert("error");

}
});

 

How am I supposed to authorize this call for a new session using oauth? I've spent days now searching for a working example with no luck. Thanks!

0 answers

Suggest an answer

Log in or Sign up to answer