python script execution failed due to tweepy error 401 - python

I'm using below code to streaming tweets and analyse them for making decisions. while running the below code I got an error. that error occurs twitter users those who had the friend list of more than 50.
import re
import tweepy
import sys
import time
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)
users = tweepy.Cursor(api.friends, screen_name='#myuser').items()
while True:
try:
user = next(users)
except tweepy.TweepError:
time.sleep(60*15)
user = next(users)
except StopIteration:
break
for status in tweepy.Cursor(api.user_timeline,screen_name=user.screen_name,result_type='recent').items(5):
text=status._json['text'].translate(non_bmp_map)
print (user.screen_name + ' >>>>>> '+text)
while executing this script I have got an error as below.
Traceback (most recent call last):
File "D:sensitive2demo.py", line 31, in <module>
for status in tweepy.Cursor(api.user_timeline,screen_name=user.screen_name,result_type='recent').items(5):
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tweepy-3.6.0-py3.6.egg\tweepy\cursor.py", line 49, in __next__
return self.next()
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tweepy-3.6.0-py3.6.egg\tweepy\cursor.py", line 197, in next
self.current_page = self.page_iterator.next()
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tweepy-3.6.0-py3.6.egg\tweepy\cursor.py", line 108, in next
data = self.method(max_id=self.max_id, parser=RawParser(), *self.args, **self.kargs)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tweepy-3.6.0-py3.6.egg\tweepy\binder.py", line 250, in _call
return method.execute()
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tweepy-3.6.0-py3.6.egg\tweepy\binder.py", line 234, in execute
raise TweepError(error_msg, resp, api_code=api_error_code)
tweepy.error.TweepError: Twitter error response: status code = 401
I have googled a lot.but nothing worked. Can somebody help me to solve the problem?

401 is an http status code for 'Unauthorized'. I would suggest verifying your credentials.

Related

Not able to handle the exception for Connection Reset while executing twitter streaming in Python

I am trying to catch the exception which is raised when the connection is reset from the peer during the real-time streaming of tweet, but seems the try-exception block is not properly catching the error raised and pass through it. Please advise, if the block is not rightly placed in the code or there is something wrong with the code.
I have created a script that will stream the tweet in real time to an excel file. Lot of times it has happened that streaming got disconnected due to ECONNRESET error which is connection reset by peer -
Exception in thread Thread-1:
Traceback (most recent call last):
File “/usr/lib/python2.7/threading.py”, line 801, in __bootstrap_inner
self.run()
File “/usr/lib/python2.7/threading.py”, line 754, in run
self.__target(*self.__args, **self.__kwargs)
File “/usr/local/lib/python2.7/dist-packages/tweepy/streaming.py”, line 297, in _run
six.reraise(*exc_info)
File “/usr/local/lib/python2.7/dist-packages/tweepy/streaming.py”, line 266, in _run
self._read_loop(resp)
File “/usr/local/lib/python2.7/dist-packages/tweepy/streaming.py”, line 316, in _read_loop
line = buf.read_line().strip()
File “/usr/local/lib/python2.7/dist-packages/tweepy/streaming.py”, line 181, in read_line
self._buffer += self._stream.read(self._chunk_size)
File “/usr/local/lib/python2.7/dist-packages/urllib3/response.py”, line 430, in read
raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
File “/usr/lib/python2.7/contextlib.py”, line 35, in exit
self.gen.throw(type, value, traceback)
File “/usr/local/lib/python2.7/dist-packages/urllib3/response.py”, line 349, in _error_catcher
raise ProtocolError(‘Connection broken: %r’ % e, e)
ProtocolError: (‘Connection broken: error("(104, ‘ECONNRESET’)",)’, error("(104, ‘ECONNRESET’)",))
Its a protocol error and i tried to catch this error by importing urllib3 library as it has protocol exceptions, but the try and exception block is not able to suppress it and continue with the streaming.
import pandas as pd
import csv
from bs4 import BeautifulSoup
import re
import tweepy
import ast
from datetime import datetime
import time
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import json
from unidecode import unidecode
from urllib3.exceptions import ProtocolError
from urllib3.exceptions import IncompleteRead
import requests
consumer_key= 'xxxxxxxxx'
consumer_secret= 'xxxxxxxxx'
access_token= 'xxxxxxxxx'
access_token_secret= 'xxxxxxxxx'
with open('TEST_FEB.csv','w')as f:
f.truncate()
f.close()
class listener(StreamListener):
def on_data(self,data):
data1 = json.loads(data)
time = data1["created_at"]
if hasattr(data1,"retweeted_status:"):
tweet = unidecode(data1["tweet"]["text"])
if data1["truncated"] == "true":
tweet = unidecode(data1["extended_tweet"]["full_text"])
else:
tweet = unidecode(data1["text"])
tweet1 = BeautifulSoup(tweet, "lxml").get_text()
url = "https://twitter.com/{}/status/{}".format(data1["user"]
["screen_name"], data1["id_str"])
file = open('TEST_FEB.csv', 'a')
csv_writer = csv.writer(file)
csv_writer.writerow([time, tweet1, url])
file.close()
def on_limit(self, track):
return True
auth = OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_token,access_token_secret)
while True:
try:
twitterStream = Stream(auth, listener(),
wait_on_rate_limit=True, retry_count=10, stall_warnings=True)
twitterStream.filter(track=["abcd"], async = True)
except ProtocolError as error:
print (str(error))
continue
except IncompleteRead as IR:
print (str(IR))
continue
The expected result is that whenever the connection is reset from the peer and the said error is raised, the code should suppress it and continue with the streaming. The code in the current form is not working that way.

