title: Azure AD Authentication Python Web API
body: <p>I'm trying to get the user authenticated using OAuth2 and access resources. However, I'm having some issues doing so. Here are the details.</p>
  
  <ol>
  <li>I've registered the app as a Web Api on the Azure portal</li>
  <li>I want to write a python script through which I can request an authorization code and then the access token</li>
  </ol>
  
  <p>Challenges:</p>
  
  <ol>
  <li>I don't have redirect url. I'm not sure what I can use here</li>
  <li>When I use the link to get the authorization code in the browser, it asks me to sign in to Azure. How can I make sure that it asks me to login through the Python API as well? </li>
  </ol>
  
  <p>Here's the python script that I'm using just to get the authentication code:</p>
  
  <pre><code>import requests
  import json
  
  '''Request Authorization code template
  
  https://login.microsoftonline.com/{tenant}/oauth2/authorize?
  client_id=6731de76-14a6-49ae-97bc-6eba6914391e
  &amp;response_type=code
  &amp;redirect_uri=http%3A%2F%2Flocalhost%2Fmyapp%2F
  &amp;response_mode=query
  &amp;resource=https%3A%2F%2Fservice.contoso.com%2F
  &amp;state=12345
  
  '''
  
  payload = {'client_id': '***', 'response_type': 'code', 'response_mode': 'query',
             'resource': '***'}
  get_authorization_code = requests.get('https://login.microsoftonline.com/tenant/oauth2/authorize',
                          params=payload, verify=False)
  print get_authorization_code
  </code></pre>
  
  <p>Response for this code I get is : Response [200]</p>