user logout issues when using REST AP

In my plugin, I'm using the SugarCRM function SUGAR.App.api.getOAuthToken()  for querying any data (get/post) on each ajax call of SugarCRM v10 REST API. Below is the piece of ajax request code

$.ajax({
beforeSend: function(request) {
request.setRequestHeader("OAuth-Token", SUGAR.App.api.getOAuthToken());
},
url: "rest/v10/Calls/",
data: {
"filter": [{
"id": callID
}]
},
type: "GET",
async: false,
success: function(data) {
contID = data.records[0].contact_id;
callStatus = data.records[0].status;
curCallID = data.records[0].id;
pType = data.records[0].parent_type;
pID = data.records[0].parent_id;
console.debug('[' + DateTime() + '] [DEBUG] sucessfull display phone numbers ...');

},
error: function(error) {
console.error(error);
var json = JSON.parse(error.responseText);
app1.alert.show('contact-ok', {
level: 'error',
messages: json.error_message,
autoClose: true
});
console.error('[' + DateTime() + '] [FATAL ERROR] display phone numbers ...');

}
});


We are facing one serious issue, some of the users automatically logged out from sugar while using the plugin. This is not happening with all plugin users. Don't know why this is happening. Is there the function SUGAR.App.api.getOAuthToken() is causing the problem.

  • That is correct; when you obtain a token you can only be logged in once per platform. You should add a custom platform into your instance of Sugar and then when you obtain the token use your custom platform.

    See the documentation at 

    support.sugarcrm.com/.../

  • This is expected behaviour. When you retrieve a new token for a user you will log them out if you are on the same platform. Where is this code running? Inside sugar? outside , in a different web app? 

  • If you are attempting to use the code inside of Sugar; we don't use $.ajax calls. Sugar has a very robust framework called Sidecar to work with all of the data in Sugar.

    You should read the Dashlet Developer's Guide

  • code is using in sugar only. We just hits the function SUGAR.App.api.getOAuthToken(). and whatever it returns the OAuth token, we use that token for the Rest API.

  • Hi Jeff,

    thanks for the reply.

    There are two ways to obtain the access token
    1- rest/v10/oauth2/token via rest API by using the API platform and its responses are


      {

    "access_token": "xxxxxx-yyyy-zzzz-b1ef-aaaaadb"
    "expires_in": 3600
    "token_type": "bearer"
    "scope": null
    "refresh_token": "nnnnn-yyyy-zzzz-b1ef-aaaaadb"
    "refresh_expires_in": 1209599
    "download_token": "mmmmm-yyyy-zzzz-b1ef-aaaaadb"

    }
     and from here we can use access_token for Sugar rest v10 API(Get/Post)

    2- by using directly SUGAR.App.api.getOAuthToken() in javascript file, this function also provides the access_token and we can use access_token for Sugar rest v10 API(Get/Post).
     And in our plugin, most of the code has been done js side.

    Can you please suggest which option will be better? 

    And strange only one sugar client reported this issue, other sugar clients using the plugin without any issue.

  • The question is where this is running. Inside sugar? And if yes why aren't you using the sidecar framework?

  • Ok thanks, I will try and let you know.