Twython Not capturing errors

I am using Twython to capture a stream of tweets from a group of users. I worked for an hour or so quite well (just a few tweets) and then crashed with an HTTP error IncompleteRead. I saw this discussed in a few posts but never resolved.
Is there any way to capture this error so it does not crash the program?
File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\response.py", line
331, in _error_catcher
yield
File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\response.py",
line 640, in read_chunked
chunk = self._handle_chunk(amt)
File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\response.py",
line 586, in _handle_chunk
value = self._fp._safe_read(amt)
File "C:\ProgramData\Anaconda3\lib\http\client.py", line 612, in
_safe_read
raise IncompleteRead(b''.join(s), amt)
http.client.IncompleteRead: IncompleteRead(0 bytes read, 1 more expected)
My code is simple and I see no other options to trap these errors.
from twython import TwythonStreamer
CONSUMER_KEY = '...'
CONSUMER_SECRET = '...'
# Access:
ACCESS_TOKEN = '...'
ACCESS_SECRET = '....'
class MyStreamer(TwythonStreamer):
def on_success(self, data):
if 'text' in data:
if not data['text'].startswith('RT') and not
data['text'].startswith('#'):
print(data['text'])
def on_error(self, status_code, data):
print(status_code)
self.disconnect()
stream = MyStreamer(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN,
ACCESS_SECRET)
# follow is watching for tweets from a user or list of users
users = [25073877, 19905457,1058764970010308611,251918778]
stream.statuses.filter(follow=users, language = 'en')

dropbox.rest.ErrorResponse: [400] u'invalid_client' dropbox api fom python

