6

I attempted to run the code below and am getting an error that states: HTTP Error code: 403: Forbidden: Authentication succeeded but account is not authorized to access this resource.

from searchtweets import ResultStream, gen_rule_payload, load_credentials, collect_results

import requests

premium_search_args = load_credentials("/home/dirname/twitter_keys.yaml",
                                       yaml_key="search_tweets_premium",
                                       env_overwrite=False)


rule = gen_rule_payload("basketball", results_per_call=100) # testing with a sandbox account
print(rule)

from searchtweets import collect_results

tweets = collect_results(rule, 
                         max_results=100, 
                         result_stream_args=premium_search_args)


# print(tweets.all_text)

[print(tweet.all_text, end='\n\n') for tweet in tweets[0:10]];

My YAML file looks like this:

search_tweets_premium:
  account_type: premium
  endpoint: https://api.twitter.com/1.1/tweets/search/fullarchive/dev.json
  consumer_key: AAAAAAAAAAAAAAAAAAAAA
  consumer_secret: BBBBBBBBBBBBBBBBBBBBBBBBBBB

Only other thing to note is that I am using the free/sandbox service.

Any ideas if I am doing anything wrong in the code, the YAML, and/or within my Twitter developer account?

user1624577
  • 547
  • 2
  • 6
  • 15
  • _I am using the free/sandbox service_ Then why are you trying to access a premium url? – John Gordon Mar 26 '19 at 03:41
  • Good catch @JohnGordon. I did not realize that. I tried changing it to a few different URLs that I found and so far same results. I do think you're probably on to the root of the problem though. – user1624577 Mar 26 '19 at 03:59
  • Sandbox provides access to the premium APIs, but at a lower volume and without some additional features such as counts. This should work, assuming that you have a developer environment configured that is named "dev" in the developer dashboard. – Andy Piper Mar 26 '19 at 20:18
  • @AndyPiper - I do have a developer dashboard but I do not know the name of it? Is that going to be the same as the application name? My previous experience was using Tweepy and it was so much simpler :/ – user1624577 Mar 26 '19 at 23:38

1 Answers1

10

You'll need to go to https://developer.twitter.com/en/account/environments

https://developer.twitter.com/en/account/environments

There you should be able to see the various development environments that you have. You can create one should they not have been created.

The dev environment label would then be the thing you use to replace in your endpoint.

In my example, it would be: https://api.twitter.com/1.1/tweets/search/fullarchive/development.json

If that still doesn't work, you might need to include a bearer token in your YAML file.

YuaNWA
  • 151
  • 2
  • 8
  • thanks it seems the name "development" is important. – bormat Mar 03 '20 at 10:54
  • To add to this, it seems that the dev environments are now not enabled by default. Going to https://developer.twitter.com/en/account/environments allows you to enable them and to choose the appropriate label. – Tom Sep 23 '20 at 15:50