2012-10-16 34 views
7

Tôi đang cố đăng hình ảnh lên Twitter bằng mô-đun Oauth. Đây là những gì tôi có:Đăng hình ảnh lên twitter trong Node.js bằng Oauth

Nó ném một lỗi 403, tôi biết im làm điều gì đó sai trái với cách tôi thêm phương tiện vào bài đăng nhưng Im chỉ không chắc chắn nơi để đi từ đây.

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

Dưới đây là những gì tôi rất thích làm nhưng tôi không biết cách thực hiện điều đó trong mô-đun Oode của Node.js. Posting image to twitter using Twitter+OAuth

+0

trước tiên, bạn đã cài đặt nodej chưa? – Chamilyan

+0

Có và im có thể cập nhật trạng thái twitter của tôi bằng cách sử dụng trạng thái/cập nhật thông thường thông qua api.twiiter.com mà không phải với phương tiện. –

+0

Tôi tin rằng sự cố của tôi gắn dữ liệu biểu mẫu vào bài đăng. –

Trả lời

13

Rà soát các mã, có vẻ như không có multipart/form-data xử lý ở tất cả trong gói node-oauth ngay bây giờ. Bạn vẫn có thể sử dụng chức năng node-oauth để tạo tiêu đề ủy quyền, nhưng bạn sẽ phải tự mình làm nhiều việc.

Có thể các thư viện của bên thứ ba có thể trợ giúp với điều đó, nhưng dưới đây là cách tôi làm cho nó hoạt động được xây dựng bằng tay.

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

Cảm ơn bạn đã dành thời gian để viết mã đó! – Luc

+0

Rõ ràng có một số cải tiến được thực hiện - đặc biệt là 'ranh giới' phải có phần tử ngẫu nhiên, nhưng đây là điều đầu tiên tôi trộn với nhau để làm việc hợp lý hoàn toàn mới với JS, và dự án của chúng tôi đã chuyển sang kể từ đó Tôi không bao giờ có cơ hội. –

Các vấn đề liên quan