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

add refresh_token function #1804

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/authentication.rst
Expand Up @@ -157,6 +157,13 @@ You can then pass the access token to :class:`Client` when initializing it::

client = tweepy.Client("Access Token here")

Tokens have a very short expiration date.
If you have a refresh token, you can renew it::

new_access_token = oauth2_user_handler.refresh_token(
"Refresh Token here"
)

3-legged OAuth
==============
This section supplements Twitter's `3-legged OAuth flow documentation`_.
Expand Down
32 changes: 32 additions & 0 deletions tweepy/auth.py
Expand Up @@ -226,3 +226,35 @@ def fetch_token(self, authorization_response):
include_client_id=True,
code_verifier=self.code_verifier
)

def refresh_token(
self,
token_url=None,
refresh_token=None,
body="",
auth=None,
timeout=None,
headers=None,
verify=True,
proxies=None,
**kwargs
):
if not token_url:
token_url = "https://api.twitter.com/2/oauth2/token"
if auth == None:
auth = self.auth
if body == "":
body = "client_id="+self.client_id

return super().refresh_token(
token_url,
refresh_token,
body,
auth,
timeout=None,
headers=None,
verify=True,
proxies=None,
**kwargs
)