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 = requests.post(“https://api.companieshouse.gov.uk/oauth2/authorise”, data=data)
print(response)
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 = “https://test-data-sandbox.company-information.service.gov.uk/test-data/company”
Set the API key
api_key = “API_KEY”
response = requests.post(url,auth=(api_key, “”))
print(response)
if response.status_code == 201:
data = response.json()
print(data)
else:
print(“Error”)
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.