Last active
April 25, 2017 17:18
-
-
Save sunnygleason/f41e5e98a771b7509730d39e20bda4b6 to your computer and use it in GitHub Desktop.
PubNub Twitter Publisher BLOCK JavaScript
This file contains 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
const xhr = require('xhr'); | |
const base64Codec = require('codec/base64'); | |
const crypto = require('crypto'); | |
export default (request) => { | |
// see https://apps.twitter.com | |
const consumerKey = "YOUR_CONSUMER_KEY"; | |
const consumerSecret = "YOUR_CONSUMER_SECRET"; | |
const accessToken = "YOUR_ACCESS_TOKEN"; | |
const oauthTokenSecret = "YOUR_OAUTH_TOKEN_SECRET"; | |
const httpReqType = "POST"; | |
const contentType = "application/x-www-form-urlencoded"; | |
let url = "https://api.twitter.com/1.1/statuses/update.json"; | |
if (!request.message.tweet) { | |
return request.abort({ "400": "400: Bad Request" }); | |
} | |
let tweetContent = (request.message.tweet || ""); | |
let content = (tweetContent ? "status=" + encodeURIComponent(tweetContent).replace(/[!'()*]/g, escape) : ""); | |
let oauth = { | |
"oauth_consumer_key": consumerKey, | |
"oauth_nonce": base64Codec.btoa(Math.random().toString(36)).replace(/(?!\w)[\x00-\xC0]/g, ""), | |
"oauth_signature_method": "HMAC-SHA1", | |
"oauth_timestamp": Math.floor(new Date().getTime() / 1000), | |
"oauth_token": accessToken, | |
"status": tweetContent | |
}; | |
return getOAuthSignature(oauth, httpReqType, url, consumerSecret, oauthTokenSecret).then((result) => { | |
oauth.oauth_signature = result; | |
delete oauth.status; | |
let authHeaderString = objectToRequestString(oauth, 'OAuth ', '="', '"', ', '); | |
let http_options = { | |
"method": httpReqType, | |
"headers": { | |
"Content-Type": contentType, | |
"Authorization": authHeaderString | |
}, | |
"body": content | |
}; | |
return xhr.fetch(url, http_options).then((response) => response.json()).then((response) => { | |
request.message.twtr_response = response; | |
return request.ok(); | |
}); | |
}) | |
.catch((error) => { | |
return request.abort({ "500": "500: Internal server error" }); | |
}); | |
}; | |
function getOAuthSignature(oauth, httpReqType, url, consumerSecret, oauthTokenSecret) { | |
let parameterString = objectToRequestString(oauth, '', '=', '', '&'); | |
let signatureBaseString = httpReqType + "&" + encodeURIComponent(url) + "&" + encodeURIComponent(parameterString); | |
let signingKey = base64Codec.btoa(encodeURIComponent(consumerSecret) + "&" + encodeURIComponent(oauthTokenSecret)); | |
return crypto.hmac(signingKey, signatureBaseString, crypto.ALGORITHM.HMAC_SHA1).then((result) => { | |
return result; | |
}); | |
} | |
function objectToRequestString(obj, prepend, head, tail, append) { | |
let requestString = prepend || ""; | |
Object.keys(obj).forEach((key, i) => { | |
requestString += key + head + encodeURIComponent(obj[key]).replace(/[!'()*]/g, escape) + tail; | |
i < Object.keys(obj).length-1 ? requestString += append : null; | |
}); | |
return requestString; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment