Get access token using Javascript

I have a Spring Boot app that’s going to use Spring Security and Okta to lock down the API endpoints.

The setup there is fairly straight forward. I also need to be able to use javascript to post to an endpoint and I believe I need to set the Bearer token in the Authorization header.

By the time the user is on the page with the javascript post, they will already be signed in to Okta.

I’ve tried to use the javascript examples as shown here

but I’m not able to get to the token. The user has a session so the console will show “Welcome back, (user info here)”

I’m basically trying to do what callMessagesApi() is showing but the access token is not there since the user has a session and the following javascript did not run:

// Save the tokens for later use, e.g. if the page gets refreshed:
        oktaSignIn.tokenManager.add('accessToken', accessToken);

What’s the correct way to get access to the token so that I can send it back to the server?

Hi @Vladimir

The access token should be present in the res retrieved from the success method, as shown in the following example:

<!DOCTYPE html>
<!-- Latest CDN production Javascript and CSS: 2.16.0 -->

<!-- Theme file: Customize or replace this file if you want to override our default styles -->
<div id="widget-container"></div>

var oktaSignIn = new OktaSignIn({
    baseUrl: "",
    clientId: "0oamd11xmu7WZUbn4356",
    authParams: {
        issuer: "",
        responseType: ['token', 'id_token']

    el: '#widget-container'
}, function success(res) {
    if (res.status === 'SUCCESS') {
        var accessToken = res[0];
        var idToken = res[1];
        oktaSignIn.tokenManager.add('accessToken', accessToken);
        oktaSignIn.tokenManager.add('idToken', idToken);
        console.log("Access token: " + oktaSignIn.tokenManager.get('accessToken')['accessToken'])
		console.log("ID token: " + oktaSignIn.tokenManager.get('idToken')['idToken'])
    } else {

Thanks but this didn’t work for me. I changed the baseUrl, clientId, redirectUri and Issuer.

btw, what is ausmr67xnx0PrAbI7356 from your example?

It should be something like this right?

issuer: "https://my dev

Once I change the values and run I get the Okta screen, user logs in, the console log never happens, I added javascript alert messages, those never pop up.

The page redirects to another login that is not Okta branded.

I have a page that i can navigate to and check the user information. It’s all there so I know the user is logged in ok.

Also, when I do this I’m always using incognito mode.

I figured out a way to solve my issue by having the javascript do a form post so that the browser sends the cookies / headers to the backend.

It still would be nice to have a working example on how to post with javascript and be authenticated.

I have absolutely the same issue now. Did someone found any solving method? :slight_smile:

1 Like

Hi @rares

Can you please open a support case with us through an email to in order to further review the issue?