开发者

Twitter, Error: urllib.error.HTTPError: HTTP Error 401: Unauthorized

开发者 https://www.devze.com 2023-01-17 04:46 出处:网络
def send_to_twitter(): msg = \"I am a message that will be sent to Twitter\" password_manager = urllib.request.HTTPPasswordMgr()
def send_to_twitter():
    msg = "I am a message that will be sent to Twitter"
    password_manager = urllib.request.HTTPPasswordMgr()
    password_manager.add_password("Twitter API",
    "http://twitter.com/statuses", "username", "password")
    http_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
    page_opener = urllib.request.build_opener(http_handler)
    urllib.request.install_opener(page_opener开发者_如何学JAVA)
    params = urllib.parse.urlencode( {'status': msg} )
    resp = urllib.request.urlopen("http://twitter.com/statuses/update.json", params)
    resp.read()

When I execute this code through IDLE, I get an error along the lines of;

urllib.error.HTTPError: HTTP Error 401: Unauthorized

What maybe wrong with my code?


Twitter no longer supports HTTP Authentication. You should use oauth in stead.

tweepy seems to be a good library to start with if you want to do twitter from python: it's current, supports oath and looks very complete.


Your are using the old HTTP Auth method. Upgrade using OAuth.

Go to the following link to setup your Twitter Application: twitter.com/apps/new (Tip: You can set your URL to 127.0.0.1 for testing purposes AND Remember to set application to Browser and specify Callback URL)

And you can continue from there.

0

精彩评论

暂无评论...
验证码 换一张
取 消