【问题标题】:PubNub Presence Repeating 403 Forbidden ErrorPubNub 存在重复 403 禁止错误
【发布时间】:2016-07-26 00:02:51
【问题描述】:

我正在尝试在我的应用中使用 PubNub 存在,但我遇到了重复的禁止错误。我确实在 PubNub 管理门户中启用了权限。

这是我的订阅代码:

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);

这是我的userConnected 回调:

userConnected = (m: any) => {
    var hereNowSettings: pubnub.IHereNowSettings = {
        channel: this.channelString,
        callback: (message: any) => {
            this.channelCount++;
        }

    };

    this.pubnub.here_now(hereNowSettings);
};

我收到一个重复的错误提示

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)

我不明白为什么会出现此错误。谁能解释一下?

更新:

我添加了一个密钥并授予我的 pubnub 配置:

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);
}

但是,现在我收到一个错误提示

缺少密钥

【问题讨论】:

    标签: javascript typescript pubnub


    【解决方案1】:

    PubNub 访问管理器和授予权限

    如果您启用了访问管理器,那么您的服务器必须授予read 在频道上的权限,如果您要在该频道上订阅(在状态下),则必须在该频道上授予权限。

    你说:

    PubNub 管理员门户中启用的权限

    ...但您没有在管理门户中启用(授予)权限,您使用密钥在代码中授予权限。

    有关如何使用 Access Manager 授予权限,请参阅以下链接:

    TL;DR(来自上面的链接) 您必须grant channel 及其 -pnpres 频道的 read 权限。因此,如果您将读取权限授予“chat”,那么您需要授予“chat-pnpres”读取权限,如果您想订阅该频道上的在线状态事件。它还为您提供频道(聊天)的get/setStateherenowwherenow 权限。


    最新版本的 PubNub SDK 更新

    顺便说一句,在当前的 PubNub SDK 中,secret-key 为您提供所有频道和频道组的所有权限。因此,当您使用 secret-key init PubNub 时不要包含 auth-key,因为 auth-key 将覆盖 secret-key 与您的服务器的grant 以外的所有 PubNub 请求有关。

    Secret Key - All Permissions

    拥有 secretKey 的任何人都可以授予和撤消您的应用的权限。永远不要让您的密钥被发现,并且只能安全地交换/交付它。仅在安全的服务器端平台上使用 secretKey。

    【讨论】:

    • 我用新代码更新了我的问题。有什么办法可以帮助我解决这个问题吗?据我所知,我使用正确。
    • 没关系,我想通了。谢谢你的回答,很有帮助。
    • 确保只在安全服务器中使用密钥,而不是在客户端应用程序中。
    【解决方案2】:

    确保您还授予了在线状态频道的读取权限。

    例如如果频道是my_channel,则还授予my_channel-pnpres 在线频道的读取权限。

    更多信息here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-02
      相关资源
      最近更新 更多