Android Pusher签名无效 - 客户端

时间:2015-11-17 10:25:01

标签: android pusher

使用Pusher,出现无效签名跟踪错误的问题:

Invalid signature: Expected HMAC SHA256 hex digest of socketID:channel, but got hash.  

这是我的代码:

public static PusherOptions pusherChannelAuthorise(){
        Log.v(TAG, "pusherChannelAuthorise");
        mMobileToken = Handler_Login.fetchmobile();
        PUSHER_MOBILETOKEN_MAP.put(PUSHER_MOBILETOKEN_LABEL, mMobileToken);

        HttpAuthorizer authoriser = new HttpAuthorizer(url);
        authoriser.setHeaders(PUSHER_MOBILETOKEN_MAP);
        options = new PusherOptions().setAuthorizer(authoriser);

        return options;
    }

    public static void connect(){
        Log.v(TAG, "connect" + "::CONNECTED::");
        PusherOptions presenceChannelOptions = pusherChannelAuthorise();
        presenceChannelConnected = true;
        pusher = new Pusher(pusherHash, presenceChannelOptions);
        pusher.connect(new ConnectionEventListener() {
            @Override
            public void onConnectionStateChange(ConnectionStateChange change) {
                socketId = pusher.getConnection().getSocketId();
                Log.v(TAG, "The socketId is: " + socketId);
            }

            @Override
            public void onError(String message, String code, Exception e) {
                Log.v(TAG, "There was a problem connecting!");
            }
        }, ConnectionState.CONNECTED);
        presencechannel = pusher.subscribePresence(PUSHER_PUBLIC_CHANNEL_NAME, listener);
        String myUuid = Handler_Login.getMyUuid();
        privatechannel = pusher.subscribePrivate(DOCSYNC_CHANNEL + myUuid, docSyncListener);
        privatechannel.bind("client-init", docSyncListener = new PrivateChannelEventListener() {
            @Override
            public void onEvent(String channelName, String eventName, String data) {
                Log.v(TAG, "Private Test onEvent: " + channelName + "  " + eventName + "  " + data);
            }

            @Override
            public void onSubscriptionSucceeded(String channelName) {
                Log.v(TAG, "Private Channel onSubscriptionSucceeded: " + channelName);
            }

            @Override
            public void onAuthenticationFailure(String message, Exception e) {
                Log.v(TAG, "Private Channel onAuthenticationFailure: " + message + ":::" + e);
            }
        });
    }

我做错了什么?我是否必须在授权人的标题中设置其他参数? @leggetter

对状态通道的身份验证工作正常,但私有通信失败。

修改 服务器代码(PHP,Laravel):

public function postMobilePusher(Request $request)
    {
        if (null !== $request->header('mobileToken')) {

            $currentUser = User::where('mobileToken', '=', $request->header('mobileToken'))->first();

            if (null !== $currentUser) {

                define('APP_ID', ID);
                define('APP_KEY', KEY);
                define('APP_SECRET', SECRET);

                $pusher = new Pusher(APP_KEY, APP_SECRET, APP_ID);

                $socket_id = $request->get('socket_id');
                $channel_name = $request->get('channel_name');

                $user_id = $currentUser->id;

                $user_info = [
                    'firstName' => $currentUser->firstName
                    ];

                return $auth = $pusher->presence_auth( $channel_name, $socket_id, $user_id, $user_info );

            }

        }

        return abort(403, 'Unauthorized action.');

    }

1 个答案:

答案 0 :(得分:1)

更新服务器端代码以使用socket_auth用于私人频道,presence_auth用于在线频道:

public function postMobilePusher(Request $request)
{
    if (null !== $request->header('mobileToken')) {

        $currentUser = User::where('mobileToken', '=', $request->header('mobileToken'))->first();

        if (null !== $currentUser) {

            define('APP_ID', ID);
            define('APP_KEY', KEY);
            define('APP_SECRET', SECRET);

            $pusher = new Pusher(APP_KEY, APP_SECRET, APP_ID);

            $socket_id = $request->get('socket_id');
            $channel_name = $request->get('channel_name');

            $auth = null;
            if(starts_with($channel_name, 'private-')) {
              // TODO: check user has permission to access channel
              $auth = $pusher->socket_auth($channel_name, $socket_id);
            }
            else {
              // presence
              // TODO: check user has permission to access channel
              $user_id = $currentUser->id;

              $user_info = [
                'firstName' => $currentUser->firstName
              ];

              $auth = $pusher->presence_auth( $channel_name, $socket_id, $user_id, $user_info );
            }



        }

    }

    return abort(403, 'Unauthorized action.');

}