Skip to content

Instantly share code, notes, and snippets.

@nortonwong
Created July 13, 2019 03:09
Show Gist options
  • Save nortonwong/84729f60ff42c1923f170d78ffd4235c to your computer and use it in GitHub Desktop.
Save nortonwong/84729f60ff42c1923f170d78ffd4235c to your computer and use it in GitHub Desktop.
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