Skip to content

Instantly share code, notes, and snippets.

@caub caub/zendesk-chat.js
Last active Aug 27, 2019

Embed
What would you like to do?
Zendesk Chat API issue
const fetch = require('node-fetch');
const cp = require('child_process');
const http = require('http');
// const ZENDESK_KEY = process.env.ZENDESK_KEY || '..........';
const ZENDESK_USER = process.env.ZENDESK_KEY || 'cyril.auburtin@ultimate.ai';
const ZENDESK_PASSWORD = process.env.ZENDESK_PASSWORD || '..........';
// const basicAuth = Buffer.from(`${ZENDESK_USER}/token:${ZENDESK_KEY}`).toString('base64');
const ZENDESK_SUBDOMAIN = process.env.ZENDESK_SUBDOMAIN || 'd3v-ultimateai';
const ZENDESK_OAUTH_CLIENT_ID = process.env.ZENDESK_OAUTH_CLIENT_ID || 'ultimate.ai_zendesk';
const ZENDESK_OAUTH_CLIENT_SECRET = process.env.ZENDESK_OAUTH_CLIENT_SECRET || '............................';
const ZENDESK_OAUTH_REDIRECT_URI = process.env.ZENDESK_OAUTH_REDIRECT_URI || 'http://localhost:3000';
// https://develop.zendesk.com/hc/en-us/community/posts/360001983148-InvalidEndpoint-while-trying-to-call-chat-api
// https://developer.zendesk.com/rest_api/docs/chat/auth
const getOauthData = () => new Promise((resolve, reject) => {
const oauthUrl = `https://${ZENDESK_SUBDOMAIN}.zendesk.com/oauth/authorizations/new?${new URLSearchParams({
response_type: 'code',
redirect_uri: ZENDESK_OAUTH_REDIRECT_URI,
client_id: ZENDESK_OAUTH_CLIENT_ID,
scopes: 'read write chat'
})}`;
try {
cp.execSync(`xdg-open '${oauthUrl}'`); // open a browser window to get an oauth2 access token
} catch{
console.log('open', oauthUrl); // open a browser window to get an oauth2 access token
}
const server = http.createServer(async (req, res) => {
res.end();
const params = new URLSearchParams(req.url.slice(1));
if (params.has('code')) {
server.close();
const data = await fetch(`https://${ZENDESK_SUBDOMAIN}.zendesk.com/oauth/tokens`, {
method: 'POST',
body: new URLSearchParams({
grant_type: 'authorization_code',
code: params.get('code'),
client_id: ZENDESK_OAUTH_CLIENT_ID,
client_secret: ZENDESK_OAUTH_CLIENT_SECRET,
redirect_uri: ZENDESK_OAUTH_REDIRECT_URI,
scope: 'read write chat'
}) + '',
headers: {
'Content-Type': 'application/x-www-form-urlencoded', // I also tried with application/json and JSON.stringify above
}
}).then(r => r.json());
resolve(data);
return;
}
if (params.has('error')) {
reject(new Error(params.get('error')));
server.close();
return;
}
});
server.listen(3000); // zendesk oauth callback is http://localhost:3000
});
(async () => {
let data;
// Get Oauth data from password
data = await fetch(`https://${ZENDESK_SUBDOMAIN}.zendesk.com/oauth/tokens`, {
method: 'POST',
body: new URLSearchParams({
grant_type: 'password', // I tried 'authorization_code' as well, like mentioned in the forum post
username: ZENDESK_USER,
password: ZENDESK_PASSWORD,
client_id: ZENDESK_OAUTH_CLIENT_ID,
client_secret: ZENDESK_OAUTH_CLIENT_SECRET,
redirect_uri: ZENDESK_OAUTH_REDIRECT_URI,
scope: 'read write chat'
}) + '',
headers: {
'Content-Type': 'application/x-www-form-urlencoded', // I also tried with application/json and JSON.stringify above
}
}).then(r => r.json());
// Get Oauth data from browser
// data = await getOauthData();
console.log(data);
console.log(await fetch('https://www.zopim.com/api/v2/chats', { headers: { Authorization: `Bearer ${data.access_token}` } }).then(r => r.json()).catch(e => e)) // NOT working in both cases: { description:'The server could not verify that you are authorized to access the requested resource', error: 'Unauthorized' }
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.