im creating a basic webapp with tweepy & having a hard-time architecting the oauth2 user context logic to my code.

im able to print and check the auth_url which when i paste in my browser, directs me as a user to authorize the app on twitter (but I get stuck on the redirect uri and in general get the error logged below):

Codebase:

import tweepy
import config


oauth2_user_handler = tweepy.OAuth2UserHandler(
    client_id="",
    redirect_uri="http://127.0.0.1:5000",
    scope=["tweet.write"],
    client_secret=""
)

auth_url = oauth2_user_handler.get_authorization_url()

access_token = oauth2_user_handler.fetch_token(f"{auth_url}")

client = tweepy.Client(f"{auth_url}")

The error message im getting:

"""
in parse_authorization_code_response
    raise MissingCodeError("Missing code parameter in response.")
oauthlib.oauth2.rfc6749.errors.MissingCodeError: (missing_code) Missing code parameter in response.
"""

A few side notes I have:

  1. What website URL do i put in my app settings? (do I need to spin up a server and get the URL from that dashboard?)
  2. How is the website URL related to the callback URI and how do they differ?

this is my first time working with authentication & Twitter api so I appreciate the patience with my newbie questions :slight_smile:

@notpickard hi ser, idk if u remember but u were helping me with ouah2 --i went offline for a bit and the thread has closed so i made a new, updated, version.

thanks for any help with this, been stuck here for a month now lol

Hope you’re enjoying the year and safe homie

Hi @im_rickjamez,

You can update your callback URL in the auth settings section of your App’s setting. For OAuth 2.0, your callback and redirect uri need to be the same.

I haven’t gotten localhost to work, but I have used ngrok in the past or example.com. Also, you will need several scopes, not just tweet.write for this to work. You can find the full list of scopes per endpoint here.

I also got a similar code sample working, so here is my code. Note, mine uses a public client and doesn’t require a client secret.

import tweepy
import requests

oauth2_user_handler = tweepy.OAuth2UserHandler(
    client_id="your-client-id",
    # Replace the following URL with your callback URL, which can be obtained from your App's auth settings.
    redirect_uri="https://www.example.com",
    scope=["users.read", "tweet.read", "offline.access", "bookmark.read"]
)

# Visit the URL to authorize your App to make requests on behalf of a user
print(oauth2_user_handler.get_authorization_url())

full_url =  input("Paste in the full URL after you authorized your App: ")
access_token = oauth2_user_handler.fetch_token(full_url)
access = access_token['access_token']

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

Hope this helps!

2 Likes

firstly, ty Jessica, very easy to read :slight_smile:

unfortunately, im still getting:

MissingCodeError("Missing code parameter in response.")

I imagine this is the line where the error is coming from:
access_token = oauth2_user_handler.fetch_token(full_url)

here’s my full codebase I got from you (without personal info), i changed my redirect uri in app settings to example.com as well:

import tweepy
import config
import requests


oauth2_user_handler = tweepy.OAuth2UserHandler(
    client_id="",
    redirect_uri="https://example.com",
    scope=["tweet.write","users.read", "tweet.read", "offline.access", "bookmark.read"],
    # Client Secret is only necessary if using a confidential client
    client_secret=""
)

# getting my aps auth url 
print(oauth2_user_handler.get_authorization_url())
full_url = access_token = input("Paste in the full URL after you authorized your App: ")

access_token = oauth2_user_handler.fetch_token(full_url)
access = access_token['access_token']


user_me = requests.request("GET", "https://api.twitter.com/2/users/me", headers={'Authorization': 'Bearer {}'.format(access)}).json()
# client = tweepy.Client(access)

I must be making a very basic error since its the exact same code and working for you, so I appreciate the help!

thanks,

rick

Do you have an authorize URL, I can look at? Let me know if you rather I send you a DM to get this information from you.

ya i just dm’d it to you on twitter

1 Like

let me know if u need anything else, ty :slight_smile:

Thanks, @im_rickjamez, I’m going to reach out to some coworkers on this one.

I heard that this is resolved via a conversation @alanbenlee, let me know if you are still having issues here.

I am having this same exact issue and cannot find a solution anywhere. I have used up all my resources. Any help would be great!

yes theve been great, heres an update on where im stuck conceptually:

https://twittercommunity.com/t/re-oauth2-user-context-missing-code-understanding-callback-uri/169088/9

ty for the help jessica!

:slight_smile: