0
votes

I'm using twitter-lite library and I want to update a Twi profile picture.

At the moment I'm trying this :

var fs = require('fs');
var Twit = require('twitter-lite'); 

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

But I get the following error :

/Users/huyvunguyen/node_modules/node-fetch/lib/index.js:272 return Body.Promise.reject(new FetchError(invalid json response body at ${_this2.url} reason: ${err.message}, 'invalid-json')); ^ FetchError: invalid json response body at https://api.twitter.com/1.1/account/update_profile_image.json reason: Unexpected end of JSON input at /Users/huyvunguyen/node_modules/node-fetch/lib/index.js:272:32 at processTicksAndRejections (node:internal/process/task_queues:96:5) { type: 'invalid-json' }

Anyone know how can I debug this ?

1

1 Answers

0
votes

I don't know what exactly is causing the error. It occurred at my end, too.

But twitter-api-client works flawlessly for the same code. Maybe it is a library-specific error.

Besides, .post method in twitter-lite doesn't take a callback. More info here.

Here's what you can use:

var fs = require('fs');
const { TwitterClient } = require('twitter-api-client')

var image64str = fs.readFileSync('pic.jpg', {encoding: 'base64'});

client = new TwitterClient({
    apiKey: '',
    apiSecret: '',
    accessToken: '',
    accessTokenSecret: ''
})

client.accountsAndUsers.accountUpdateProfileImage({image: image64str}).catch(e=>console.log(e)).then(res => console.log(res))