Using twitter API get error sometimes
Asked Answered
F

2

11

I use the following code which works sometimes but its unstable , when I run the program sometimes I got error 420 with json parse error which doesnt give you lot of hints how to solve it. any idea what am I doing wrong ?

The error is :

Error getting tweets: Error: Status Code: 420 Error getting tweets: SyntaxError: Unexpected token E in JSON at position 0

var Twitter=require('twitter');
var lclconf = require('../config.json');


var client=new Twitter({
  consumer_key: lclconf.twitter.consumer_key,
  consumer_secret: lclconf.twitter.consumer_secret,
  access_token_key: lclconf.twitter.access_token_key,
  access_token_secret: lclconf.twitter.access_token_secret
});
stream.on("data", function(data){
  console.log(data.id_str);
  var tweet_id="https://api.twitter.com/1.1/statuses/oembed.json?id="+data.id_str;
  request.get(tweet_id)
  .end(function(err,res){
      if(err){
        console.log("Error from Twitter API: " + err);
      }else{
        //console.log(res.body);
        io.emit('tweet',res.body);
      }
  });
});
stream.on('error', function(err){
  console.log("Error getting tweets: "+err);
});
io.on('connection', function(client){
  client.on("join", function(data){
    console.log(data);
  });
  client.emit("join",{"message":"running"});
});

Maybe if there is a way that when the error is occurred ignore it and proceed since now the process is stopped.

Update:

In twitter docs there is info about HTTP 420 but not sure how to fix it ...

Falla answered 15/2, 2017 at 12:9 Comment(0)
H
3

As per pii_ke's response you should simply modify tweet_id as follows:

var tweet_id = "https://publish.twitter.com/oembed?url=https://twitter.com/" + data.user.screen_name + "/statuses/" + data.id_str;

Full modified code you can copy paste:

var Twitter=require('twitter');
var lclconf = require('../config.json');


var client=new Twitter({
  consumer_key: lclconf.twitter.consumer_key,
  consumer_secret: lclconf.twitter.consumer_secret,
  access_token_key: lclconf.twitter.access_token_key,
  access_token_secret: lclconf.twitter.access_token_secret
});
stream.on("data", function(data){
  console.log(data.id_str);
  var tweet_id = "https://publish.twitter.com/oembed?url=https://twitter.com/" + data.user.screen_name + "/statuses/" + data.id_str;
  request.get(tweet_id)
  .end(function(err,res){
      if(err){
        console.log("Error from Twitter API: " + err);
      }else{
        //console.log(res.body);
        io.emit('tweet',res.body);
      }
  });
});
stream.on('error', function(err){
  console.log("Error getting tweets: "+err);
});
io.on('connection', function(client){
  client.on("join", function(data){
    console.log(data);
  });
  client.emit("join",{"message":"running"});
});
Hernando answered 23/2, 2017 at 12:28 Comment(1)
Thanks I try it but I got the same erorr :( , any other idea?Falla
V
6

HTTP 420 is returned when you are being rate limited.

There is a https://publish.twitter.com/oembed resource URL, that is neither rate limited nor requires authentication. I think it returns the same things that your program expects. You can use that if you pass a query parameter url having the link to tweet. Try making the link like:

"https://publish.twitter.com/oembed?url=https://twitter.com/"+data.user.screen_name+"/statuses/"+data.id_str

For sample data returned by Twitter click here

Victualage answered 18/2, 2017 at 13:39 Comment(2)
Thanks, but not sure how to do it with your example, can you provide the complite example with my code ?Falla
I do not program in Javascript much. I can see how to create Twitter and client objects in your code, but I am not sure about the objects request, stream, and io. If you tell how to create these then I can test something. @shopiaTVictualage
H
3

As per pii_ke's response you should simply modify tweet_id as follows:

var tweet_id = "https://publish.twitter.com/oembed?url=https://twitter.com/" + data.user.screen_name + "/statuses/" + data.id_str;

Full modified code you can copy paste:

var Twitter=require('twitter');
var lclconf = require('../config.json');


var client=new Twitter({
  consumer_key: lclconf.twitter.consumer_key,
  consumer_secret: lclconf.twitter.consumer_secret,
  access_token_key: lclconf.twitter.access_token_key,
  access_token_secret: lclconf.twitter.access_token_secret
});
stream.on("data", function(data){
  console.log(data.id_str);
  var tweet_id = "https://publish.twitter.com/oembed?url=https://twitter.com/" + data.user.screen_name + "/statuses/" + data.id_str;
  request.get(tweet_id)
  .end(function(err,res){
      if(err){
        console.log("Error from Twitter API: " + err);
      }else{
        //console.log(res.body);
        io.emit('tweet',res.body);
      }
  });
});
stream.on('error', function(err){
  console.log("Error getting tweets: "+err);
});
io.on('connection', function(client){
  client.on("join", function(data){
    console.log(data);
  });
  client.emit("join",{"message":"running"});
});
Hernando answered 23/2, 2017 at 12:28 Comment(1)
Thanks I try it but I got the same erorr :( , any other idea?Falla

© 2022 - 2024 — McMap. All rights reserved.