Reputation: 3997
So I'm new to OAuth and Node.JS, but I have to create a Flickr bot to upload photos for a university project. From my understanding Node doesn't have native support of multipart/formdata
requests so I constructed the POST request manually.
I know the following code isn't necessarily clean or best-practice, but it's meant to be a one-off hack: the server is being requested by a single computer and only running for a few hours.
this.post = function(text, image) {
var me = this;
if (me.authenticated) {
fs.readFile(image, 'utf8', function(err,data) {
if (err) throw new Error(err);
var bound = (crypto.createHash('md5').update((Math.random()*9999999).toString()).digest('hex'));
var req = http.request({host:'api.flickr.com',port:80,path:'/services/upload/',method:'POST',
headers:{
'Content-Type':'multipart/form-data; boundary='+bound,
'Content-Length':data.length,
'Connection':'keep-alive'
}
}, function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
console.log(chunk);
});
console.log(me);
});
bound = "--"+bound;
req.on('error', function(msg) {console.log('FLICKR UPLOAD ERROR: '+msg.message);});
req.write(bound+"\r\n");
req.write('Content-Disposition: form-data; name="api_key"\r\n\r\n'+flickroptions.key);
req.write("\r\n"+bound+"\r\n");
console.log("sending token "+me.token);
req.write('Content-Disposition: form-data; name="auth_token"\r\n\r\n'+me.token);
req.write("\r\n"+bound+"\r\n");
req.write('Content-Disposition: form-data; name="api_sig"\r\n\r\n'+function() {
var str = 'api_key'+flickroptions.key;
str += 'api_token'+me.token;
return crypto.createHash('md5').update(str).digest('hex');
}());
req.write("\r\n"+bound+"\r\n");
req.write('Content-Disposition: form-data; name="photo"; filename="'+image+'"\r\n');
req.write('Content-Type: image/jpeg\r\n\r\n');
req.write(data);
req.write('\r\n'+bound+"--"); req.end();
});
}
}
The above function generates a POST request more or less the same as recommended in the documentation. But for some reason, the request responds saying that auth_token
is invalid.
I'm setting up authentication with the following two functions:
this.init = function(text,image) {
var me = this;
fauth.getOAuthRequestToken({'oauth_callback':'http://192.168.0.19:7001/flickr'}, function(err, token, secret, results) {
if (err) console.log(err);
if (token && secret) {
me.secret = secret; me.token = token;
me.location = 'http://flicker.com/services/oauth/authorize?perms=write&oauth_token='+token;
}
});
}
//?oauth_token&oauth_verifier
this.auth = function(token, verifier) {
var me = this;
this.token = token; this.verifier = verifier;
fauth.getOAuthAccessToken(this.token, this.secret, this.verifier, function(err,token,secret,results) {
if (err) {console.log(err); return;}
me.results = results; me.authenticated = true;
me.token = token;
me.secret = secret;
me.post('hello world', '../eyes/memory/eyes000001.jpg');
});
}
Where this.init
gets a Flickr authentication page and this.auth
processes the redirected callback and then calls this.post
. fauth
is an instance of node-oauth. Am I missing a step in the process or have I messed up along the way? Thanks.
Upvotes: 2
Views: 1767
Reputation: 56853
When using OAuth you no longer us the api_key
, api_sig
and auth_token
parameters anymore, even for uploading. You should be using oauth_consumer_key
, oauth_signature
and oauth_token
instead.
An OAuth Token is not a valid old style auth_token, and vice versa.
Check out my blog series on Flickr and OAuth for more details: http://www.wackylabs.net/2011/12/oauth-and-flickr-part-1/
Upvotes: 3