How do I get the "auth_code" from location key in the request header?

105 views Asked by At

I am using a combination of the Robot request library, Python requests library and the Python requests_oauthlib library for the testing of an OAuth2 project.

I wonder if there is an easy way to grab the auth code (without string indexing) so I can use it in generating a token

So for example, I have the response header (from logging in):

{'location': 'http://127.0.0.1:19333/callback?code=clT17dFPedsgdfgsSFGDF%3D%3D&state=random%2F', 'content-length': '0', 'date': 'Fri, 09 Oct 2020 11:20:52 GMT'}    

which I got by doing response.header

Is there some way I can just get the clT17dFPedsgdfgsSFGDF%3D%3D by doing something like request.auth_code

1

There are 1 answers

1
Dev On

Use the Urllib.parse. The parse_qs function returns a dictionary of key-value pairs whereas the parse_qsl function returns a list of (key, value) tuples.

import urllib.parse
queryStr = 'http://127.0.0.1:19333/callback?code=clT17dFPedsgdfgsSFGDF%3D%3D&state=random%2F'
urllib.parse.parse_qsl(queryStr)

Output -

enter image description here

For more details on URL encoding