Camera Proxy API 401

Hello! I’m trying to access one of my Home Assistant camera with URL:
http://192.168.1.6:8123/api/camera_proxy_stream/camera.videokamera?api_password=

But I only get:
401: Unauthorized

I’ve tried to use this config:

homeassistant:
  auth_providers:
    - type: homeassistant
    - type: trusted_networks
      trusted_networks:
        - 192.168.1.0/24
    - type: legacy_api_password
      api_password: !secret http_password

Without any success. What do I have to do in order to be able to access my camera from API URL?

I dont see an api endpoint for camera_proxy_stream covered here. REST API | Home Assistant Developer Docs

On the other hand, you can call rest endpoint /api/states/camera.videokamera and extract the auth token to use in url? Maybe?

1 Like

Thanks it works but I get low fps. It shows a snapshoot every 10 seconds. Can I get HLS m3u8 stream or something like that?

What is the output of this api? Just image url?

Here is an example:

{“entity_id”: “camera.videokamera”, “state”: “recording”, “attributes”: {“access_token”: “313c8219063e9abbd27274d91e519492ac9e4123a80695447fc751c200bdc70c”, “model_name”: "Video Camera ", “brand”: “Tuya”, “frontend_stream_type”: “hls”, “entity_picture”: “/api/camera_proxy/camera.videokamera?token=313c8219063e9abbd27274d91e519492ac9e4123a80695447fc751c200bdc70c”, “friendly_name”: "Videokamera ", “supported_features”: 2}, “last_changed”: “2022-02-10T19:45:06.937392+00:00”, “last_updated”: “2022-02-10T19:49:56.020221+00:00”, “context”: {“id”: “4b8bd0f8e6491047783c1cd0c5b0b22f”, “parent_id”: null, “user_id”: null}}

When starting camera stream in loveplace I get a HLS from api/hls/token/master_playlist.m3u8

http://192.168.1.6:8123/api/camera_proxy_stream/camera.videokamera?token=313c8219063e9abbd27274d91e519492ac9e4123a80695447fc751c200bdc70c only gives me snapshoots

What i am saying that;

  • read access_token value from /api/camera_proxy rest call
  • generate your hls url via /api/hls/{access_token}/master_playlist.m3u8

The problem is that access_token from api isn’t the same as HLS access_token. I don’t know how to get HLS access_token.

There is also a web service to generate hls endpoint url: source code

In case you want to check web socket: WebSocket API | Home Assistant Developer Docs

This seems complicated for me, I’m still new to home assistant. What do I have to call to get HLS access token, can you help me out?

So far this code. How do I continue?

from websocket import create_connection
import json
ws = create_connection("ws://192.168.1.6:8123/api/websocket")
result =  ws.recv()
print("Received '%s'" % result)
ws.send(json.dumps(
{
  "type": "auth",
  "access_token": "eyJ0eXAi(...)6XuzGzBmq4rrV9LdqXX-dGrBhfBo"
}))
result2 =  ws.recv()
print("Received '%s'" % result2)
ws.close()

I will give a hand hopefully evening today, when i am back at home

1 Like

Hello did you have successfully get the token work ? In my case the token only have the duration of 5 min and after this is renewed.

I’m try to find a solution to get this token or too have one long time token in order to put this working

Please let me know if you find any solution :slight_smile:

Thank you

is there any other way to get hls endoint url ?

@WattageGuy i managed to do it, a bummer that this is not available over simple REST calls

import websocket 

class HaClient:
    def __init__(self):
        self.ws = websocket.WebSocket()
        self.ws.connect(f'ws://{HA_HOST}/api/websocket')
        if self.receive()["type"] == "auth_required":
            auth = {
                "type": "auth",
                "access_token": HA_TOKEN
            }
            r = self.send(auth)
            if r["type"] != "auth_ok":
                print("Authentication failed")
                sys.exit()

    def send(self, dict_obj):
        self.ws.send(json.dumps(dict_obj))
        return self.receive()

    def receive(self):
        return json.loads(self.ws.recv())

    def get_hls_url(self):
        service_call = {
            "type": "camera/stream",
            "id": 41,
            "entity_id": HA_CAM_ENTITY,
        }
        hls_url = self.send(service_call)["result"]["url"]
        return f"http://{HA_HOST}{hls_url}"

c = HaClient()
print(c.get_hls_url())