I have created my own sdk by using the Appylar API. The sdk is based on the Appylar REST sample app. The first thing I do is to initialise the sdk, establish a session and store it in a session token:
async init(appKey: string, adTypes: AdType[], orientations: Orientation[]) {
this.appKey = appKey
this.sessionToken = null
this.buffer = new AdBuffer()
this.adTypes = adTypes
this.orientations = orientations
const size = this.renderer.getScreenSize()
try {
const data = await this.request<CreateSessionResponse>(
API_URL_CREATE_SESSION,
{
app_key: appKey,
orientations,
width: size.width,
height: size.height,
density: parseFloat($('#id_density').val() ?? '1'),
app_id: $('#id_app_id').val(),
language: $('#id_language').val(),
country: $('#id_country').val(),
test_mode: $('#id_test_mode').val() === '1',
} as CreateSessionRequestData,
)
this.sessionToken = data.session_token
this.bufferMinLimit = data.buffer_limits.min
this.rotationInterval = data.rotation_interval
this.buffer = new AdBuffer()
this.triggerEvent({
name: 'initialized',
})
const combinations: Record<string, AdType[]> = orientations.reduce(
(total, item) => ({ ...total, [item]: adTypes }),
{},
)
this.requestAds(combinations)
} catch (error) {
console.error('Error when initializing', { error })
this.triggerEvent({
name: 'error',
error,
})
}
}
It works as it should, but after a while, I just get 401 responses when I try to fetch new banners and interstitials. Why am I suddenly unauthorised? I use the same session token as for all previous calls and I can see that it remains unchanged.
Any help on this matter would be very much appreciated.
The reason that you get the 401 error is most probably because your session has timed out. As with all API:s, the session token that you get is only valid for a certain period of time.
In the Appylar REST documentation overview under section 5, it says:
The session token that was created and returned calling the POST /api/v1/session/ endpoint, will expire after a while. From there on, the API will respond with an HTTP 401 for all requests. When that happens, the SDK must make sure to automatically renew the session by calling the POST /api/v1/session/ endpoint again.
In other words, you have to refresh the session in your sdk when you get the 401 error.