title: Using Python to test HTTP APIs
body: <p>I'm fairly new to Python programming and I don't know all the libraries needed for the following.</p>
  
  <p>I would like to use Python to test some HTTP APIs. Mainly I want to use OAuth and make a few JSON calls. The APIs in question can be found on: <a href="https://developers.trustpilot.com/authentication" rel="nofollow">https://developers.trustpilot.com/authentication</a> and the generate product review link (I can only use one link)</p>
  
  <p>I want to authenticate myself and then generate a product review link in one step. So far I've been using the Advanced REST client (ARC) to make these calls individually. I could also use .arc files if you think it's easier.</p>
  
  <p>The idea would be make these calls successively in one go. So it would be something along the lines: </p>
  
  <p>1) Make the authentication call.</p>
  
  <p>The HTTP Method looks like this:
  <a href="https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken" rel="nofollow">https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken</a>
  Method Post:</p>
  
  <p>Header
  Authorization: Basic Base64encode(APIkey:Secret)
  Content-Type: application/x-www-form-urlencoded</p>
  
  <p>Payload:
  grant_type=password&amp;username=user@mail.com&amp;password=SomePass</p>
  
  <p>Translate this bit into Python basically.</p>
  
  <p>1.a) Add a header to the call</p>
  
  <p>Header Authorization: base64encode hash Content-Type: application/x-www-form-urlencoded</p>
  
  <p>1.b) Add a payload to the call</p>
  
  <p>Payload: grant_type=password&amp;username</p>
  
  <p>4) Receive the token from call made in step 1) (Result is format)</p>
  
  <p>"access token": Auth_token</p>
  
  <p>5) Take the token and use it in creating a product review.</p>
  
  <p>5.a) Add the token in the header</p>
  
  <p>Header: Authorization: Bearer Auth_token</p>
  
  <p>6.a) Add a JSON payload to the call made in step 5.</p>
  
  <p>Here's the code I have so far: </p>
  
  <pre><code>Import requests
  
  header = {'Authorization: Basic NnNrQUprTWRHTU5VSXJGYXBVRGxack1oT01oTUFRZHI6QTFvOGJjRUNDdUxBTmVqUQ==}','Content-Type: application/x-www-form-urlencoded'}
  payload = {'grant_type=password&amp;username=email@address.com&amp;password=SomePassword'}
  r = requests.post('https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken', headers=header, params=payload )
  </code></pre>
  
  <p>Ideally I want to create the requests.post(url, header, payload) and then return what the server answers in JSON format. I think that print r.text would do the last part.</p>
  
  <p>So this is the code I have writtent (that works now):</p>
  
  <pre><code>import requests
  import getpass
  import json
  from requests.auth import HTTPBasicAuth
  
  header = {'grant_type':'password' , 'username':'mail@maildomain.com', 'password':'YourPassword'}
  username= "YOURAPIKEY" #APIKey
  password= "YOURSECRET" #Secret
  res = requests.post(
      'URL/v1/oauth/oauth-business-users-for-applications/accesstoken',
      auth=HTTPBasicAuth(username, password),  # basic authentication
      data=header)
  
  #print(res.content) #See content of the call result.
  
  data = res.json()  # get response as parsed json (will return a dict)
  auth_token = data.get('access_token')
  </code></pre>