Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oauth2 fails to authenticate #2021

Open
javinievas opened this issue Nov 20, 2022 · 5 comments
Open

Oauth2 fails to authenticate #2021

javinievas opened this issue Nov 20, 2022 · 5 comments

Comments

@javinievas
Copy link

Hi,

I have the same issue that was mentioned in issue #2014. It was closed but was not solved.

After granting the app the permissions, the user is redirected to the callback url with two parameters: state and code.
When passing the full URL to the fetch_token method:

fetch_token("https://mydomain.tld/mycallbackuri/?state=CfVn3zt150Z673dUu8rKLDYvX&code=QlR2VjFxVTYXXXXXXXXXXXXXXXXXMTW5rc3VXXXXXXXXXXxExLOjE2Njg5ODI3MDUyNTI6MTowOmFjOjE")

This error is returned:
InvalidClientIdError: (invalid_request) Missing required parameter [code_verifier]

I was digging a bit and fount this specific line in the stacktrace:

File XXXXXXX/site-packages/tweepy/auth.py:217, in OAuth2UserHandler.fetch_token(self, authorization_response)
return super().fetch_token(
218 "https://api.twitter.com/2/oauth2/token",
219 authorization_response=authorization_response,
220 auth=self.auth,
221 include_client_id=True,
222 code_verifier=self._client.code_verifier
223 )

I can see that self._client.code_verifier is passed as code_verifier, but if I check the actual value of this variable is empty.

Can't see what am I doing wrong? Or if this is an issue?

@javinievas
Copy link
Author

I've just found the way to workaround this.

Before performing the redirect to the authorization uri, I store the ._client.code_verifier on the browser session, so, when the callback uri is called I get this value and set it again on its place before calling fetch_token.

I hope there is a better a way, this solution looks a bit ugly to me.

I had to do something like this:
oauth2_user_handler = self._get_oauth2_user_handler() // to create an instance of OAuth2UserHandler
oauth2_user_handler._client.code_verifier = request.session["code_verifier"] // Here I set this value manually from session
access_token = oauth2_user_handler.fetch_token(state) // And this way the fetch_token call gets to work.

@lazmond3
Copy link

lazmond3 commented Dec 4, 2022

My solution:

→ select "Native App"

image

@arthuruwalaka
Copy link

Hey, I have the same issue. Is "code_verifier in "oauth2_user_handler._client.code_verifier = request.session["code_verifier"]" supposed to be the code parameter in the callback url?

@canache
Copy link

canache commented Dec 14, 2022

My solution:

→ select "Native App"

image

Doesn't work !

@IParraMartin
Copy link

Maybe make sure you got the right Twitter dev access? I remember having a similar issue when I didn't have the research access.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants