Refresh Token for OAuth 2.0 PKCE #1912
Unanswered
markusspitzli
asked this question in
Questions
Replies: 1 comment 2 replies
|
Because PR #1806 is not being merged for a very long time, you can implement the following workaround in your code to avoid forking whole tweepy and implement the PR in your fork:
import tweepy
class MyOAuth2UserHandler(tweepy.OAuth2UserHandler):
# Kudos https://github.com/tweepy/tweepy/pull/1806
def refresh_token(self, refresh_token):
new_token = super().refresh_token(
"https://api.twitter.com/2/oauth2/token",
refresh_token=refresh_token,
body=f"grant_type=refresh_token&client_id={self.client_id}",
)
return new_token
# ... get old_token from your local storage ...
auth = MyOAuth2UserHandler(
client_id=MYCLIENTID, # same credentials as used before
client_secret= MYCLIENTSECRET,
redirect_uri=MYURL,
scope=[..., "offline.access"],
)
new_token = auth.refresh_token(old_token["refresh_token"])
# ... replace old_token by new_token in your local storage ...
Worked fine for me. Alternative approach would be to nudge the maintainers to merge the PR 😇 |
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
hi
I'm using the OAuth2UserHandler for getting the token. It works fine for 2 hours, but then I have to manually approve my app again. I rather want that my script refreshes the token automatically. How would you achieve that?
My code so far:
All reactions