diff --git a/src/authenticators/client.js b/src/authenticators/client.js index 11d667c..570bfd3 100644 --- a/src/authenticators/client.js +++ b/src/authenticators/client.js @@ -52,17 +52,23 @@ async () => { if(!outstanding) { debug(`Getting token with client credentials`); - outstanding = client.grant({ grant_type: 'client_credentials' }) - + let grantWrapFunc = async () => { + let result = client.grant({ grant_type: 'client_credentials' }) + try { + return await result; + } catch(err) { + if (err && err.statusCode === 429) { + await _sleep(1000); + return await grantWrapFunc(); + } + throw err; + } + } + outstanding = grantWrapFunc(); try { token_set = await outstanding + } finally { outstanding = undefined - } catch(err) { - outstanding = undefined - if (err && err.statusCode === 429) { - await _sleep(1000); - await oauthtoken_set.refresh(); - } } debug(`Got fresh token: ${JSON.stringify(token_set)}`);