Twitter Oauth 2.0 making requests on behalf of other users

Uce*_*llo 6 twitter oauth-2.0

I am trying to use the PKCE flow for the user authentication.

After successful redirection by using the following pattern:

https://twitter.com/i/oauth2/authorize?response_type=code&client_id=M1M5R3BMVy13QmpScXkzTUt5OE46MTpjaQ&redirect_uri=https://www.example.com&scope=tweet.read%20users.read%20follows.read%20offline.access&state=state&code_challenge=challenge&code_challenge_method=plain

I got the code, but then when I try to get the access token the following request fails:

curl --location --request POST 'https://api.twitter.com/2/oauth2/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Authorization: Basic <base 64 encode>' \
--data-urlencode 'code=<code received>' \
--data-urlencode 'grant_type=authorization_code' \
--data-urlencode 'redirect_uri=<my_redirect>' \
--data-urlencode 'code_verifier=challenge' \
--data-urlencode 'client_id=<my_client_id>'
Run Code Online (Sandbox Code Playgroud)

Error message is {"error":"unauthorized_client","error_description":"Missing valid authorization header"}

I tried to use all of the methods described here for the Basic header, but none of them were successful:

https://twittercommunity.com/t/how-to-generate-a-bear-token-for-api-2-authentication/171837

Thanks!

小智 11

在您的应用程序 > 用户身份验证设置 > 单击 OAuth 1.0a 和 OAuth 2.0 的编辑 > 打开 OAuth 1.0a 和 OAuth 2.0 并将应用程序类型更改为“单页应用程序”,该应用程序应该是公共客户端。