Traceback when updating status on twitter via Tweepy - python

I've been trying to post the readings from my Rpi on Twitter using tweepy, but first I wanted to check if tweepy was working properly, but it's not.
I installed the packages properly, but when I'm trying to run a simple code to post something, I got an error (Yes, I already created an app and have the 4 credentials).
The code I'm trying to run:
import tweepy
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)
single_tweet = 'hello world'
api.update_status(single_tweet)
print "successfully Updated"
I got this:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.linux-armv6l/egg/tweepy/api.py", line 193, in update_status
File "build/bdist.linux-armv6l/egg/tweepy/binder.py", line 239, in _call
File "build/bdist.linux-armv6l/egg/tweepy/binder.py", line 223, in execute
tweepy.error.TweepError: [{u'message': u'media_ids parameter is invalid.', u'code': 44}]
I'm runing the python code that is in the tweepy folder "oauth.py" (adding it my credentials)
$ sudo python oauth.py
RapiCARA
Traceback (most recent call last):
File "oauth.py", line 34, in <module>
api.update_status(' Hello world ')
File "build/bdist.linux-armv6l/egg/tweepy/api.py", line 193, in update_status
File "build/bdist.linux-armv6l/egg/tweepy/binder.py", line 239, in _call
File "build/bdist.linux-armv6l/egg/tweepy/binder.py", line 223, in execute
tweepy.error.TweepError: [{u'message': u'media_ids parameter is invalid.', u'code': 44}]
The code in that file, you can find it in its origial source: Tweepy in GITHUB
Any help/advice please?

The first positional argument to the update_status() method is interpreted as the media_ids parameter. You need to explicitly name your status parameter to avoid this:
api.update_status(status=single_tweet)
This is a recent change in Tweepy and it looks like their documentation hasn't been updated yet to reflect this.
The different signature has been reported as a bug for the project.
The bug was fixed in August 2015; version 3.5 or newer of Tweepy once again treat the first positional argument as the status parameter.

Related

Why do I get error "Expected a message object, but got kind: " when using Datastore API?

I'm using this Datastore API documentation and here is my code in Python:
class DatastoreConf:
def __init__(self):
self.datastore_client = datastore.Client()
def insert_entity(self):
complete_key = self.datastore_client.key("Task", "sampleTask")
task = datastore.Entity(key=complete_key)
task.update(
{
"category": "Personal",
"done": False,
"priority": 4,
"description": "Learn Cloud Datastore",
}
)
self.datastore_client.put(task)
d = DatastoreConf()
d.insert_entity()
I've already set GOOGLE_APPLICATION_CREDENTIALS variable to my service account key and I have sufficient roles, but I'm getting the error
TypeError: Expected a message object, but got kind: "Task" name: "sampleTask".
The traceback that I got is:
Traceback (most recent call last):
File "C:/Users/user/project/datastore_configuration.py", line 24, in <module>
d.get_entity()
File "C:/Users/user/project/datastore_configuration.py", line 21, in get_entity
self.datastore_client.put(task)
File "C:\Users\user\project\venv\lib\site-packages\google\cloud\datastore\client.py", line 575, in put
self.put_multi(entities=[entity], retry=retry, timeout=timeout)
File "C:\Users\user\projectvenv\lib\site-packages\google\cloud\datastore\client.py", line 612, in put_multi
current.put(entity)
File "C:\Users\user\project\venv\lib\site-packages\google\cloud\datastore\batch.py", line 227, in put
_assign_entity_to_pb(entity_pb, entity)
File "C:\Users\user\project\venv\lib\site-packages\google\cloud\datastore\batch.py", line 373, in _assign_entity_to_pb
bare_entity_pb = helpers.entity_to_protobuf(entity)
File "C:\Users\user\project\venv\lib\site-packages\google\cloud\datastore\helpers.py", line 207, in entity_to_protobuf
key_pb = entity.key.to_protobuf()
File "C:\Users\user\project\venv\lib\site-packages\google\cloud\datastore\key.py", line 298, in to_protobuf
key.path.append(element)
Do you have any idea what causes this error?
I resolved the problem. The error was due to version difference. Datastore worked with library protobuf 3.20.1 but didn't with 4.21.0.
I think it's beacause Firestore is now developed much more and the versions in Datastore are no longer supported.

While True Error in Tweepy while Tweeting in Python

