Hey everyone! I’m trying to figure out how to obtain an access token for Home Assistant using the authentication API. I came across the documentation, but I’m a bit stuck on how to implement it in Python. Let me walk you through what I’ve tried so far.
First, I followed the example provided in the documentation:
http://your-instance.com/auth/authorize?client_id=https%3A%2F%2Fhass-auth-demo.glitch.me&redirect_uri=https%3A%2F%2Fhass-auth-demo.glitch.me%2F%3Fauth_callback%3D1
But I’m not sure how to translate this into a Python script. I tried sending a POST request with the necessary headers and payload, but I keep getting a 405 error. It seems like the endpoint doesn’t support POST requests. When I switch to GET, it just returns some unhelpful information about the URL.
Here’s the code I tried:
python
headers = {
“content-type”: “application/x-www-form-urlencoded”,
}
payload = {
“client_id”: “http%3A%2F%2F192.168.30.130%3A8123%2F&”,
“redirect_uri”: “http%3A%2F%2F192.168.30.130%3A8123%2Fprofile%3Fauth_callback%3D1&”,
‘response_type’: ‘code’,
“state”: “http%3A%2F%2Fhassio.local%3A8123”
}
url = “http://192.168.30.130:8123/auth/authorize?”
ret = requests.post(url, json=payload, headers=headers)
print(ret)
And also:
python
headers = {
“content-type”: “application/x-www-form-urlencoded”,
}
url = “http://192.168.30.130:8123/auth/authorize?response_type=code&redirect_uri=http%3A%2F%2F192.168.30.130%3A8123%2F%3Fauth_callback%3D1&client_id=http%3A%2F%2F192.168.30.130%3A8123%2F&state=eyJoYXNzVXJsIjoiaHR0cDovLzE5Mi4xNjguMzAuMTMwOjgxMjMiLCJjbGllbnRJZCI6Imh0dHA6Ly8xOTIuMTY4LjMwLjEzMDo4MTIzLyJ9”
ret = requests.post(url, headers=headers)
print(ret.text)
Both attempts resulted in errors. I’m not sure if I’m missing something obvious or if there’s a better way to approach this. If anyone has a simple example or can point me in the right direction, I’d really appreciate it! Thanks in advance for your help!