Following the tutorial from the python api I get this error
Traceback (most recent call last):
File "/home/user/PycharmProjects/api/apid.py", line 17, in <module>
access_token, user_id = flow.finish(code)
File "/usr/local/lib/python2.7/dist-packages/dropbox/client.py", line 1398, in finish
return self._finish(code, None)
File "/usr/local/lib/python2.7/dist-packages/dropbox/client.py", line 1265, in _finish
response = self.rest_client.POST(url, params=params)
File "/usr/local/lib/python2.7/dist-packages/dropbox/rest.py", line 316, in POST
return cls.IMPL.POST(*n, **kw)
File "/usr/local/lib/python2.7/dist-packages/dropbox/rest.py", line 254, in POST
post_params=params, headers=headers, raw_response=raw_response)
File "/usr/local/lib/python2.7/dist-packages/dropbox/rest.py", line 227, in request
raise ErrorResponse(r, r.read())
dropbox.rest.ErrorResponse: [400] u'invalid_client'
And the code its the same of the tutorial
#!/usr/bin/env python
import dropbox
app_key = 'key'
app_secret = 'secret'
flow = dropbox.client.DropboxOAuth2FlowNoRedirect(app_key, app_secret)
authorize_url = flow.start()
print '1. Go to: ' + authorize_url
print '2. Click "Allow" (you might have to log in first)'
print '3. Copy the authorization code.'
code = raw_input("Enter the authorization code here: ").strip()
access_token, user_id = flow.finish(code)
client = dropbox.client.DropboxClient(access_token)
print 'linked account: ', client.account_info()
I haven't changed anything from the original code

TweepError: [{u'message': u'SSL is required', u'code': 92}]

Here is my python script to fetch the tweets from my account
import tweepy
consumer_key=""
consumer_secret=""
access_token=""
acces_token_secret=""
auth=tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api.tweepy.API(auth)
public_tweets = api.home_timeline()
for tweet in public_tweets:
print tweet.text
But I am getting the following error:
Traceback (most recent call last):
File "C:\enter code herePython27\twitter_api.py", line 16, in <module>
public_tweets = api.home_timeline()
File "build\bdist.win32\egg\tweepy\binder.py", line 185, in _call
return method.execute()
File "build\bdist.win32\egg\tweepy\binder.py", line 168, in execute
raise TweepError(error_msg, resp)
TweepError: [{u'message': u'SSL is required', u'code': 92}]
Please help
Try this:
auth=tweepy.OAuthHandler(consumer_key, consumer_secret, secure=True)

Working with Twitter API v1.1

I launched this code via terminal via command python py/twi.py and it shows no reaction:
import oauth, tweepy
from time import sleep
message = "hello"
def init():
global api
#confident information
consumer_key = "***"
consumer_secret = "***"
callback_url = "https://twitter.com/Problem196"
access_key="***"
access_secret="***"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret, callback_url)
auth.set_access_token(access_key, access_secret)
api=tweepy.API(auth)
init()
api.update_status(message)
But it supposed to post tweet "hello" on page https://twitter.com/problem196 .
I have updated tweepy, it's fine. Why it's not posting? I have no idea. Please help.
UPD: After I put code print api.last_response.msg terminal showed me some errors:
artem#artem-VirtualBox:~$ python py/twi.py
Traceback (most recent call last):
File "py/twi.py", line 20, in <module>
api.update_status(message)
File "/usr/local/lib/python2.7/dist-packages/tweepy-2.1-py2.7.egg/tweepy/binder.py", line 197, in _call
return method.execute()
File "/usr/local/lib/python2.7/dist-packages/tweepy-2.1-py2.7.egg/tweepy/binder.py", line 154, in execute
raise TweepError('Failed to send request: %s' % e)
tweepy.error.TweepError: Failed to send request: [Errno -2] Name or service not known
artem#artem-VirtualBox:~$ python py/twi.py
Traceback (most recent call last):
File "py/twi.py", line 20, in <module>
api.update_status(message)
File "/usr/local/lib/python2.7/dist-packages/tweepy-2.1-py2.7.egg/tweepy/binder.py", line 197, in _call
return method.execute()
File "/usr/local/lib/python2.7/dist-packages/tweepy-2.1-py2.7.egg/tweepy/binder.py", line 173, in execute
raise TweepError(error_msg, resp)
tweepy.error.TweepError: [{'message': 'Status is a duplicate', 'code': 187}]

Categories