1

我正在尝试使用searchtweets API 检索历史数据,但我不断收到 twitter 422 错误。奇怪的是,在这段代码之前,我有一个以前的代码,它运行得很好。但在此之后,旧代码也不起作用

我遵循了文档,仔细检查了我是否拥有完整的存档访问权限,甚至重新生成了我的令牌和访问密钥。

这是我的python代码:

import json
import csv

from searchtweets import collect_results, load_credentials, gen_rule_payload

enterprise_search_args = load_credentials("twitter_keys.yaml", yaml_key = "search_tweets_api", env_overwrite = False)

from_date = "2016-06-01"
to_date = "2016-06-23"

with open('map_data/london_borough.csv', 'r', newline = "") as csvFile:
    boroughs = list(csv.reader(csvFile, delimiter = ','))
    for num, borough in enumerate(boroughs):
        if num:
            name = boroughs[0]
            box_coord = borough[5]
            if box_coord:
                box_coord = '['+box_coord.replace(',',' ')+']'
                print(box_coord)
                rule = gen_rule_payload("brexit", results_per_call = 100, from_date = from_date, to_date = to_date)
                rule = json.loads(rule)
                rule['bounding_box'] = box_coord
                rule = json.dumps(rule)
                print(rule)
                tweets = collect_results(rule, max_results = 500, result_stream_args = enterprise_search_args)
                with open('%s.csv'%(name), 'w', newline = "") as writeFile:
                    for tweet in tweets:
                        writeFile.write('%s, %s, %s\n'%(tweet.name, tweet.created_at_string, tweet.all_text))

还有我的 yaml 文件:

search_tweets_api:
account_type: premium
endpoint: https://api.twitter.com/1.1/tweets/search/fullarchive/StreamingTweets.json
consumer_key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
consumer_secret: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

运行上面的代码后,我在控制台上得到了这个:

Grabbing bearer token from OAUTH
[-0.105400 51.507354 -0.074673 51.521028]
{"query": "brexit", "maxResults": 100, "toDate": "201606230000", "fromDate": "201606010000", "bounding_box": "[-0.105400 51.507354 -0.074673 51.521028]"}
retrying request; current status code: 422
retrying request; current status code: 422

我的计算机时钟设置为 UTC 协调世界时间,我已经四次检查了我的不记名令牌并重新生成了几次,应用程序访问设置为读取、写入和访问直接消息,但仍然不知道如何解决这个问题。

有谁知道解决这个问题或我可能会出错的地方?任何帮助将不胜感激!

4

0 回答 0