簡體   English   中英

Python-urllib2.HTTPError 400錯誤的請求

[英]Python - urllib2.HTTPError 400 bad request

我正在學習如何訪問Twitter API,但是當我運行以下簡單代碼時,出現錯誤“ urllib2.HTTPError:HTTP錯誤400:錯誤的請求”:

import urllib2
import simplejson

url = "https://api.twitter.com/1.1/search/tweets.json?screen_name=twitterapi"

if __name__ == "__main__":
    req = urllib2.Request(url)
    opener = urllib2.build_opener()
    f = opener.open(req)
    json = simplejson.load(f)

    for item in json:
        print item.get("created_at")  # this will get the section that is "Created At from JSON"
        print item.get('text')        # this will get the text (in Twitter, a Tweet)

我不確定為什么-我已經在Google周圍搜索了,似乎無法找到為什么這會返回Bad Request錯誤。 謝謝你的幫助!

您需要來自docs的API密鑰:

“請注意,現在API v1.1要求必須對請求進行身份驗證,請選中身份驗證和授權”

使用curl快速測試:

$curl https://api.twitter.com/1.1/search/tweets.json?q=%40twitterapi
{"errors":[{"message":"Bad Authentication data","code":215}]}

因此,首先,您應該獲得一個API Key 這是一些額外的幫助:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM