嘿,我想用nodejs编写一个机器人来更改个人资料图片
var Twitter = require('twitter');
var data = require('fs').readFileSync('image1.png');
var client = new Twitter({
consumer_key: 'asd',
consumer_secret: 'asd',
access_token: 'asd',
access_token_secret: 'asd'
});
client.post('account/update_profile_image', {image: data}, function(error, tweet, response) {
console.log("done"); // Raw response object.
});
好了,但我的个人资料照片没有改变
答案 0 :(得分:1)
如文档中所述,图像必须是base64编码的。这是一个工作代码:
var fs = require('fs');
var Twit = require('twit'); // npm install twit
var T = new Twit({
consumer_key: '*',
consumer_secret: '*',
access_token: '*',
access_token_secret: '*'
});
var image64str = fs.readFileSync('kitten.jpg', {encoding: 'base64'});
T.post('account/update_profile_image', { image: image64str }, function(err) {
if(err) {console.error(err); return;}
console.log('done');
});