get auth code from URL response - Python
<p>I'm using <a href="https://github.com/OneDrive/onedrive-sdk-python" rel="nofollow">OneDrive Python SKD</a> in order to handle authentication with OneDrive SDK. The authentication is done as:</p>

<pre><code>import onedrivesdk
from onedrivesdk.helpers import GetAuthCodeServer

redirect_uri = "http://localhost:8080/"
client_secret = "your_app_secret"

client = onedrivesdk.get_default_client(client_id='your_client_id',
                                        scopes=['wl.signin',
                                                'wl.offline_access',
                                                'onedrive.readwrite'])

auth_url = client.auth_provider.get_auth_url(redirect_uri)

#this will block until we have the code
code = GetAuthCodeServer.get_auth_code(auth_url, redirect_uri)

client.auth_provider.authenticate(code, redirect_uri, client_secret)
</code></pre>

<p>However since I use a EC2 instance to run this authentication, and furthermore I don't want to utilize a browser just for that, the code blocks indefinitely. Here's the <code>get_auth_code</code> from Microsoft:</p>

<pre><code>def get_auth_code(auth_url, redirect_uri):
    """Easy way to get the auth code. Wraps up all the threading
    and stuff. Does block main thread.
    Args:
        auth_url (str): URL of auth server
        redirect_uri (str): Redirect URI, as set for the app. Should be 
            something like "http://localhost:8080" for this to work.
    Returns: 
        str: A string representing the auth code, sent back by the server
    """
    HOST, PORT = urlparse(redirect_uri).netloc.split(':')
    PORT = int(PORT)
    # Set up HTTP server and thread
    code_acquired = threading.Event()
    s = GetAuthCodeServer((HOST, PORT), code_acquired, GetAuthCodeRequestHandler)    
    th = threading.Thread(target=s.serve_forever)
    th.start()
    webbrowser.open(auth_url)
    # At this point the browser will open and the code
    # will be extracted by the server
    code_acquired.wait()  # First wait for the response from the auth server
    code = s.auth_code
    s.shutdown()
    th.join()
    return code
</code></pre>

<p>I want to return the code. Here's a sample of <code>auth_url</code>:</p>

<pre><code>https://login.live.com/oauth20_authorize.srf?scope=wl.offline_access+onedrive.readwrite&amp;redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F&amp;response_type=code&amp;client_id='your_client_id'
</code></pre>

<p>When I enter that URL in the browser, I get the code back:</p>

<pre><code>http://localhost:8080/?code=Mb0bba7d1-adbc-9c1d-f790-3709cd0b9f16
</code></pre>

<p>SO I want to avoid that cumbersome process to get the code back by using <a href="http://docs.python-requests.org/en/master/" rel="nofollow">requests</a>. How can I accomplish that?</p>