From bde6d2956b4d31b0cf4d785d2f8d6339db809cfe Mon Sep 17 00:00:00 2001 From: Meher Gara Date: Thu, 5 Oct 2017 12:31:27 -0400 Subject: [PATCH 1/2] Added a multipart method to send multipart form to facebook (required for image uploading) --- lib/graph.js | 71 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 64 insertions(+), 7 deletions(-) diff --git a/lib/graph.js b/lib/graph.js index 879e539..34f251d 100644 --- a/lib/graph.js +++ b/lib/graph.js @@ -32,7 +32,7 @@ function extend(target) { var accessToken = null , appSecret = null , graphUrl = 'https://graph.facebook.com' - , graphVersion = '2.9' // default to the oldest version + , graphVersion = '2.10' // default to the oldest version , oauthDialogUrl = "https://www.facebook.com/v2.0/dialog/oauth?" // oldest version for auth , oauthDialogUrlMobile = "https://m.facebook.com/v2.0/dialog/oauth?" // oldest version for auth , requestOptions = {}; @@ -60,6 +60,8 @@ function Graph(method, url, postData, callback) { postData = {}; } + let httpMethod = method; + url = this.prepareUrl(url); this.callback = callback || noop; this.postData = postData; @@ -67,8 +69,11 @@ function Graph(method, url, postData, callback) { this.options = extend({}, requestOptions); this.options.encoding = this.options.encoding || 'utf-8'; + if(httpMethod==="MULTIPART") + httpMethod = "POST"; + // these particular set of options should be immutable - this.options.method = method; + this.options.method = httpMethod; this.options.uri = url; this.options.followRedirect = false; @@ -196,11 +201,7 @@ Graph.prototype.get = function () { } self.end(body); - }).on('error', function(err) { - self.callback({ - message: 'Error processing https request' - , exception: err - }, null); + }); }; @@ -230,6 +231,62 @@ Graph.prototype.post = function() { }; + +/** + * https.multipart request wrapper + */ + +Graph.prototype.multipart = function() { + + var self = this + , postData = this.postData; + + this.options.formData = postData; + + return request(this.options, function (err, res, body) { + if (err) { + self.callback({ + message: 'Error processing https request' + , exception: err + }, null); + + return; + } + + self.end(body); + }); + +}; + + +/** + * Publish to the facebook graph + * access token will be needed for posts + * Ex: + * + * var wallPost = { message: "heyooo budday" }; + * graph.multipart(friendID + "/feed", wallPost, callback); + * + * @param {string} url + * @param {object} postData + * @param {function} callback + */ + +exports.multipart = function (url, postData, callback) { + console.log("multipart first call") + if (typeof url !== 'string') { + return callback({ message: 'Graph api url must be a string' }, null); + } + + if (typeof postData === 'function') { + callback = postData; + postData = url.indexOf('access_token') !== -1 ? {} : {access_token: accessToken}; + } + + + return new Graph('MULTIPART', url, postData, callback); +}; + /** * Accepts an url an returns facebook * json data to the callback provided From e16f9a35bbb335dd64f65b14542248189dede86a Mon Sep 17 00:00:00 2001 From: Meher Gara Date: Thu, 5 Oct 2017 14:42:56 -0400 Subject: [PATCH 2/2] Removed trailing console.log --- lib/graph.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/graph.js b/lib/graph.js index 34f251d..9571dcf 100644 --- a/lib/graph.js +++ b/lib/graph.js @@ -273,7 +273,7 @@ Graph.prototype.multipart = function() { */ exports.multipart = function (url, postData, callback) { - console.log("multipart first call") + if (typeof url !== 'string') { return callback({ message: 'Graph api url must be a string' }, null); }