Response object is not a dict

python

#1

My app is approved for the developer access. I am trying out twitter_ads SDK with these simple lines of python code.

# initialize the client
client = Client(
    CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

account = client.accounts(id=ACCOUNT_ID)

On this line above, I am getting this exception. How can I find out what went wrong?

Traceback (most recent call last):
File “twitter.py”, line 15, in
account = client.accounts(id=ACCOUNT_ID)
File “/Library/Python/2.7/site-packages/twitter_ads/client.py”, line 89, in accounts
return Account.load(self, id) if id else Account.all(self)
File “/Library/Python/2.7/site-packages/twitter_ads/account.py”, line 54, in load
return Cursor(klass, request, init_with=[client])
File “/Library/Python/2.7/site-packages/twitter_ads/cursor.py”, line 29, in init
self.__from_response(request.perform())
File “/Library/Python/2.7/site-packages/twitter_ads/cursor.py”, line 91, in __from_response
self._collection.append(obj.from_response(item))
File “/Library/Python/2.7/site-packages/twitter_ads/resource.py”, line 43, in from_response
value = response.get(name, None)
AttributeError: ‘unicode’ object has no attribute ‘get’

When I print out the response object, which is a string, it becomes just ‘name’.


#2

Have you tried running any of the provided examples that are in the twitter-python-ads-sdk repository? Could you give those a go? Those should all work as expected and you can build your code up from those as a reference.


#3

Thank you for your response Andrs. Yes, this is actually from running the examples and it failed on this line:

I debugged this a little bit and see the data element I got back in response is a dict
{“data_type”:“account”,“data”:{“name”:“lyft”,“timezone”:“America/Los_Angeles”,“timezone_switch_at”:“2013-06-08T07:00:00Z”,“id”:“nrjmpf”,“created_at”:“2013-05-21T18:21:36Z”,“salt”:“6d1587bd32c9c8f12772aabd6d472b82”,“updated_at”:“2016-01-04T03:22:31Z”,“approval_status”:“ACCEPTED”,“deleted”:false},“request”:{“params”:{“account_id”:“nrjmpf”}}}

But it seems the SDK code expect it to be a list here.

I modified my local cursor.py to wrap data in a list which helped pass the line above and the example appears fine executing
account = client.accounts(id=ACCOUNT_ID)

However if I want to immediately do

for campaign in account.campaigns():

I got

AttributeError: ‘Cursor’ object has no attribute ‘campaigns’.

the account should be a Account class not Cursor, right?


#4

Thanks for digging into this and being so thorough @AngryNotBird :slightly_smiling:

It sounds like you might’ve stumbled upon a bug - do you mind opening up an issue on our GitHub repo? If you think you’ve addressed it with your changes in cursor.py please do go ahead and open a pull request - we love community contributions! Have a look at our contributing guidelines so you know what you need to do.

In terms of wanting to do something like for campaign in account.campaigns(), wrapping account.campaigns() around list should do the trick for you (have a look at this quick start example) :slightly_smiling: