【问题标题】:Uploading to Flickr with Node.JS: Invalid auth_token使用 Node.JS 上传到 Flickr:无效的 auth_token
【发布时间】:2012-03-28 17:21:38
【问题描述】:

所以我是 OAuth 和 Node.JS 的新手,但我必须创建一个 Flickr 机器人来为大学项目上传照片。据我了解,Node 不支持 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 请求与documentation 中的建议大致相同。但是由于某种原因,请求响应说auth_token 无效。

我正在使用以下两个功能设置身份验证:

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 获得 Flickr 身份验证页面,this.auth 处理重定向的回调,然后调用 this.postfauthnode-oauth 的一个实例。我是否错过了流程中的一个步骤,或者我在此过程中搞砸了?谢谢。

【问题讨论】:

    标签: node.js oauth flickr


    【解决方案1】:

    使用 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/

    【讨论】:

    • 谢谢!为什么没有按照应有的方式记录下来?
    猜你喜欢
    • 2011-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多