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 auth.ini.
7+ '''
8+
9+ from imgurpython import ImgurClient
10+ from helpers import get_input , get_config
11+
12+ def authenticate ():
13+ # Get client ID and secret from auth.ini
14+ config = get_config ()
15+ config .read ('auth.ini' )
16+ client_id = config ['credentials' ]['client_id' ]
17+ client_secret = config ['credentials' ]['client_secret' ]
18+
19+ client = ImgurClient (client_id , client_secret )
20+
21+ # Authorization flow, pin example (see docs for other auth types)
22+ authorization_url = client .get_auth_url ('pin' )
23+
24+ print ("Go to the following URL: {0}" .format (authorization_url ))
25+
26+ # Read in the pin, handle Python 2 or 3 here.
27+ pin = get_input ("Enter pin code: " )
28+
29+ # ... redirect user to `authorization_url`, obtain pin (or code or token) ...
30+ credentials = client .authorize (pin , 'pin' )
31+ client .set_user_auth (credentials ['access_token' ], credentials ['refresh_token' ])
32+
33+ print ("Authentication successful! Here are the details:" )
34+ print (" Access token: {0}" .format (credentials ['access_token' ]))
35+ print (" Refresh token: {0}" .format (credentials ['refresh_token' ]))
36+
37+ return client
38+
39+ # If you want to run this as a standalone script, so be it!
40+ if __name__ == "__main__" :
41+ authenticate ()
0 commit comments