public
Last active

share picture with twitter and flickr

  • Download Gist
package.json
JSON
1 2 3 4 5 6 7 8 9 10
{
"name": "oauth-shares",
"description": "oauth twitter and flickr shares integration",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "3.x",
"oauth": "0.9.x"
}
}
picture_share.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
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");
upload.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
var sys = require("sys");
var express = require("express");
 
var app = express();
 
app.use(express.bodyParser({
uploadDir: "/tmp",
keepExtensions: true
}));
app.use(express.limit("5mb"));
 
app.post("/upload", function (req, res) {
sys.puts(sys.inspect(req.files));
sys.puts(sys.inspect(req.body));
res.send("");
});
 
app.listen(8183);
console.log("Listening on port 8183");

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.