Skip to content

Commit c7ab5d8

Browse files
committed
Create standalone auth example
This will also save some typing for other examples
1 parent fc6b1d0 commit c7ab5d8

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

examples/auth.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python3
2+
3+
'''
4+
Here's how you go about authenticating yourself! The important thing to
5+
note here is that this script will be used in the other examples so
6+
set up a test user with API credentials and set them up in here.
7+
'''
8+
9+
#client_id = 'YOUR CLIENT ID'
10+
#client_secret = 'YOUR CLIENT SECRET'
11+
12+
client_id = u'6d2d7ee5f212dc1'
13+
client_secret = u'18828c1021069154576672e86b8ab2e1559d329a'
14+
15+
16+
from imgurpython import ImgurClient
17+
18+
def get_input(string):
19+
''' Get input from console regardless of python 2 or 3 '''
20+
try:
21+
return raw_input(string)
22+
except:
23+
return input(string)
24+
25+
def authenticate():
26+
client = ImgurClient(client_id, client_secret)
27+
28+
# Authorization flow, pin example (see docs for other auth types)
29+
authorization_url = client.get_auth_url('pin')
30+
31+
print("Go to the following URL: {0}".format(authorization_url))
32+
33+
# Read in the pin, handle Python 2 or 3 here.
34+
pin = get_input("Enter pin code: ")
35+
36+
# ... redirect user to `authorization_url`, obtain pin (or code or token) ...
37+
credentials = client.authorize(pin, 'pin')
38+
client.set_user_auth(credentials['access_token'], credentials['refresh_token'])
39+
40+
print("Authentication successful! Here are the details:")
41+
print(" Access token: {0}".format(credentials['access_token']))
42+
print(" Refresh token: {0}".format(credentials['refresh_token']))
43+
44+
return client
45+
46+
# If you want to run this as a standalone script, so be it!
47+
if __name__ == "__main__":
48+
authenticate()

0 commit comments

Comments
 (0)