twitterwebhookstwitter-oauthtwitter4j

Can not register twitter webhook. {“errors”:[{“code”:32,”message”:”Could not authenticate you.”}]}


I am trying to register a endpoint for twitter webhook but it returns {“errors”:[{“code”:32,“message”:“Could not authenticate you.”}]}

I am using twitter sample request to register webhook. Here is my curl request, curl request to register url

curl --request POST --url "https://api.twitter.com/1.1/account_activity/all/:ENV_NAME/webhooks.json?https%3A%2F%2Fd216d0d30d8f.ngrok.io%2Fapi%2F1.0%2Fwebhooks%2Ftwitter" --header "authorization: OAuth oauth_consumer_key="CONSUMER_KEY", oauth_nonce="EhOhXekPMpf", oauth_signature="GvOx3V91BrfWuBuPKUuftYgRgHY%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1597823037", oauth_token="ACCESS_TOKEN", oauth_version="1.0""

My endpoint is running on https using ngrok tunnel endpoint running on https using ngrok

I have got all requirements from twitter:

I have tried request using angular and https mydomain.

//function to register twitter webhook 
registerTwitterWebhook() {

    // authorization in headers
    const headers = new HttpHeaders({
      'authorization': 'OAuth oauth_consumer_key="CONSUMER_KEY",oauth_nonce="UAkSZPWDfmM",oauth_signature="%2FWSSbq4R1eTbGF4X9xg1T1e9sY4%3D",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1597728768",oauth_token="ACCESS_TOKEN",oauth_version="1.0"',
      'Content-Type':  'application/x-www-form-urlencoded'
       })
    const url = 'https://api.twitter.com/1.1/account_activity/all/:ENV_NAME/webhooks.json?url="https%3A%2F%mydomain.com%2Fapi%2F1.0%2Fwebhooks%2Ftwitter"';
    return this.http.post(url, {}, { headers: headers });
}

On sending POST request it shows error as follows Error chrome web console

Kindly guide me with the possible solution for registering twitter webhook.

Thank you


Solution

  • The code you’re trying to run from the browser will not work, because Twitter does not support CORS (that’s what the error message is telling you). The curl command needs to use form data for the URL submission per this answer