Skip to content

Instantly share code, notes, and snippets.

@ryanvalentin
Created August 22, 2014 05:58
Show Gist options
  • Save ryanvalentin/53d440e7e844d8f11c4b to your computer and use it in GitHub Desktop.
Save ryanvalentin/53d440e7e844d8f11c4b to your computer and use it in GitHub Desktop.
// Disqus API public key
var apiPublic = 'YOUR_PUBLIC_KEY';
// Disqus API secret key
var apiSecret = 'YOUR_SECRET_KEY';
// Should match exactly what you've entered in your Disqus API application
var oAuthRedirectUri = 'https://MOBILE_SERVICE_NAME.azure-mobile.net/api/disqus_callback/';
exports.get = function(request, response) {
// Retrieve the code from the query
var code = request.query.code;
if (code !== undefined) {
// Code is set, so we know we're handling an OAuth callback request
// Make POST request for the temporary access token
var httpRequest = require('request');
httpRequest({
uri: 'https://disqus.com/api/oauth/2.0/access_token/',
method: 'POST',
form: {
grant_type: 'authorization_code',
client_id: apiPublic,
client_secret: apiSecret,
code: code,
redirect_uri: oAuthRedirectUri
}
}, function(httpError, httpResponse, httpBody) {
// Handle response from Disqus here
if (httpError || httpResponse.statusCode !== 200) {
// Error
console.error('Error retrieving access token: ' + httpBody);
response.send(
httpResponse.statusCode,
'Unable to connect to Disqus.'
);
}
else {
// Everything is good, respond with the access token
/* Body of request looks like this:
{
"access_token": "c2d06abacfbb40179e47f62f06546ea9",
"refresh_token": "9182211bf2f746a4b5c5b1e3766443d6",
"expires_in": 2592000,
"username": "batman"
"user_id": "947103743"
}
*/
var authorization = JSON.parse(httpBody);
// Get a reference to our user table
// TODO replace 'YOUR_TABLE_NAME' with the table name you created
// for your Disqus users
var userTable = request.service.tables.getTable("YOUR_TABLE_NAME");
userTable.where({ userId: authorization.user_id.toString() }).read({
success: function (results) {
if (results.length === 0) {
// This user hasn't authenticated yet, so insert a new entry
userTable.insert(getUserEntry(authorization));
}
else {
// User exists, so update the entry
var entry = getUserEntry(authorization);
entry.id = results[0].id;
userTable.update(entry);
}
},
error: function (error) {
console.error('Error reading database', error);
response.send(500, error);
}
});
response.send(
statusCodes.OK,
'Success! This is your access token: ' +
authorization.access_token
);
}
});
}
else {
// No code in query, so send the user to the authorize URL
var redirectUri = 'https://disqus.com/api/oauth/2.0/authorize/' +
'?client_id=' + apiPublic +
'&scope=read,write' +
'&response_type=code';
response.redirect(redirectUri);
}
};
function getUserEntry(authorization) {
return {
accessToken: authorization.access_token,
refreshToken: authorization.refresh_token,
userId: authorization.user_id,
username: authorization.username,
expires: Math.round((new Date().getTime() / 1000) + authorization.expires_in)
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment