We have used python language for integration, and we used Rest API for sending a request for authentication.
For that we have used the python packages requests to send a POST, GET request.
Please check the following operation I did for authentication using OAuth2.
Step 1: Authorize
We refer to Authorization Code Flow with PKCE.
First, we redirect to this URL = “i/oauth2/authorize?response_type=code&client_id=UXo4RFF1ZzNtYUJqaWdjREtRTjQ6MTpjaQ&redirect_uri=/twitter_oauth%2Fauthorize&scope=users.read&state=sXPDZqSiOnTFB1kwmcwNdRwPoSLRth&code_challenge=challenge&code_challenge_method=plain”
Then open one login page of twitter.
After add credentials of user, they redirect to authorize user page. (Please see attached image.)
Then hit on Authorize App Button we got response from Twitter API as below.
{‘state’: ‘sXPDZqSiOnTFB1kwmcwNdRwPoSLRth’, ‘code’: ‘Sk5odzUzN0xZZXM3YTA1NnpxbWo4S2h0N2hZVmJtQzQ0LXdWUW1GVlhxbHo1OjE2NzQ3OTY2NjQxODQ6MToxOmFjOjE’}
Step 2: Get Access Token
We have sent request to this URL “/2/oauth2/token” for get access token using code which I got in step1 response.
Please check below request and response code.
import requests
import json
headers = {‘Content-Type’: ‘application/x-www-form-urlencoded’}
params = {
‘code’: ‘Sk5odzUzN0xZZXM3YTA1NnpxbWo4S2h0N2hZVmJtQzQ0LXdWUW1GVlhxbHo1OjE2NzQ3OTY2NjQxODQ6MToxOmFjOjE’,
‘grant_type’: ‘authorization_code’,
‘client_id’: ‘UXo4RFF1ZzNtYUJqaWdjREtRTjQ6MTpjaQ’,
‘client_secret’: ‘ns_dz4hOw6DLLXkYc7CcpbRxwDERHaX6DgNCicfqkAu9pws7Dx’,
‘redirect_uri’: ‘/twitter_oauth/authorize’, ‘code_verifier’: ‘challenge’}
url = ‘’/2/oauth2/token"
token_request = requests.post(url=url, params=params, headers=headers)
Or
I have also set params in body instead of params parameter but also getting same issue.
token_request = requests.post(url=url, body=json.dumps(params), headers=headers)
response = token_request.json()
We’re getting response as below:
response = {“error”:“unauthorized_client”,“error_description”:“Missing valid authorization header”}
Please Note- We removed domain from all link due to constrains.
Please check above steps and let me know.
Thanks,
Smith