我正在尝试生成Pusher身份验证字符串。 https://pusher.com/docs/auth_signatures。在我的Django视图中,响应是一个带有auth属性的JSON字符串,就像它要求的那样。
{u'auth': u'1019dcd6d219db50d37e:926260b960edf94509e0fd86547ec756cc4a1006baef4e0b3f8ec35c1e7b8c05'}
但是我收到了这个错误。
Pusher : Error : {"type":"WebSocketError","error":{"type":"PusherError","data":{"code":null,"message":"Auth info required to subscribe to private-account-313"}}}
JSON格式不正确,还是问题出在其他地方?在Pusher的网站上找不到任何东西。
def pusher_auth(request):
# """ -Pusher private channel authentication
# Docs: https//pusher.com/docs/authenticating_users
# """
if request.method == 'POST':
user = request.user
socket_id = request.POST['socket_id']
channel = request.POST['channel_name']
if not (socket_id or channel or user):
raise Exception("Permission denied.")
fragments = channel.split('-')
resource = fragments[1]
resource_id = int(fragments[2])
account = Account.objects.get(email=user.email)
pusher_client = Pusher(app_id='199731', key='1019dcd6d219db50d37e', secret='9550fb09aacce399eeb6',
cluster='ap1', ssl=True)
auth = pusher_client.authenticate(channel, socket_id)
try:
if resource == 'account' and (account.id == resource_id):
print(auth)
context = auth
return composeJsonResponse(200, "", context)
else:
return {'nope'}
except:
raise Exception("Permission denied.")
订阅频道的角度代码
/**
* pusher-js wrapper as a factory.
* Docs: https://github.com/pusher/pusher-js
*/
(function () {
'use strict';
$pusher.$inject = ["Config"];
angular
.module('app.common')
.factory('$pusher', $pusher);
/** ngInject */
function $pusher(Config) {
var self = this;
self.client = new Pusher(Config.pusher.key, Config.pusher.options || {});
return {
client: self.client
};
}
})();
(function () {
'use strict';
angular
.module('app.core')
.constant('Config', getConfig());
function getConfig() {
return {
api_path: '',
pusher: {
// TODO: add environment-based configs values.
key: '1019dcd6d219db50d37e',
options: {
encrypted: true
}
}
};
}
})();
/**
* Subscribes to user's Pusher channel and binds callback events.
*/
function bindPusher() {
var defer = $q.defer();
var channelName = 'private-account-' + Session.account.id;
var channel = $pusher.client.subscribe(channelName);
channel.bind('pusher:subscription_succeeded', function (data) {
$log.debug('Pusher subscribed: ' + channel.name);
PushListener.bindAndListen(channel);
defer.resolve(data);
});
channel.bind('pusher:subscription_error', function (status) {
if (status === 403) {
var msg = 'Pusher channel not authorized.';
$log.warn(msg);
defer.reject(msg);
}
});
return defer.promise;
}