I’m trying to register a twitter webhook on an API I create on AWS API Gateway. I was able to set up the CRC portion which provides the appropriate response through a browser however I’m unable to make post requests to the same endpoint. I’ve been following multiple guides on this including this and this
The code is below that is giving a 403 reponse:
from urllib.parse import quote_plus
from requests_oauthlib import OAuth1Session
import json
import tweepy
import urllib
CONSUMER_KEY = 'xxxxxxxxxxxxx'
CONSUMER_SECRET = 'xxxxxxxxxxxxx'
ACCESS_TOKEN = 'xxxxxxxxxxxxx'
ACCESS_SECRET = 'xxxxxxxxxxxxx'
twitter = OAuth1Session(CONSUMER_KEY,
client_secret=CONSUMER_SECRET,
resource_owner_key=ACCESS_TOKEN,
resource_owner_secret=ACCESS_SECRET)
webhook_endpoint = urllib.parse.quote_plus("https://#######.execute-api.us-east-2.amazonaws.com/dev/webhook")
url = 'https://api.twitter.com/1.1/account_activity/all/:env_name/'
def register_webhook(url,twitter,webhook_endpoint):
url+=f'webhooks.json?url={webhook_endpoint}'
response = twitter.post(url)
print(response)
def subscribe_to_user_activity(url,twitter):
url+=f'subscriptions.json'
response = twitter.post(url)
print(response)
if __name__ == '__main__':
register_webhook(url,twitter,webhook_endpoint)
subscribe_to_user_activity(url,twitter)
A couple of things I checked:
I can verify that the account is approved for account activity API, I created and counter checked the dev environment label, Keys were regenerated twice, Testing on Postman as advised here is giving a 200 forbidden response and doesn’t work
In postman I selected Oauth1.0 as the auth type and filled in Consumer Key, Consumer Secret, Access Token, Token Secret values, and provided the url endpoint in the body. The POST request being sent to https://api.twitter.com/1.1/account_activity/all/:my_env_name/webhooks.json?
On AWS side, the POST method was created on the resource with all default values
Any idea where I could be going wrong? Or a fool proof method of registering a twitter webhook with AWS API gateway as the listener?
1 Like
The : should not be in the url just in case that is not a placeholder in your example. It should be the actual environment name.
Does the code work on your local machine? If you use ngrok.com or something like that? Maybe there’s something else wrong? Might be worth eliminating AWS stuff entirely just to make sure there are no other errors.
Yeah the :env_name is just a placeholder I used my actual environment label without the “:”
Haven’t looked into ngrok just yet but I’ll try set up an environment to test this, I was under the impression I’d need substantial JS knowledge for that but doesn’t appear so.
1 Like
Exactly the same problem. Twitter docs are very confusing and incomplete. Amatorial level. Did you find a solution?