How do extract the authorization code from the redirect URL (Python SDK)?
Hi, I am able to get the user to authenticate their credentials for my app, which then redirects them to the redirect_uri I have specified. The URL contains the auth code I need to generate my access token but I have no idea how to retrieve it. I am using Flask/Python and followed the code in the docs but am having no success (Step 3 in https://developer.box.com/docs/authenticate-with-oauth-2). Can anyone help me out?
-
Hi ,
Under step 3 of that guide this will be the line that extracts the code that you need:
code = request.args.get('code')
You'll also need the state parameter that is the line below the above. Basically when you set the state you'll just set some string of data (you set this in step 1). Typically this might be an invoice number, random token, or some other piece of data that you can use to verify the request. The state will have that original value that you set, so just check the state against the same value that you originally set the csrf_token to in step 1.
assert state == csrf_token
You then capture the access token session and refresh token with this line:
access_token, refresh_token = oauth.authenticate(code)
I also have a working Flask / Python sample over at https://github.com/jcleblanc/box-examples/blob/master/python/samples/auth_oauth.py if you'd prefer to just grab that.
Let me know if you run into any issues.
- Jon
-
This is fine, but how do we do it without an application. I just want the auth code in my python script. I have no application, no website, just a python script. Or is it not possible i.e. we have to have an application that redirects us to box, where we have to click the access button and then it redirects us to our app url from where we can get the code. I basically just want the access token in my script(and its just a python script not connected to anything), please let me know a way to do that. Thanks.
Please sign in to leave a comment.
Comments
2 comments