Search code examples
pythontwitterkeyerror

key error in searchtweets API


I am new to searchtweets API and am getting a key error while executing the code as follows.

My Code:

from searchtweets import collect_results,ResultStream, gen_rule_payload, 
load_credentials
premium_search_args = load_credentials("E:\\residency_5\\practicum\\twitter_keys.yaml",yaml_key="search_tweets_premium",env_overwrite=False)

My Yaml file:

search_tweets_api:
  account_type: premium
  endpoint: https://api.twitter.com/1.1/tweets/search/30day/dev.json
  consumer_key: wgjwneglwegkweglkew
  consumer_secret: rglknrgkrwrerherhrehreh

The error I am getting is:

KeyError                                  Traceback (most recent call last)
<ipython-input-18-a65d7e772e67> in <module>()
----> 1 premium_search_args = load_credentials("E:\\residency_5\\practicum\\twitter_keys.yaml",yaml_key="search_tweets_premium",env_overwrite=False)

C:\Users\Srivatsav\Anaconda3\lib\site-packages\searchtweets\credentials.py in load_credentials(filename, account_type, yaml_key, env_overwrite)
    184                    if env_overwrite
    185                    else merge_dicts(env_vars, yaml_vars))
--> 186     parsed_vars = _parse_credentials(merged_vars, account_type=account_type)
    187     return parsed_vars
    188 

C:\Users\Srivatsav\Anaconda3\lib\site-packages\searchtweets\credentials.py in _parse_credentials(search_creds, account_type)
     80         """
     81         logger.error(msg)
---> 82         raise KeyError
     83 
     84     try:

KeyError: 

How can I fix this. Is there something wrong with my yaml file? Thanks in advance


Solution

  • The reason why you have a KeyError is because the key you wanted to find in the yaml file doesnot exist with the same name you have mentioned in your python code. You are requesting the yaml_key="search_tweets_premium" which means the yaml file has a key with the name search_tweets_premium but instead this key is not found in the yaml file.

    The key in your yaml file is search_tweets_api instead of search_tweets_premium. Either correct the key in the yaml file or the python code such that they're both the same and your code should work fine.