url signature missing or invalid getstream

2019-08-10 21:46发布

问题:

var client = stream.connect('my-client-id', null, '7723');

var user1 = client.feed('flat', 'my-client-id', 'NuAW6yHVQ2sr9RQvBE-cCuewUlo'); // What is this token param (3rd one)? How is this generated?

var acticity = {
  actor: 'QUXdERFPto', 
  tweet: 'Hello world', 
  verb: 'tweet', 
  object: 1
}

user1.addActivity(acticity).then(null).catch(function(e) {
   // Error object is
   // code: null
   // detail: "url signature missing or invalid"
   // duration: "10ms"
   // exception: "AuthenticationFailed"
   // status_code: 403
});

What is the signature that I am missing?

回答1:

Stream-JS client-side feed tokens

When using the stream-js library on the client you should initiate the connect without your secret key, to avoid sharing your private key with the world (its secret).

var client = stream.connect('api-key', null, 'app-id');

Initiating the client this way does not allow you to read or write from any feed created from this client, thus the following feed will return 403 errors when you try to read or write from it.

client.feed('flat', 'user-id');

But if you generate a read/write token on server side you can initiate a feed with this token and allow read/writes from the client-side:

client.feed('flat', 'user-id', 'read/write token');

To generate a read/write token on the server initiate a client with your secret key and call the following methods:

var client = stream.connect('api-key', 'api-secret', 'app-id');

var readToken = client.getReadOnlyToken('flat', 'user-id');
var readWriteToken = client.getReadWriteToken('flat', 'user-id');

Supply one of these tokens to your client and create a feed instance with this token.

When to use Stream-JS on the client

In most use-cases however you would want to use the stream-js client on your server side and retrieve/publish activities there, enrich these activities with data stored in your local database and send this to the client. One use-case for using stream-js on the client is for realtime notifications