0

I am having an issue using the Twitter V2 API with Tweepy. I have created code to refresh the Access_token once I have carried out the initial auth for the app whilst logged in.

I need to use OAuth 2.0 Authorization for Twitter an have even paid the $100 to access the API

I am trying to just respond to tweet mentions. I've been trying to figure this out for days now. I can read tweets using one of the collection of auth options I've tried. but I just cant seem to get the right combination to read and write with the V2 API.

        if self.tokens is not None and 'access_token' in self.tokens:
            self.access_token = self.tokens['access_token']
            logging.info("Tokens found in init section twitter file")
            logger.info(f"Access token in init is : {self.access_token}")
            #auth = OAuth2Session(client_id, redirect_uri=redirect_uri, state=state, scope=scopes)
            #auth = tweepy.OAuth2UserHandler(client_id, client_secret, redirect_uri, scope=scopes)
            #auth = tweepy.OAuth2BearerHandler(self.access_token)
            #self.api = tweepy.API(auth)  not too sure which ones to use 
            #auth = tweepy.OAuth2BearerHandler(self.access_token)
            #self.api = tweepy.OAuth2BearerHandler(access_token=self.access_token)
            #self.api = tweepy.Client(bearer_token=self.access_token)
            # set up your authentication credentials
            # set up your authentication credentials
            #self.auth = tweepy.OAuth2BearerHandler(self.access_token)
            #self.api = tweepy.Client(self.auth)
            self.api = tweepy.Client(self.access_token)

Further down the code 

     self.api.create_tweet(text=f"@{author_username} {response_text}", in_reply_to_tweet_id=mention.id)

So I'm trying to get the right combination here. I obviously have my access_token to use. Getting this working was bad enough on a refresh schedule. Any help would be most appreciated. Its all quite new so I can't really find too much info on this.

Twitter callback

@app.route("/oauth/callback", methods=["GET"])
@cross_origin()
def callback():
    # Recover the state from the session
    state = session.get("oauth_state")
    code = request.args.get("code")

    # Create a new OAuth2Session object for this route
    twitter = OAuth2Session(client_id, redirect_uri=redirect_uri, state=state, scope=scopes)
                            
    token = twitter.fetch_token(
        token_url=token_url,
        client_secret=client_secret,
        code=code,
    )

I have seen somewhere that I might need to create a url combination like this

    user_me = requests.request(
        "GET",
        "https://api.twitter.com/2/users/me",
        headers={"Authorization": "Bearer {}".format(token["access_token"])},
    ).json()

But Im trying to use tweepy. Happy to share my refresh token code if needed. Hope someone can help before all my har falls out. Thanks

Tried everything, when using Client

I get the the error - so it seems its using the OAuth1 not OAuth2 even though it states here to use Client https://docs.tweepy.org/en/stable/authentication.html?#twitter-api-v2

ERROR:root:Error while trying to reply to tweet ID 1660294975718932480. 2023-05-30T21:25:32.965965+00:00 app[web.1]: Traceback (most recent call last): 2023-05-30T21:25:32.965966+00:00 app[web.1]: File "/app/twitter.py", line 243, in respond_to_mentions 2023-05-30T21:25:32.965966+00:00 app[web.1]: self.api.create_tweet(text=f"@{author_username} {response_text}", in_reply_to_tweet_id=mention.id) 2023-05-30T21:25:32.965966+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/tweepy/client.py", line 835, in create_tweet 2023-05-30T21:25:32.965967+00:00 app[web.1]: return self._make_request( 2023-05-30T21:25:32.965967+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^ 2023-05-30T21:25:32.965967+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/tweepy/client.py", line 129, in _make_request 2023-05-30T21:25:32.965968+00:00 app[web.1]: response = self.request(method, route, params=request_params, 2023-05-30T21:25:32.965968+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2023-05-30T21:25:32.965969+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.11/site-packages/tweepy/client.py", line 69, in request 2023-05-30T21:25:32.965969+00:00 app[web.1]: auth = OAuth1UserHandler(

0 Answers0