Skip to content

Instantly share code, notes, and snippets.

@gr2m
Created August 27, 2019 05:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gr2m/53c346f7e9c2ee9ab9565e96a816ad44 to your computer and use it in GitHub Desktop.
Save gr2m/53c346f7e9c2ee9ab9565e96a816ad44 to your computer and use it in GitHub Desktop.
A Cloudflare worker to exchange an OAuth Web Flow code for an access token. Configure your CLIENT_ID and CLIENT_SECRET and you are all set.
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
const CLIENT_ID = '<your client ID>'
const CLIENT_SECRET = '<your client Secret>'
async function handleRequest(request) {
if (request.method === 'GET') {
return new Response(`$ curl -XPOST -H'Content-Type: application/json' -d'{"code": "<your oauth code>"}' ${request.url}`)
}
const { code } = await request.json()
return await fetch('https://github.com/login/oauth/access_token', {
method: "POST",
headers: {
'content-type': "application/json",
accept: "application/json"
},
body: JSON.stringify({
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
code
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment