I think i defined ‘api’ as twitter.api, idk why this error is happening
code:
import twitter
def auth():
api = twitter.Api(consumer_key='CsqkkrnhBZQMhGLpnkqGqOUOV',
consumer_secret='jzbWgRLZqIyJQjfh572LgbtuifBtXw6jwm1V94oqcQCzJd7VAE',
access_token_key='1300635453247361031-EWTTGf1B6T2GUqWmFwzLfvgni3PoVH',
access_token_secret='U2GZsWT0TvL5U24BG9X4NDAb84t1BB059qdoyJgGqhWN4')
auth()
api.PostUpdate('Hello World')
error:
Traceback (most recent call last):
File "C:/Users/Xtrike/AppData/Local/Programs/Python/Python37/twitter python.py", line 11, in <module>
api.PostUpdate('Hello World')
NameError: name 'api' is not defined
2
Answers
For what you posted, you need to initiate
api
variable. It just get everything and do thePostUpdate
, but first you need to instantiate it.You may need to learn about local and global scopes in Python. In short you’ve created a local variable
api
that is not visible outside of function.As of solving the provided error, there are different approaches depending on desired result:
global
to make variable visible at global scope:However I’d not recommend using global variables without proper conscise
Last, but important word: avoid publishing secrets in public posts – these are not needed for solution but may be exposed to wrecker.