Oauth

2012-10-16 19 views
7

का उपयोग करते हुए नोड.जेएस में ट्विटर पर छवियों को पोस्ट करना मैं ओथ मॉड्यूल का उपयोग करके ट्विटर पर छवियों को पोस्ट करने का प्रयास कर रहा हूं। यहां मेरे पास है:Oauth

यह 403 त्रुटि फेंकता है, मुझे पता है कि मैं इस पोस्ट में मीडिया कैसे जोड़ता हूं, लेकिन मुझे यकीन नहीं है कि यहां से कहां जाना है।

var https = require('https'); 
var OAuth= require('oauth').OAuth; 
var keys = require('./twitterkeys'); 
var twitterer = new OAuth(
      "https://api.twitter.com/oauth/request_token", 
      "https://api.twitter.com/oauth/access_token", 
      keys.consumerKey, 
      keys.consumerSecret, 
      "1.0", 
      null, 
      "HMAC-SHA1" 
     ); 


var params = { 
    status : "Tiger!", 
    media : [("data:" + mimeType + ";base64,") + fs.readFileSync(path,'base64')] 
}; 

//function(url, oauth_token, oauth_token_secret, post_body, post_content_type, callback) 
twitterer.post("https://upload.twitter.com/1/statuses/update_with_media.json", 
      keys.token, keys.secret, params, "multipart/form-data", 
      function (error, data, response2) { 
      if(error){ 
       console.log('Error: Something is wrong.\n'+JSON.stringify(error)+'\n'); 

      }else{ 
       console.log('Twitter status updated.\n'); 
       console.log(response2+'\n'); 
      } 
      }); 

यहाँ मैं im supose कर रहा है कि क्या belive लेकिन मैं ऐसा करने के लिए कैसे Node.js Oauth मॉड्यूल में पता नहीं है है। Posting image to twitter using Twitter+OAuth

+0

पहली चीजें पहले, क्या आपके पास nodejs इंस्टॉल है? – Chamilyan

+0

हाँ और मैं सामान्य स्थिति/अद्यतन का उपयोग कर अपने ट्विटर स्थिति को अपडेट करने में सक्षम हूं api.twiiter.com के माध्यम से सिर्फ मीडिया के साथ नहीं। –

+0

मेरा मानना ​​है कि पोस्ट में फॉर्म डेटा संलग्न करने में मेरी समस्या है। –

उत्तर

13

कोड की समीक्षा, ऐसा लगता है वहाँ की तरह कोई multipart/form-datanode-oauth पैकेज में सभी पर निपटने अभी। आप प्राधिकरण शीर्षलेख बनाने के लिए अभी भी node-oauth फ़ंक्शन का उपयोग कर सकते हैं, लेकिन आपको अपने आप में मल्टीपार्ट सामान करना होगा।

शायद तीसरे पक्ष के पुस्तकालय हैं जो इससे मदद कर सकते हैं, लेकिन यहां मैं इसे हाथ से निर्मित काम करने के लिए कैसे मिला।

var data = fs.readFileSync(fileName); 
var oauth = new OAuth(
    'https://api.twitter.com/oauth/request_token', 
    'https://api.twitter.com/oauth/access_token', 
    twitterKey, twitterSecret, 
    '1.0', null, 'HMAC-SHA1'); 

var crlf = "\r\n"; 
var boundary = '---------------------------10102754414578508781458777923'; 

var separator = '--' + boundary; 
var footer = crlf + separator + '--' + crlf; 
var fileHeader = 'Content-Disposition: file; name="media"; filename="' + photoName + '"'; 

var contents = separator + crlf 
    + 'Content-Disposition: form-data; name="status"' + crlf 
    + crlf 
    + tweet + crlf 
    + separator + crlf 
    + fileHeader + crlf 
    + 'Content-Type: image/jpeg' + crlf 
    + crlf; 

var multipartBody = Buffer.concat([ 
    new Buffer(contents), 
    data, 
    new Buffer(footer)]); 

var hostname = 'upload.twitter.com'; 
var authorization = oauth.authHeader(
    'https://upload.twitter.com/1/statuses/update_with_media.json', 
    accessToken, tokenSecret, 'POST'); 

var headers = { 
    'Authorization': authorization, 
    'Content-Type': 'multipart/form-data; boundary=' + boundary, 
    'Host': hostname, 
    'Content-Length': multipartBody.length, 
    'Connection': 'Keep-Alive' 
}; 

var options = { 
    host: hostname, 
    port: 443, 
    path: '/1/statuses/update_with_media.json', 
    method: 'POST', 
    headers: headers 
}; 

var request = https.request(options);  
request.write(multipartBody); 
request.end(); 

request.on('error', function (err) { 
    console.log('Error: Something is wrong.\n'+JSON.stringify(err)+'\n'); 
}); 

request.on('response', function (response) {    
    response.setEncoding('utf8');    
    response.on('data', function (chunk) { 
     console.log(chunk.toString()); 
    }); 
    response.on('end', function() { 
     console.log(response.statusCode +'\n'); 
    }); 
});  
+0

कोड को समय देने के लिए धन्यवाद! – Luc

+0

जाहिर है कि कुछ सुधार किए जा सकते हैं - विशेष रूप से 'सीमा' में एक यादृच्छिक तत्व होना चाहिए, लेकिन यह पहली चीज थी जिसे मैंने जेएस को पूरी तरह से नया होने के कारण अच्छी तरह से काम करने के लिए एकत्र किया था, और तब से हमारी परियोजना तब तक चली गई है मुझे कभी मौका नहीं मिला। –