geduldig / TwitterAPI

Minimal python wrapper for Twitter's REST and Streaming APIs
936 stars 263 forks source link

Authenticating error (api_version='2') #226

Closed 965realestate closed 1 year ago

965realestate commented 1 year ago

Is there a problem with authentication requests or I'm doing something wrong?

I tried the Twitters API v2 postman with (OAuth 1.0) Consumer Key, Consumer Secret, Access Token, Token Secret after that creating a tweet with Twitters API v2 it worked perfectly.

code:

# Consumer Keys
consumer_key = ??'
consumer_secret = '??'

# Access Token and Secret
access_token_key = '??'
access_token_secret = '??'

from TwitterAPI import TwitterAPI
api = TwitterAPI(consumer_key, consumer_secret, access_token_key, access_token_secret, api_version='2')

api.request('tweets', {'text': 'testing api'})

response:

Request returned an error: 403 {"client_id":"26924600","detail":"When authenticating requests to the Twitter API v2 endpoints, you must use keys and tokens from a Twitter developer App that is attached to a Project. You can create a project via the developer portal.","registration_url":"https://developer.twitter.com/en/docs/projects/overview","title":"Client Forbidden","required_enrollment":"Appropriate Level of API Access","reason":"client-not-enrolled","type":"https://api.twitter.com/2/problems/client-forbidden"}

geduldig commented 1 year ago

Looks good to me. Are you using the same credentials with Postman and TwitterAPI?

I would add something to your code to confirm that your credentials are valid.

965realestate commented 1 year ago

Yes, I'm using the same exact credentials with Postman and TwitterAPI.

What code do I need to add that check my credentials are valid?

geduldig commented 1 year ago

You could try other enpoints, such as searching for tweets, that don't require write privileges. I'm really not sure what to tell you. It doesn't make sense that the same credentials would would work in one case and not in the other. Are those credentials associated with a Twitter developer project?

geduldig commented 1 year ago

The error you are receiving is from Twitter, so it's impossible to for me to look into the cause. There must be some difference between your requests in Postman and in TwitterAPI. It might be helpful to try the same request using another library such as twarc.

geduldig commented 1 year ago

I see one thing you are doing wrong. The 'tweets' request, by default uses HTTP GET. You need to use HTTP POST in order to upload a tweet. Try this... r = api.request('tweets', {'text':'a test to test'}, method_override='POST')

965realestate commented 1 year ago

I see one thing you are doing wrong. The 'tweets' request, by default uses HTTP GET. You need to use HTTP POST in order to upload a tweet. Try this... r = api.request('tweets', {'text':'a test to test'}, method_override='POST')

method_override='POST'

Thank you! Problem fixed

geduldig commented 1 year ago

@qkxie How are you creating your api object?

On Wed, Aug 2, 2023 at 10:18 PM qkxie @.***> wrote:

I have the same error. even when I set method to post:

resp = api.request('tweets', {'text': 'testing api'}, method_override='POST')

It still return error message:

'{"title":"Forbidden","status":403,"detail":"Your client app is not configured with the appropriate oauth1 app permissions for this endpoint.","type":"https://api.twitter.com/2/problems/oauth1-permissions"}'

— Reply to this email directly, view it on GitHub https://github.com/geduldig/TwitterAPI/issues/226#issuecomment-1663198635, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAWUOP6WPCPTSAYKERPHEALXTMC55ANCNFSM6AAAAAAXBERWKA . You are receiving this because you modified the open/close state.Message ID: @.***>

qkxie commented 1 year ago

@geduldig

api = TwitterAPI(consumer_key, consumer_secret, access_key, access_secret, api_version='2')