Created
July 13, 2019 03:09
-
-
Save nortonwong/84729f60ff42c1923f170d78ffd4235c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
await (async () => { | |
const formUrlEncode = params => Object.entries(params).map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`).join(`&`) | |
const auth = `Bearer ` + await fetch(`https://www.example.com/api/oauth2`, { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
}, | |
body: formUrlEncode({ | |
username: 'admin', | |
password: '1234', | |
}), | |
credentials: 'omit', | |
referrerPolicy: 'no-referrer', | |
}).then(r => r.json()).then(r => r.access_token); | |
const beans = await fetch(`https://www.example.com/api/cool-beans`, { | |
headers: { | |
Authorization: auth, | |
}, | |
credentials: 'omit', | |
referrerPolicy: 'no-referrer', | |
}).then(r => r.json()).then(r => r.beans); | |
return beans; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment