【问题标题】:Channel API -- Invalid Token on production, works on dev serverChannel API——生产中的无效令牌,在开发服务器上工作
【发布时间】:2014-07-10 23:21:13
【问题描述】:

我在使用 Channel API 时遇到了困难,但仅限于生产环境。在开发服务器上这是可行的,但在生产中,我的 javascript 中出现了 Invalid+token 错误,并且我的 Python 中出现了某种空白错误。

这是我的服务器端代码。 'channel_test' 是我的控制器,'do_send_message' 是一个被延迟的函数,'send_message' 是一个带有 try-except 块的包装器。基本上它是创建通道,将令牌设置为 cookie,并调用延迟任务,直到计数器达到 100。每次运行任务时,它都会通过通道发送消息。

def channel_test(self, key):
    if not users.is_current_user_admin(): return 403
    client_id = self.user.email()+key
    token = channel.create_channel(client_id, duration_minutes=10)
    print 'token: %s' % token
    self.response.set_cookie('token', urllib.quote(token))
    deferred.defer(do_send_message, client_id, _queue='retry')

# channel communication
def send_message(client_id, msg):
    """
    msg is a dict
    """
    if client_id:
        print 'client_id: ', client_id
        try:
            channel.send_message(client_id, json.dumps(msg))
            print 'sent message %s' % msg
        except Exception as e:
            print 'something went wrong with msg, %s: %s, %s' % (msg, e, e.__str__())
    else:
        print 'not sending message; no client id'

def do_send_message(client_id, x=10):
    time.sleep(1)

    text = "here's a message! %s" % x
    if x >= 100:
        text = "Done!"
    msg = {"text":text, "percent":x}
    send_message(client_id, msg)

    if x < 100:
        deferred.defer(do_send_message, client_id, x+10, _queue='retry')

每次调用 'send_message' 时都会失败,但 'e' 不会打印任何内容。这是日志中的输出:

something went wrong with msg, {'text': "here's a message! 10", 'percent': 10}: , 

此外,我的客户端 javascript 无法打开连接。它只是得到错误“无效+令牌”。这基本上是从 cookie 中读取令牌,打开连接,每次传递消息时,它都会将消息写入通知托盘,直到 message == 'Done!'然后重新加载页面。但是,如前所述,它会立即失败,并且 onError 会打印 'Object {description: "Invalid+token.", code: "401"}'。

// globals
var channel, socket, hide;
var $msg = $('#channel-message');
var $bar = $('#notification-tray .progress-bar');


onMessage = function(obj){
    var message = JSON.parse(obj.data);
    if (message.percent !== undefined){
        $bar.css('width', message.percent+'%');
    }
    switchMsg(message.text);
};

onOpen = function(){
    $('#notification-tray').fadeIn();
    $bar.css('width', '10%');
};

onClose = function(){
    $('#notification-tray').fadeOut();
};

onError = function(err){
    $('#notification-tray').fadeOut();
    console.log(err);
};


function closeAndReload(){
    socket.close();
    $.removeCookie('token', {path:'/'});
    $.removeCookie('hide', {path:'/'});
    location.reload();
}

function switchMsg(msg){
    $msg.fadeOut(function(){
        $msg.html(msg);
        $msg.fadeIn(function(){
            if (msg == 'Done!'){
                closeAndReload();
            }
        });
    });
}

function initializeChannel(){
    channel = new goog.appengine.Channel(token);
    socket = channel.open();
    socket.onmessage = onMessage;
    socket.onopen = onOpen;
    socket.onclose = onClose;
    socket.onerror = onError;
}


$(function(){
    token = $.cookie('token');
    if (token !== undefined && token != ""){
        initializeChannel();
    }
});

这在我的开发服务器上完美运行,即使我在生产中遇到了这个问题。

提前致谢。

【问题讨论】:

    标签: javascript google-app-engine google-api google-api-python-client channel-api


    【解决方案1】:

    好的,这令人沮丧,但我想我已经发现令牌具有某个未记录的最大长度。我将client_id设置为

    client_id = self.user.email()+key[0:10]
    

    而不是

    client_id = self.user.email()+key
    

    它现在可以工作了。当然,令人讨厌的是(1)它可以在开发服务器上运行,而不是在生产服务器上运行,并且(2)错误信息不是很丰富。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多