使用Node.JS上传到Flickr:无效的auth_token

所以我是OAuth和Node.JS的新手,但是我必须创build一个Flickr机器人来上传大学项目的照片。 从我的理解,节点没有本地支持multipart/formdata请求,所以我构造了POST请求手动。

我知道下面的代码不一定是干净的或者是最好的,但是它是一次性的攻击:服务器被一台计算机请求,只运行几个小时。

 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(); }); } } 

上述函数生成的POST请求与文档中build议的大致相同。 但是由于某种原因,请求回应说auth_token是无效的。

我正在设置以下两个function的身份validation:

 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'); }); } 

如果this.init获得Flickrauthentication页面, this.auth处理redirect的callback,然后调用this.postfauth是node-oauth的一个实例。 我是否错过了这个过程中的一个步骤,或者我在这个过程中混乱了吗? 谢谢。

使用OAuth时,您不再使用api_keyapi_sigauth_token参数,即使上传也是如此。 您应该使用oauth_consumer_keyoauth_signatureoauth_token来代替。

OAuth令牌不是有效的旧式auth_token,反之亦然。

查看关于Flickr和OAuth的博客系列,了解更多详情: http : //www.wackylabs.net/2011/12/oauth-and-flickr-part-1/

    Interesting Posts