Everyone. Today, I was creating a Twitter Bot in Python using the tweepy module, which works perfectly. I've observed that when I use a While True command to tweet anything, it will Tweet it at first, so show me an error here it is below.
Response(data={'id': '1522239311277486080', 'text': 'Alas, after a certain age every man is responsible for his face.-Albert Camus'}, includes={}, errors=[], meta={})
Traceback (most recent call last):
File "C:\Users\varni\PycharmProjects\Twitter_Bot Version 1\main.py", line 13, in <module>
response = client.create_tweet(text=Quotes.Quotes_Random)
File "C:\Users\varni\PycharmProjects\Twitter_Bot Version 1\venv\lib\site-packages\tweepy\client.py", line 824, in create_tweet
return self._make_request(
File "C:\Users\varni\PycharmProjects\Twitter_Bot Version 1\venv\lib\site-packages\tweepy\client.py", line 126, in _make_request
response = self.request(method, route, params=request_params,
File "C:\Users\varni\PycharmProjects\Twitter_Bot Version 1\venv\lib\site-packages\tweepy\client.py", line 99, in request
raise Forbidden(response)
tweepy.errors.Forbidden: 403 Forbidden
And here is the code-
import config
import tweepy
import Quotes
import time
client = tweepy.Client(consumer_key=config.API_KEY,
consumer_secret=config.API_SECRET,
access_token=config.ACCESS_TOKEN,
access_token_secret=config.ACCESS_TOKEN_SECRET)
while True:
# time.sleep(600)
response = client.create_tweet(text=Quotes.Quotes_Random)
print(response)
is there way to fix this error?
You're likely encountering this because you can't Tweet the exact same text you've already recently Tweeted.

Python - Get Instagram API Access Token

I am trying to call the Instagram Basic API to get the user info. However, I need to fetch the access token first for the authentication.
I have implemented the code for getting the same in python but I am only able to fetch the re-direct uri. Now, as a manual step we execute that uri on browser and get the code from the url.
But in case of programming how can I get the access token?
In below code: I am getting error
from instagram_basic_display.InstagramBasicDisplay import InstagramBasicDisplay
from flask import request
instagram_basic_display = InstagramBasicDisplay(app_id='5163403347035424', app_secret='80473de6ad506c837969e03b8ccdb4cb', redirect_url='https://mycogito.me/')
print(instagram_basic_display.get_login_url()) # Returns login URL you need to follow
code = request.args.get('code')
print(code)
Error
Traceback (most recent call last):
File "D:\Work Backup\Development\Workspaces\python\SocialMedia\src\com\social\media\HelloWorld.py", line 9, in <module>
https://api.instagram.com/oauth/authorize?client_id=5163403347035424&redirect_uri=https%3A%2F%2Fmycogito.me%2F&scope=user_profile%2Cuser_media&response_type=code
code = request.args.get('code')
File "C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\werkzeug\local.py", line 347, in __getattr__
return getattr(self._get_current_object(), name)
File "C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\werkzeug\local.py", line 306, in _get_current_object
return self.__local()
File "C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\globals.py", line 38, in _lookup_req_object
raise RuntimeError(_request_ctx_err_msg)
RuntimeError: Working outside of request context.
There is a package when you want to get data from instagram and written in python click.

Error while trying to serialise Google NLP API Response using Protobuf using Python

I am trying to pass the response of Google's NLP API to Google's Protobuf on my GCP VM. I keep getting the following response:
Traceback (most recent call last):
File "/app/scrapers_v3/utils/google_nlp.py", line 53, in get_entities_from_text
element = json_format.MessageToDict(entity)
File "/usr/local/lib/python3.7/site-packages/google/protobuf/json_format.py", line 168, in MessageToDict
return printer._MessageToJsonObject(message)
File "/usr/local/lib/python3.7/site-packages/google/protobuf/json_format.py", line 204, in _MessageToJsonObject
return self._RegularMessageToJsonObject(message, js)
File "/usr/local/lib/python3.7/site-packages/google/protobuf/json_format.py", line 240, in _RegularMessageToJsonObject
js[name] = self._FieldToJsonObject(field, value)
File "/usr/local/lib/python3.7/site-packages/google/protobuf/json_format.py", line 282, in _FieldToJsonObject
if field.file.syntax == 'proto3':
AttributeError: 'NoneType' object has no attribute 'syntax'
The value entity is an element of [json_format.MessageToDict(entity) for entity in response.entities].
Interestingly the same code runs properly on my local machine.
google-cloud-language version: 1.0.2
Protobuf version: 3.10.0
Take a look on this post about AttributeErrors messages, maybe you should re-check the proto object to be serialized

API Python smartsheet can't connect with SDK

I'm pretty new to Python and I'm trying to connect to smartsheet with API.
I have ran "pip install smartsheet-python-sdk" and it installed smartsheet as I can find it under "lib"
This is code I have found and supposed to work(I replaced the token with the token)
# Import.
import smartsheet
# Instantiate smartsheet and specify access token value.
smartsheet = smartsheet.Smartsheet('Token_here')
# Get all columns.
action = smartsheet.Sheets.get_columns('Template for Bram', include_all=True)
columns = action.data
# For each column, print Id and Title.
for col in columns:
print(col.id)
print(col.title)
print('')
It shows this error:
Traceback (most recent call last):
File "C:\Users\bram\Desktop\smartsheet.py", line 2, in <module>
import smartsheet
File "C:\Users\bram\Desktop\smartsheet.py", line 5, in <module>
smartsheet = smartsheet.Smartsheet('token_here')
AttributeError: 'module' object has no attribute 'Smartsheet'
Now I'm not sure what my next step is. I think I have followed all of the appropriate steps. When I run import smartsheet by itself it won't error out.
What am I doing wrong?
Thank you
Update***
After using the code from the github page and implementing my token and sheet id I get this error:
Traceback (most recent call last):
File "C:\Users\bvanhout\Desktop\test23.py", line 58, in <module>
sheet = ss.Sheets.get_sheet(sheet_id)
File "C:\Python27\lib\site-packages\smartsheet\sheets.py", line 460, in get_sheet
response = self._base.request(prepped_request, expected, _op)
File "C:\Python27\lib\site-packages\smartsheet\smartsheet.py", line 178, in request
res = self.request_with_retry(prepped_request, operation)
File "C:\Python27\lib\site-packages\smartsheet\smartsheet.py", line 242, in request_with_retry
return self._request(prepped_request, operation)
File "C:\Python27\lib\site-packages\smartsheet\smartsheet.py", line 210, in _request
raise UnexpectedRequestError(rex.request, rex.response)
UnexpectedRequestError: (<PreparedRequest [GET]>, None)
# TODO: Update this with the ID of your sheet to update
sheet_id = 48568543424234
I printed ss and ss.Sheets and both do not reflect the actual token or sheet_id
>>> print (ss.Sheets)
<smartsheet.sheets.Sheets object at 0x0000000003874438>
I suspect the problem is that you are using a local variable with the same name as the module ('smartsheet')
Please take a look at the sample here: https://github.com/smartsheet-samples/python-read-write-sheet

Categories

Resources