Error 401 Unauthorized using Portal via Api

Hi,

i want to open portal via api using single sign on, but i always get the error "401 Unauthorized" when calling Portal.open() in javascript. The exact request that fails is:

POST https://<sitename>.chargebee.com/api/internal/ssp_users/activate_token

When doing this, the portal opens and a screen "Token has expired." shows up.

What am i doing wrong? Thanks.      

$(document).ready(function () {
    Chargebee.init({
        site: "<sitename>"
    });
});

function openChargebeePortal() { //called on click
    var chargebeeInstance = Chargebee.getInstance();
    chargebeeInstance.setPortalSession(function () {

        return new Promise(function (resolve, reject) {
            
            $.ajax({
                url: 'CreatePortalSession',
                method: 'POST',
                data: '{"customerId":"<costumerid>"}',
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (msg) {
                    resolve(msg);
                }
            });
        });
    });

    var cbPortal = chargebeeInstance.createChargebeePortal();

    cbPortal.open({
    });
}

    

//Server code    
public JsonResult CreatePortalSession(string customerId) {
      ApiConfig.Configure(<sitename>, <api-key>);
      EntityResult result = PortalSession.Create()
            .CustomerId(customerId)
            .Request();
      return Json(result.PortalSession);
}

  


UP ?
Same code, same problem

Hi there!

Could you try the below code for the client side and let me know if this works?

function openChargebeePortal() { //called on click
event.stopPropagation();
event.preventDefault();
    var chargebeeInstance = window.Chargebee.init({site: "<your-site>"});
    chargebeeInstance.setPortalSession(function () {
        return $.ajax({
                url: 'CreatePortalSession',
                method: 'POST',
                data: '{"customerId":"<custumerid>"}',
                dataType: "json",
                contentType: "application/json; charset=utf-8"
            });
    });
chargebeeInstance.createChargebeePortal().open({
});
}


For further reference  :  https://github.com/chargebee/chargebee-checkout-samples/blob/master/api/front-end/jquery/index.html

Hi, i found a working solution for this:  

var chargebeeInstance = window.Chargebee.init({ site: <sitename> });
chargebeeInstance.setPortalSession(function () {
     return new Promise(function (resolve, reject) {
         $.post({
             url: 'Home/CreatePortalSession',
             contentType: "application/json; charset=utf-8",
             success: function (response) {
                 resolve(response);
             }
         });
     });
 });

    var callbacks = {
        close: function () {
            chargebeeInstance.logout();
        }
    }

    chargebeeInstance.createChargebeePortal().open(callbacks);

   

//Server
public object CreateChargebeePortalSession() {
        ApiConfig.Configure(<Cb site>, <Api Key>);
        EntityResult result = PortalSession.Create()
              .CustomerId(<customerId>)
              .Request();
        return result.PortalSession.GetJToken();
      }
}

 You need to return JToken as object from server

If someone lands here like me, in NODE.JS, return the portal_session object from the anonymous object (unlike hosted_page which requires to return the anonymous object).

Login or Signup to post a comment
×