:wave: Hello, team! I don't know what i am doing w...
# ask-ai
c
đź‘‹ Hello, team! I don't know what i am doing wrong, my token generated using the correct api key and secret, but the react native app throw unauthorized error websocket closed {"code": 1006, "pID": undefined, "participant": undefined, "reason": "Expected HTTP 101 response but was '401 Unauthorized'", "room": undefined, "roomID": undefined, "state": 4, "wasClean": undefined} WARN could not fetch region settings {"error": [ConnectionError: Could not fetch region settings: ], "pID": "", "participant": "", "room": "", "roomID": undefined} WARN [ConnectionError: could not establish signal connection: invalid authorization token]
Copy code
class GetLiveKitToken(generics.CreateAPIView):
    """
    Get livekit token for a user
    """
    # permission_classes = (IsAuthenticated,)

    def get(self, request):
        try:
            # user = "Asim ali"
            user_id = 100
            user_name = "Asim ali"
            # room_id = uuid.uuid4()
            token = api.AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET)\
                .with_identity(user_id)\
                .with_name(user_name)\
                .with_grants(
                api.VideoGrants(
                    room_join=True,
                    room="my room",
                    can_publish=True,
                    can_subscribe=True,
                    can_publish_data=True,
                    can_publish_sources=["audio", "data"]
                )
            ).with_metadata(json.dumps({
                "userId": user_id,
                "room_id": "my room",
            }))
            return Response({"token": token.to_jwt(), "room_id": "my room", "url": LIVEKIT_URL})
        except Exception as error:
            logger.error(
                f"Error getting livekit token for user {user_id}: {str(error)}")
            return Response({"error": "Failed to get livekit token"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
t
Thanks for reaching out, Humed. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm here to help you understand why a LiveKit token might not be working, even if it was generated with the correct API key and secret. Let's walk through some common reasons together: • Expired Token: Please double-check that the token's expiration time is still valid and hasn't passed. • Missing Required Fields: Make sure your token includes all the necessary fields, like
identity
and
room
, especially if you're using
room_join
grants. • Clock Skew: It’s important that your server’s clock is synchronized and falls within the token’s valid time window. • Incorrect Token Usage Context: Remember, tokens need to be valid at the moment of the initial connection. • Server-Side Verification: Ensure the server that verifies the token is using the same API key and secret that were used to generate it. Take a look at these points in your setup, and if anything isn’t clear or if you need more help, please don’t hesitate to reach out. I’m here to support you every step of the way! Sources: spec/livekit/access_token_spec.rb | livekit-api/src/access_token.rs | livekit-api/livekit/api/access_token.py | Authentication | LiveKit Docs