If an application token is expired or invalid the API will return the HTTP status code 401. That signals the application to initiate the OAuth flow to get a new access token.
I’m attempting to handle this case using the PureCloud JavaScript SDK. I noticed that PureCloudSession.setUnauthenticatedErrorHandler() seems like the perfect fit for this job; however, in testing this handler is never called when the access token expires.
It appears that when the access token expires, any calls to the PureCloud API do not return an Access-Control-Allow-Origin header, so PureCloudSession.setUnauthenticatedErrorHandler() is not called and the following error is traced out instead:
XMLHttpRequest cannot load https://api.inindca.com/api/v2/users/me. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://apps.inindca.com' is therefore not allowed access. The response had HTTP status code 401.
It seems like PureCloudSession.setUnauthenticatedErrorHandler() was added just for this purpose, but it doesn’t seem to be working as expected. Any chance that this is supposed to work and I’m just doing something wrong?
I tested the error handler with the code below and it was working for me. Can you provide more information about your environment? (web page/node, version, etc.)
edit: I tried it by specifying an auth token directly (shown below), and it still works. My responses are coming back with the access-control-allow-origin header set correctly. In addition to info about your environment, can you provide a context ID for one of the requests that doesn't have the CORS headers in the response?
var pureCloudSession = purecloud.platform.PureCloudSession({
strategy: 'token',
token:'DfJ3Bwiv_NGVlvGG_72CmdCLOSThXBu0rsjFkNBRjodqO-AaosLg3QBzAYGNHImvX2p8gLs8ym15ISgoPVaK-w'
});
Noob question, inside the setUnauthenticatedErrorHandler, what is the right way to refresh the token? Do you just call re-establish the session again with