2015-10-15 5 views
0

Я пытаюсь сделать автоматический пост на моей странице Facebook с моего сервера, я использую Nodejs и Facebook-узловой-SDK.Как разместить на Facebook страницу с медиа-контентом с помощью Node JS

Моя проблема заключается в том, что я не в состоянии добавить изображение, и я не знаю, как загрузить видео с помощью этого пакета.

это мой код, чтобы разместить на моей странице тексте с изображением (от URL), но это не работает у меня есть только сообщение, появляющееся на моей странице.

var FB = require('fb'); 
var request = require('request'); 
FB.setAccessToken('MY_PAGE_TOKEN'); 

var body = 'My first post using facebook-sdk'; 
FB.api('me/feed/', 'post', { message: body ,url : 'http://google.com/doodle.png'}, function (res) { 
    if(!res || res.error) { 
    console.log(!res ? 'error occurred' : res.error); 
    return; 
    } 
    console.log('Post Id: ' + res.id); 
}); 

ответ

0

Чтобы загрузить фотографию, которую необходимо отправить в конечную точку/photos, а не конечную точку/feed.

https://developers.facebook.com/docs/graph-api/reference/user/photos/#Creating

FB.api('me/photos/', 'post', { caption: 'Photo description' ,url : 'http://google.com/doodle.png'}, function (res) { 
    if(!res || res.error) { 
    console.log(!res ? 'error occurred' : res.error); 
    return; 
    } 
    console.log('Post Id: ' + res.id); 
}); 
0

Вот код, который я использовал год назад, чтобы загрузить видео в Node Js с использованием Facebook Graph API:

// It publishes the video in chunks to Facebook until it finishes 
var publishVideo = function(access_token, text, file) { 

    var stats = fs.statSync(file.path); 

    var formData = { 
    access_token: access_token, 
    upload_phase: 'start', 
    file_size: stats.size 
    }; 

    // First step, we send the video size 
    request.post({ url: 'https://graph-video.facebook.com/v2.3/me/videos', form: formData, json: true }, 
    function(err, response, body) { 
    if (!err) { 
     // With the response, we start making the video transfer 
     transferProcess(undefined, file, access_token, body, function(err, currentUploadSession) { 
     if (!err) { 

      var formData = { 
      access_token: access_token, 
      upload_phase: 'finish', 
      upload_session_id: currentUploadSession, 
      description: text 
      }; 

      // Once the video transfer ended, we publish the video 
      request.post({ url: 'https://graph-video.facebook.com/v2.3/me/videos', form: formData, json: true }); 
     } 
     }); 
    } 
    }); 
}; 

// It processes each part of the video until it finishes 
var transferProcess = function(uploadSession, file, access_token, body, callback) { 

    // First we generate a copy of the file in order to be independent to the original file 
    // because it can have problems when opening it at the same time from other file 
    var copyFileName = file.path + '-facebook'; 
    fse.copySync(file.path, copyFileName); 

    // Once we have the copy, we open it 
    var fd = fs.openSync(copyFileName, 'r'); 

    var bytesRead, data, bufferLength = 1000000000; 
    var buffer = new Buffer(bufferLength); 

    var length = body.end_offset - body.start_offset; 

    // We read the amount of bytes specified from body.start_offset until length 
    bytesRead = fs.readSync(fd, buffer, body.start_offset, length, null); 
    data = bytesRead < bufferLength ? buffer.slice(0, bytesRead) : buffer; 

    // We generate a file with the recently read data, and with a name of copyFileName-chunked-12313123 
    var chunkFileName = copyFileName + '-chunked-' + body.start_offset; 

    // We create the file so then we can read it and send it 
    fs.writeFile(chunkFileName, data, function(err) { 
    if (err) { 
     callback(err); 
    } 
    else { 

     var currentUploadSession = uploadSession ? uploadSession : body.upload_session_id; 
     var startOffset = parseInt(body.start_offset); 

     var formData = { 
     upload_phase: 'transfer', 
     start_offset: startOffset, 
     upload_session_id: currentUploadSession, 
     access_token: access_token 
     }; 

     formData.video_file_chunk = fs.createReadStream(chunkFileName); 

     // Once we have the file written, we upload it 
     request.post({ url: 'https://graph-video.facebook.com/v2.3/me/videos', 
     formData: formData, json: true }, function (err, response, body) { 
     // If there was an error, we return it 
     if (err || body.error) { 
      callback(err ? err : body.error, null); 
     } 
     // If the lecture of the file has ended, facebook will send us the body.start_offset and the body.end_offset, 
     // if they are the same, it means that we have ended uploading the video, so we return 
     else if (body.start_offset === body.end_offset) { 
      callback(err, currentUploadSession); 
     } 
     // Else, we keep reading the file 
     else { 
      transferProcess(currentUploadSession, file, access_token, body, callback); 
     } 
     }); 
    } 
    }); 
}; 

Надеется, что вы можете использовать его.

Joel

Смежные вопросы