Last active
August 29, 2015 14:05
-
-
Save ryanvalentin/6fa54703a2cd64b0caae to your computer and use it in GitHub Desktop.
Disqus Azure Mobile Service callback function Pt 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
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); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment