|
var sys = require("sys"); |
|
var fs = require("fs"); |
|
var https = require("https"); |
|
var http = require("http"); |
|
var url = require("url"); |
|
var OAuth = require("oauth").OAuth; |
|
var express = require("express"); |
|
|
|
var app = express(); |
|
var picture = fs.readFileSync("../temp/pic.jpg"); |
|
|
|
OAuth.prototype.post_image = function(link, content, error, response) { |
|
var auth = this.authHeader(link.href, this.oauth_access_token, this.oauth_access_token_secret, "POST"); |
|
var protocol = null; |
|
var port = null; |
|
if(link.protocol === "https:") { |
|
protocol = https; |
|
port = link.port || 443; |
|
} else { |
|
protocol = http; |
|
port = link.port || 80; |
|
} |
|
var request = protocol.request({ |
|
host: link.hostname, |
|
path: link.pathname, |
|
port: port, |
|
method: "POST", |
|
headers: { |
|
"Authorization": auth, |
|
"Content-Type": "multipart/form-data; boundary=" + boundary, |
|
"Content-Length": content.length |
|
} |
|
}); |
|
request.write(content); |
|
request.end(); |
|
|
|
request.on("error", error); |
|
request.on("response", response); |
|
} |
|
|
|
var twitter_oauth = new OAuth( |
|
"https://twitter.com/oauth/request_token", |
|
"https://twitter.com/oauth/access_token", |
|
"${API KEY}", |
|
"${SECRET KEY}", |
|
"1.0A", |
|
"http://localhost:8182/twitter/callback", |
|
"HMAC-SHA1" |
|
); |
|
var twitter_post_link = url.parse("https://upload.twitter.com/1/statuses/update_with_media.json"); |
|
// var twitter_post_link = url.parse("http://localhost:8183/upload/"); |
|
|
|
var flickr_oauth = new OAuth( |
|
"http://www.flickr.com/services/oauth/request_token", |
|
"http://www.flickr.com/services/oauth/access_token", |
|
"${API KEY}", |
|
"${SECRET KEY}", |
|
"1.0A", |
|
"http://localhost:8182/flickr/callback", |
|
"HMAC-SHA1" |
|
); |
|
var flickr_post_link = url.parse("http://api.flickr.com/services/upload/"); |
|
// var flickr_post_link = url.parse("http://localhost:8183/upload/"); |
|
|
|
var boundary = "share" + new Date().valueOf(); |
|
var twitter_content = Buffer.concat([ |
|
new Buffer( |
|
"--" + boundary + "\r\n" + |
|
"Content-Disposition: form-data; name=\"status\"\r\n" + |
|
"\r\n" + |
|
"test tweet with picture\r\n" + |
|
|
|
"--" + boundary + "\r\n" + |
|
"Content-Disposition: form-data; name=\"media[]\"; filename=\"picture.jpg\"\r\n" + |
|
"Content-Type: image/jpeg\r\n" + |
|
"\r\n" |
|
), |
|
picture, |
|
new Buffer( |
|
"\r\n" + |
|
"--" + boundary + "--\r\n" |
|
) |
|
]); |
|
var flickr_content = Buffer.concat([ |
|
new Buffer( |
|
"--" + boundary + "\r\n" + |
|
"Content-Disposition: form-data; name=\"title\"\r\n" + |
|
"\r\n" + |
|
"test photo\r\n" + |
|
|
|
"--" + boundary + "\r\n" + |
|
"Content-Disposition: form-data; name=\"photo\"; filename=\"picture.jpg\"\r\n" + |
|
"Content-Type: image/jpeg\r\n" + |
|
"\r\n" |
|
), |
|
picture, |
|
new Buffer( |
|
"\r\n" + |
|
"--" + boundary + "--\r\n" |
|
) |
|
]); |
|
|
|
app.get("/twitter/connect", function(req, res) { |
|
twitter_oauth.getOAuthRequestToken(function(error, oauth_request_token, oauth_request_token_secret, results) { |
|
if (error) { |
|
res.send("error getting oauth request token : " + sys.inspect(error), 500); |
|
} else { |
|
twitter_oauth.oauth_request_token = oauth_request_token; |
|
twitter_oauth.oauth_request_token_secret = oauth_request_token_secret; |
|
res.redirect("https://twitter.com/oauth/authorize?oauth_token=" + oauth_request_token); |
|
} |
|
}); |
|
}); |
|
app.get("/twitter/callback", function(req, res) { |
|
twitter_oauth.getOAuthAccessToken( |
|
twitter_oauth.oauth_request_token, |
|
twitter_oauth.oauth_request_token_secret, |
|
req.query.oauth_verifier, |
|
function(error, oauth_access_token, oauth_access_token_secret, results) { |
|
if (error) { |
|
res.send("error getting oauth access token : " + sys.inspect(error), 500); |
|
} else { |
|
twitter_oauth.post_image( |
|
twitter_post_link, |
|
twitter_content, |
|
function(error) { |
|
res.send(sys.inspect(error), 500); |
|
}, |
|
function(response) { |
|
res.send(sys.inspect(response)); |
|
} |
|
); |
|
} |
|
}); |
|
}); |
|
|
|
app.get("/flickr/connect", function(req, res) { |
|
flickr_oauth.getOAuthRequestToken(function(error, oauth_request_token, oauth_request_token_secret, results) { |
|
if (error) { |
|
res.send("error getting oauth request token : " + sys.inspect(error), 500); |
|
} else { |
|
flickr_oauth.oauth_request_token = oauth_request_token; |
|
flickr_oauth.oauth_request_token_secret = oauth_request_token_secret; |
|
res.redirect("http://www.flickr.com/services/oauth/authorize?oauth_token=" + oauth_request_token); |
|
} |
|
}); |
|
}); |
|
app.get("/flickr/callback", function(req, res) { |
|
flickr_oauth.getOAuthAccessToken( |
|
flickr_oauth.oauth_request_token, |
|
flickr_oauth.oauth_request_token_secret, |
|
req.query.oauth_verifier, |
|
function(error, oauth_access_token, oauth_access_token_secret, results) { |
|
if (error) { |
|
res.send("error getting oauth access token : " + sys.inspect(error), 500); |
|
} else { |
|
flickr_oauth.post_image( |
|
flickr_post_link, |
|
flickr_content, |
|
function(error) { |
|
res.send(sys.inspect(error), 500); |
|
}, |
|
function(response) { |
|
res.send(sys.inspect(response)); |
|
} |
|
); |
|
} |
|
}); |
|
}); |
|
|
|
app.listen(8182); |
|
console.log("Listening on port 8182"); |