-
-
Notifications
You must be signed in to change notification settings - Fork 466
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1687 from praw-dev/callbacks
Support single-use refresh tokens
- Loading branch information
Showing
19 changed files
with
501 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Token Manager | ||
============= | ||
|
||
.. automodule:: praw.util.token_manager | ||
:inherited-members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#!/usr/bin/env python3 | ||
from urllib.parse import quote_plus | ||
|
||
import praw | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#!/usr/bin/env python3 | ||
"""This example demonstrates using the file token manager for refresh tokens. | ||
In order to run this program, you will first need to obtain a valid refresh token. You | ||
can use the `obtain_refresh_token.py` example to help. | ||
In this example, refresh tokens will be saved into a file `refresh_token.txt` relative | ||
to your current working directory. If your current working directory is under version | ||
control it is strongly encouraged you add `refresh_token.txt` to the version control | ||
ignore list. | ||
Usage: | ||
EXPORT praw_client_id=<REDDIT_CLIENT_ID> | ||
EXPORT praw_client_secret=<REDDIT_CLIENT_SECRET> | ||
python3 use_file_token_manager.py | ||
""" | ||
import os | ||
import sys | ||
|
||
import praw | ||
from praw.util.token_manager import FileTokenManager | ||
|
||
REFRESH_TOKEN_FILENAME = "refresh_token.txt" | ||
|
||
|
||
def initialize_refresh_token_file(): | ||
if os.path.isfile(REFRESH_TOKEN_FILENAME): | ||
return | ||
|
||
refresh_token = input("Initial refresh token value: ") | ||
with open(REFRESH_TOKEN_FILENAME, "w") as fp: | ||
fp.write(refresh_token) | ||
|
||
|
||
def main(): | ||
if "praw_client_id" not in os.environ: | ||
sys.stderr.write("Environment variable ``praw_client_id`` must be defined\n") | ||
return 1 | ||
if "praw_client_secret" not in os.environ: | ||
sys.stderr.write( | ||
"Environment variable ``praw_client_secret`` must be defined\n" | ||
) | ||
return 1 | ||
|
||
initialize_refresh_token_file() | ||
|
||
refresh_token_manager = FileTokenManager(REFRESH_TOKEN_FILENAME) | ||
reddit = praw.Reddit( | ||
token_manager=refresh_token_manager, | ||
user_agent="use_file_token_manager/v0 by u/bboe", | ||
) | ||
|
||
scopes = reddit.auth.scopes() | ||
if scopes == {"*"}: | ||
print(f"{reddit.user.me()} is authenticated with all scopes") | ||
elif "identity" in scopes: | ||
print( | ||
f"{reddit.user.me()} is authenticated with the following scopes: {scopes}" | ||
) | ||
else: | ||
print(f"You are authenticated with the following scopes: {scopes}") | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.