I'm trying to use PubNub presence in my app and I'm getting a repeating forbidden error. I do have the permissions enabled in the PubNub Admin Portal.
Here is my subscription code:
var initSettings: pubnub.IInitSettings = {
publish_key: "myPubKey",
subscribe_key: "mySubKey",
uuid: "myUUID",
auth_key: "myAuthKey"
};
this.pubnub = PUBNUB(initSettings);
console.log(this.pubnub);
var subscribeSettings: pubnub.ISubscribeSettings = {
channel: "chat",
presence: this.userConnected,
message: this.processMessage
};
this.pubnub.subscribe(subscribeSettings);
This is my userConnected
callback:
userConnected = (m: any) => {
var hereNowSettings: pubnub.IHereNowSettings = {
channel: this.channelString,
callback: (message: any) => {
this.channelCount++;
}
};
this.pubnub.here_now(hereNowSettings);
};
I get a repeating error that says
pubnub-3.7.14.js:2644 GET http://ps17.pubnub.com/subscribe/mySubKey/chat%2Cchat-pnpres/0/0?uuid=myUUID&pnsdk=PubNub-JS-Web%2F3.7.14 403 (Forbidden)
I don't understand why I'm getting this error. Can anyone explain this?
I added a secret key and grant to my pubnub config:
createPubNubConnections() {
let initSettings: pubnub.IInitSettings = {
publish_key: publishKey,
subscribe_key: subscribeKey,
uuid: uuid,
auth_key: authKey,
secret_key: secretKey
};
this.pubnub = PUBNUB(initSettings);
console.log(this.pubnub);
let subscribeSettings: pubnub.ISubscribeSettings = {
channel: "chat",
presence: this.userConnected,
message: this.processMessage
};
this.pubnub.subscribe(subscribeSettings);
let grantSettings: pubnub.IGrantSettings = {
read: true,
callback: (message: any) => { console.log(message); }
};
this.pubnub.grant(grantSettings);
}
However, now I'm getting an error that says
Missing Secret Key
If you have Access Manager enabled then your server must grant permission to read
on the channel and the presence channel if you are going to subscribe (with presence) on that channel.
You said:
permissions enabled in the PubNub Admin Portal
... but you do not enable (grant) permissions in the Admin Portal, you grant permission in code using the secret key.
See the following links for how to use Access Manager to grant permissions:
TL;DR (from the links above) You have to grant
the read permission on both the channel and its -pnpres channel. So if you grant read to " chat " then you need to grant read to " chat-pnpres ", if you want to subscribe to presence events on that channel. It also gives you get/setState
, herenow
and wherenow
permissions on the channel ( chat ), too.
By the way, with current PubNub SDKs, the secret-key
gives you all permissions on all channels and channel groups. So do NOT include an auth-key when you init
PubNub with a secret-key because the auth-key will override the secret-key with respect to all PubNub requests other than grant
by your server.
Anyone with the secretKey can grant and revoke permissions to your app. Never let your secretKey be discovered, and to only exchange it / deliver it securely. Only use the secretKey on secure server-side platforms.
Make sure you've granted read permission on the presence channel also.
Eg if the channel is my_channel
, grant read permission to the my_channel-pnpres
presence channel also.
More info available here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.