title: How to Create Tweet with OAuth 2.0 (Twitter API v2) using tweepy
body: <p>I try to create a tweet using tweepy under OAuth 2.0 instead of OAuth 1.0a. In other words, I am looking for an OAuth 2.0 equivalent of the following code.</p>
  <pre><code>import tweepy
  consumer_key = &quot;&quot;
  consumer_secret = &quot;&quot;
  access_token = &quot;&quot;
  access_token_secret = &quot;&quot;
  client = tweepy.Client(
      consumer_key=consumer_key, consumer_secret=consumer_secret,
      access_token=access_token, access_token_secret=access_token_secret
  )
  response = client.create_tweet(
      text=&quot;This Tweet was Tweeted using Tweepy and Twitter API v2!&quot;
  )
  print(f&quot;https://twitter.com/user/status/{response.data['id']}&quot;)
  </code></pre>
  <p>Following Twitter's guide on <a href="https://developer.twitter.com/en/docs/authentication/oauth-2-0/user-access-token" rel="nofollow noreferrer">OAuth 2.0 Making requests on behalf of users</a> and tweepy's guide on <a href="https://docs.tweepy.org/en/stable/authentication.html#oauth-2-0-authorization-code-flow-with-pkce-user-context" rel="nofollow noreferrer">OAuth 2.0 Authorization Code Flow with PKCE (User Context)</a>, I am able to obtain an access token, which is passed instead of the Bearer Token.</p>
  <pre><code>import tweepy
  
  client_id = &quot;&quot;
  redirect_uri = &quot;&quot;
  client_secret = &quot;&quot;
  
  oauth2_user_handler = tweepy.OAuth2UserHandler(
      client_id=client_id,
      redirect_uri=redirect_uri,
      scope=[&quot;tweet.read&quot;, &quot;tweet.write&quot;, &quot;users.read&quot;],
      client_secret=client_secret
  )
  
  print(oauth2_user_handler.get_authorization_url())
  
  authorization_response = input(&quot;--&gt; &quot;)
  
  access_token = oauth2_user_handler.fetch_token(
      authorization_response
  )
  
  
  client = tweepy.Client(access_token)
  </code></pre>
  <p>It seems that <a href="https://twittercommunity.com/t/which-api-to-get-user-after-oauth2-authorization/162417" rel="nofollow noreferrer">Twitter's implementation of OAuth 2.0 is in unfinished</a> as of Dec '21. However, in Feb '21, <a href="https://forum.bubble.io/t/login-with-twitter-and-post-tweet-on-behalf-of-user/74418/13" rel="nofollow noreferrer">the steps to post Tweets on behalf of users under OAuth 2.0</a> was described in another forum.</p>