Help with Oauth 2.0 to Bulk change registered addresses of companies

I am looking to create a python application to change a batch of companies’ registered offices. I am having issues understanding why I’m getting a 401 reply every time I try to get an access token, to help me then create a transaction.
Here is a snippet of what I have done below:
#-------- This section if for the Oauth2.0-----------------------

Set up client credentials

client_id = “MY_ID”
client_secret = “MY_SECRET”
redirect_uri = “”

Authenticate and get access token

data = {
“client_id”: client_id,
“client_secret”: client_secret,
“redirect_uri”: redirect_uri,
“grant_type”: “client_credentials”
response =“”, data=data)

access_token = response.json()[“access_token”]

headers = {‘Authorization’: 'Bearer ’ + access_token}


My print response gives a 401 error every time. I plan to use this in sandbox mode whilst testing. I have it creating a company before running the Auth section:

import requests
import json

#Create Test Data Enviroment
url = “

Set the API key

api_key = “API_KEY”
response =,auth=(api_key, “”))
if response.status_code == 201:
data = response.json()

CompanyNumber = data[‘company_number’]
AuthCode = data[‘auth_code’]
CompURL = data[‘company_uri’]

print(CompanyNumber, AuthCode, CompURL)

If anyone can help me sort this out, I feel the transaction and put requests are strait forward after this part.