Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script_auth_friend_list.py example.
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/usr/bin/env python | ||
|
||
"""script_auth_friend_list.py outputs the authenticated user's list of friends. | ||
This program demonstrates the use of ``prawcore.ScriptAuthorizer``, which | ||
enables those listed as a developer of the application to authenticate using | ||
their username and password. | ||
""" | ||
import os | ||
import prawcore | ||
import sys | ||
|
||
|
||
def main(): | ||
"""main is the program's entry point when directly executed.""" | ||
authenticator = prawcore.Authenticator( | ||
os.environ['PRAWCORE_CLIENT_ID'], | ||
os.environ['PRAWCORE_CLIENT_SECRET']) | ||
authorizer = prawcore.ScriptAuthorizer(authenticator, | ||
os.environ['PRAWCORE_USERNAME'], | ||
os.environ['PRAWCORE_PASSWORD']) | ||
authorizer.refresh() | ||
|
||
url = 'https://oauth.reddit.com/api/v1/me/friends' | ||
with prawcore.session(authorizer) as session: | ||
data = session.request('GET', url) | ||
|
||
for friend in data['data']['children']: | ||
print(friend['name']) | ||
|
||
return 0 | ||
|
||
|
||
if __name__ == '__main__': | ||
sys.exit(main()) |