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

Conversation

yamahubuki
Copy link

I added refresh_token function.

It need to make an application that Operate for a long time.

Copy link

@ewtoombs ewtoombs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OAuth2Session.refresh_token() has a different signature from what you're using. I'm pretty sure this is going to break OAuth2Session's ability to do automatic token refresh.

tweepy/auth.py Outdated
@@ -226,3 +226,12 @@ def fetch_token(self, authorization_response):
include_client_id=True,
code_verifier=self.code_verifier
)

def refresh_token(self, refresh_token):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are changing the signature of a method that is also used internally. I can't imagine this going over well.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ewtoombs Thank you for noticing.
I fixed the problem.

@ewtoombs
Copy link

ewtoombs commented Jan 27, 2023

I have also been working on token refresh. My solution (https://github.com/tweepy/tweepy/pull/2039/files) uses requests-oauthlib's built-in support for token autorefresh. It works well, but is probably incompatible with your work, and I'm not sure how to resolve this. Could you have a look?

@yamahubuki
Copy link
Author

@ewtoombs Your auto-refresh solution is great! But manually refresh is needed too(ex: save token file when refresh).

I think my code has resolved the conflict and is compatible with your solution, but am I missing something?

@ewtoombs
Copy link

ewtoombs commented Jan 29, 2023

@ewtoombs Your auto-refresh solution is great! But manually refresh is needed too(ex: save token file when refresh).

My solution actually does support saving the token to a file on refresh. See the new token_updater= argument to OAuth2UserHandler's constructor that I added, but basically, you pass a function that OAuth2UserHandler will run every time the token is refreshed. I also added this info to the documentation. Would that solve your use case?

I think my code has resolved the conflict and is compatible with your solution, but am I missing something?

I didn't provide a way to turn off token autorefresh. If manual refresh is really necessary, I'd have to think of a way to do this. At that point, your change should be compatible with mine. But I am really stuggling to think of a case where manual refresh would be necessary.

@yamahubuki
Copy link
Author

@ewtoombs

But I am really stuggling to think of a case where manual refresh would be necessary.

manually refresh is needed.

For example, one token is used by two or more servers.
If one server does an automatic refresh and writes it to the database before the other server does an refresh and saves it to the database first, the token will be corrupted.

@ewtoombs
Copy link

For example, one token is used by two or more servers. If one server does an automatic refresh and writes it to the database before the other server does an refresh and saves it to the database first, the token will be corrupted.

Ooh. OK, this is getting more complicated. I don't understand this example, but I'm just going to take your word for it. I'll implement a boolean argument to the constructor that allows autorefresh to be disabled.

There may be other concerns about compatibility between our modifications, though. I passed the twitter-specific modifications in a different way from how you did it. It is possible that your manual refresh function, refresh_token, could be made simpler as a result, if my modifications help it work. Or it could be that refresh_token is the best place for the modifications, in which case, I'll need to get rid of the modifications where I made them.

To be continued.

@ewtoombs
Copy link

Sorry, but I've run out of time for this, @yamahubuki .

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

Successfully merging this pull request may close these issues.

None yet

3 participants