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 outside the praw #2021

Open
HT-Moh opened this issue May 5, 2024 · 5 comments
Open

oauth2 outside the praw #2021

HT-Moh opened this issue May 5, 2024 · 5 comments

Comments

@HT-Moh
Copy link

HT-Moh commented May 5, 2024

Describe the solution you'd like

When oauth2 is done outside the praw, we should be able to instantiate praw.Reddit only with access token, I am not sure if this is possible now, I can not find anything in the doc. the link Using a Saved Refresh Token it's not exactly the same thing.

For the moment I just went with raw implementation

headers = {
      'Authorization': f'Bearer {self.token}',
    }
    params = {'q': query}
    url = 'https://oauth.reddit.com/search'

    response = requests.get(url, headers=headers, params=params)
    response_data = response.json()

Describe alternatives you've considered

No response

Additional context

No response

@LilSpazJoekp
Copy link
Member

Why are you handling oauth2 outside of PRAW?

As for instantiating PRAW with just a access token, see here: #2012 (comment)

@HT-Moh
Copy link
Author

HT-Moh commented May 5, 2024

We need users to log in and connect their Reddit accounts using their access tokens. It's not ideal for each user to generate a client ID and secret, beside not all users are techy :-) . This is a common use case for OAuth2 authentication.

@LilSpazJoekp
Copy link
Member

LilSpazJoekp commented May 6, 2024

PRAW supports this oauth 2 flow already. See here for more details on how this works.

@HT-Moh
Copy link
Author

HT-Moh commented May 9, 2024

Thanks, but still not clear, let me clarify my setup:

I use a Reddit AI tool designed to search posts for AI agents.
I store the token and refresh token in a database. Users authorize via OAuth2 with a permanent token, which is then stored in the database.
We aim to decouple the Reddit AI tool from the ORM. We plan to pass only the token and refresh token as arguments to the tool. Praw's sole task would be to execute the necessary queries on Reddit.

Note: The AI operations are managed through a job that executes at regular intervals.

Your example didn't illustrate how to pass the refresh token or token to the praw instance.

@LilSpazJoekp
Copy link
Member

The usage of the refresh token is documented here.

PRAW will automatically fetch a new access token with the refresh token right before the first network request is made.

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

2 participants