Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Non-200 response code #1

Open
FabioRosado opened this issue Dec 4, 2020 · 5 comments
Open

Non-200 response code #1

FabioRosado opened this issue Dec 4, 2020 · 5 comments

Comments

@FabioRosado
Copy link

Hello Simon,

thanks for creating this, I've been around twitter API docs and other libraries to try and get the webhooks working for ages now...

I've copied your code to test it out, although I keep getting this error:

 [{'code': 214, 'message': 'Non-200 response code during CRC GET request (i.e. 404, 500, etc).'}]

I'm passing my URL and env as expected. I've also checked that the endpoint is working and all seems good. Just wanted to check with you if you have any idea on how I could try to debug this? At the moment I'm out of ideas haha

Thank you

@bpluly
Copy link
Owner

bpluly commented Dec 5, 2020

Hi Fabio, 214 is a Host key error, so is there something you're doing with the request key that could mangle it in some way?

I'll post an example from one of my implementations, which I've meant to do for a long time.

@FabioRosado
Copy link
Author

I'm thinking that this is either a issue with using tweepy with an aiohttp web server or something that tweepy is doing 🤔

I'm using ngrok to forward my traffic on port 8080 - where the server is running.

I'm setting the route as mentioned in aiohttp: web_app.router.add_get(endpoint, handler)

My get handler looks like this (I've changed it around so many times tho, I've used the twitter example, your example and a misture in between)

    async def handle_get(self, request):
        """Handle Twitter get request."""
        crc_token = request.rel_url.query.get('crc_token')
        sha256_hash_digest = hmac.new(self.consumer_secret.encode(), msg=crc_token.encode(), digestmod=hashlib.sha256).digest()

        _LOGGER.info(sha256_hash_digest)
        # construct response data with base64 encoded hash
        response = {
            'response_token': 'sha256=' + base64.b64encode(sha256_hash_digest).decode('utf-8')
        }

        _LOGGER.info(response)

        return aiohttp.web.Response(status=200, body=json.dumps(response), headers={'Content-Type': 'application/json'})

Then I am setting tweepy and your code like you wrote on the docs (on that PR that isn't merged yet)

    async def listen(self):
        """Listen is handled by webhook."""
        #self.base_url == ngrok URL
        # self.name == twitter (or other thing set in config)
        opsdroid_url = f"{self.base_url}/connector/{self.name}"

        auth = tweepy.OAuthHandler(self.consumer_key, self.consumer_secret)
        auth.set_access_token(self.access_token, self.access_token_secret)

        api = ActivityAPI(auth)

        #Try/except was just so the server wouldn't crash due to unhandled tweepy exception
        try: 
            activityResponse = api.enable_activity(url=opsdroid_url, env=self.env)
        except Exception as e:
            _LOGGER.info(e)

The weird thing is, when I run the server, ngrok shows a 502 error straight away before we get the error that I mentioned: 214.. but if I log anything on my get handler, nothing is logged in the terminal so it seems that the get request might have been handled by tweepy? Or you know, that's just an async issue and I need to figure out how to do this in a different way haha

@bpluly
Copy link
Owner

bpluly commented Dec 5, 2020

I haven't used aiohttp I'll take a look tomorrow but the 502 is a clue I think. It suggests there's a failure in proxying, tweepy might be treating that as a key failure.

S

@bpluly
Copy link
Owner

bpluly commented Dec 7, 2020

The interfaces you've published to twitter are all to port 8080, so they're not https but http, or are they https? Either way you're listening on whatever local port using aiohttp. The 502 is reported by the ngrok tunnel to the twitter API, is it the initial leg of the 3 legged dance? Or the returned access code, or the validation step?

@bpluly
Copy link
Owner

bpluly commented Dec 7, 2020

Is it only the AccountActivity call that fails? Does the initial authorise flow work?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants