try:
tweets=twitter_client.get_user_timeline_tweets(USER_TWEETS_TO_CAPTURE,user_id)
exceptTweepErroraserr:
iferr.api_code==401:
logger.error(f"you are not authorized to access this user {user_id}")
else:
print(err.api_code)
print(err.args)
logger.error(f"Exception occurred with {user_id}{err.api_code}")
Expected Behaviour
you are not authorized to access this user {user_id}
Actual Behaviour
None
('Twitter error response: status code = 401',)
Exception occurred with {user_id} None
I read here
That the 401 error is not supported yet, but an alternative would be to get the error code via:
e.args[0][0]['code']
But that throws me this error:
print(err.args[0][0]['code'])
TypeError: string indices must be integers
Am I missing something?
The text was updated successfully, but these errors were encountered:
TweepError.api_code corresponds to the Twitter API error code in the response. If there isn't one, then it will be None. This is different from the HTTP status code of the response.
See https://developer.twitter.com/en/docs/basics/response-codes.
BaseException.args simply returns a tuple of arguments given to the exception constructor. In the case of TweepError, only TweepError.reason is passed to that constructor:
This should also be apparent where you printed e.args and it was a tuple containing a single string.
With e.args[0][0]['code'], you're accessing the first character of the string in the tuple, 'T', and attempting to index that character by 'code', i.e. 'T'['code']. This results in the TypeError, and as it says, string indices have to be integers. I'd advise against copying and pasting code from StackOverflow that you don't understand, especially from answers that are more than 6 years old.
To access the HTTP status code from the TweepError, you can simply use TweepError.response.status_code, as TweepError.response will be the requests.Response object corresponding with the response from Twitter's API if there was one.
I want to get the error code via err.api_code, but it just returns "None".
But in err.args is the actual error code.
Some people are using it, like here
This is my code:
Expected Behaviour
you are not authorized to access this user {user_id}
Actual Behaviour
None
('Twitter error response: status code = 401',)
Exception occurred with {user_id} None
I read here
That the 401 error is not supported yet, but an alternative would be to get the error code via:
But that throws me this error:
Am I missing something?
The text was updated successfully, but these errors were encountered: