我正在尝试通过API在Twitter上发布帖子。我希望用户填写一个表单,提交后在Twitter上用表单信息发布一个帖子,我可以轻松做到这一点,
let tweet = request.body.text + '\n' + request.body.hashtags;
T.post('statuses/update', {
status: tweet
}, function(err, data, response) {
console.log(data)
created_at = data.created_at;
post_id = data.id_str;
hashtags = request.body.hashtags
user = data.user.name;
screen_name = data.user.screen_name;
text = request.body.text;
let obj = {
post_id: post_id,
created_at: created_at,
text: text,
hashtags: hashtags,
screen_name: screen_name,
user: user
}
let posts = []
posts.push(obj)
TwitterModel.TwitterModel.insertMany(obj, (error, docs) => {
if (error) return resp.status(MESSAGES.error.e0.http).send(error);
return resp.render('back-office.ejs');
});
})
这很好用, 但是现在我想让用户能够添加图像,为此,我需要在此处使用此代码。
//
// post a tweet with media
//
var b64content = fs.readFileSync('/path/to/img', {
encoding: 'base64'
})
// first we must post the media to Twitter
T.post('media/upload', {
media_data: b64content
}, function(err, data, response) {
// now we can assign alt text to the media, for use by screen readers and
// other text-based presentations and interpreters
var mediaIdStr = data.media_id_string
var altText = "Small flowers in a planter on a sunny balcony, blossoming."
var meta_params = {
media_id: mediaIdStr,
alt_text: {
text: altText
}
}
T.post('media/metadata/create', meta_params, function(err, data, response) {
if (!err) {
// now we can reference the media and post a tweet (media will attach to the tweet)
var params = {
status: 'loving life #nofilter',
media_ids: [mediaIdStr]
}
T.post('statuses/update', params, function(err, data, response) {
console.log(data)
})
}
})
})
要执行此操作,我需要获取我不知道该怎么做的图像的相对路径,用户通过(input type =“ file”)上传图像。 我相信必须有一种非常简单的方法来获得我不知道的相对路径,但是也许有人可以告诉我。 我目前正在尝试使用Firebase Storage,但似乎无法使用它,我能够将图像存储在Firebase上,但是我不知道如何在API请求中实际使用它。 PS:我正在使用Twit NPM模块在Twitter上发布