Skip to content

Instantly share code, notes, and snippets.

@caub
Last active November 15, 2021 17:23
Show Gist options
  • Save caub/1da87be7897c531478a7887f46dc44da to your computer and use it in GitHub Desktop.
Save caub/1da87be7897c531478a7887f46dc44da to your computer and use it in GitHub Desktop.
Zendesk Chat API
const http = require('http');
const fetch = require('node-fetch');
const getToken = () => new Promise((resolve, reject) => {
const server = http.createServer(async (req, res) => {
res.end();
const params = new URLSearchParams(req.url.slice(1));
console.log('rec', req.url, params);
if (params.has('code')) {
resolve(params.get('code'));
server.close();
return;
}
if (params.has('error')) {
reject(params.get('error'));
server.close();
return;
}
});
server.listen(3000); // zendesk oauth callback is http://localhost:3000
});
getToken().then(async code => {
try {
const data = await fetch('https://foobar.zendesk.com/oauth/tokens', {
method: 'POST',
body: new URLSearchParams({
grant_type: 'authorization_code',
code,
client_id: 'SOME_CLIENT_ID',
client_secret: 'SOME_CLIENT_SECRET',
redirect_uri: 'http://localhost:3000',
scope: 'read write chat'
})+'',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(r => r.json());
console.log('data', data);
} catch (e) {
console.error(e);
}
})
const oauthUrl = `https://foobar.zendesk.com/oauth/authorizations/new?${new URLSearchParams({
response_type: 'code',
redirect_uri: 'http://localhost:3000',
client_id: 'SOME_CLIENT_ID',
scopes: 'read write chat'
})}`;
console.log('open', oauthUrl); // open the url in your browser, then you should obtain a ZENDESK_KEY
/*
// that can later be used:
await fetch('https://www.zopim.com/api/v2/chats', {
headers: {
'Authorization': `Basic ${Buffer.from(`dude@company.com/token:${ZENDESK_KEY}`).toString('base64')}`,
}
});
*/
@FarahNoon
Copy link

how did you fix it?

@caub
Copy link
Author

caub commented Nov 15, 2021

updated gist with what I found, but please this is old don't ask me more, no longer working for this job

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment