Search code examples
javascriptdjangogetstream-io

Getstream.io "Not authenticated" with read only token


I am using the django getstream.io client. My backend code looks like the following, it generates a read-only token and stores it in the response with my jwt token that is sent on a successful login. This code is at the bottom of my settings.py file, which contains the STREAM_API_SECRET, and STREAM_API_KEY key settings. These are also in my settings.py and match what is in my getstream.io dashboard.

from stream_django.client import stream_client
def jwt_response_payload_handler(token, user=None, request=None):
    user_feed_1 = stream_client.feed('user', str(user.id))
    readonly_token = user_feed_1.get_readonly_token()
    return {
        'token': token,
        'stream': str(readonly_token)
    }

On the frontend, the token is correctly gotten from the login response, which contains the stream token. It attempts to setup a real time stream, but when it connects i get a "Not authenticated error". I have confirmed, that the token passed to the following client side function, matches the token generated above.

function setupStream (token, id) {
  var client = stream.connect(STREAM_API_KEY, null, STREAM_APP_ID)
  var user1 = client.feed('user', id, token)

  function callback (data) {
    console.log(data)
  }

  function failCallback (data) {
    alert('something went wrong, check the console logs')
    console.log(data)
  }

  user1.subscribe(callback).then(() => {}, failCallback)
}

I am not sure what I am doing wrong because as far as I can tell everything is setup correctly. The tokens, and user id's match what is on the front and backend.

I am following what is in the documentation, but its not working: https://getstream.io/docs/#readonly-tokens

When i tried just the following in console:

  user1.get({ limit: 5, offset: 0 })
  .then(callback)
  .catch(failCallback)

The exact error response body i get from that is:

{
    "code": null,
    "detail": "url signature missing or invalid",
    "duration": "7ms",
    "exception": "AuthenticationFailed",
    "status_code": 403
}

EDIT:

it seems by changing: get_readonly_token() to .token, creating a read/write token, the client side code works. Does readonly token not work?


Solution

  • so it turns out, I am decoding the read_only token incorrectly. Changing the backend code to the following solved my issues;

     'stream': readonly_token.decode("utf-8")