drq61040 2014-11-19 16:59
浏览 57
已采纳

如何检查API令牌是否仍然有效

I'm using a token for my API, the problem is that when the token is invalid (time to live < 0) there's no way to know it, so you go on and fill out a form and only when you submit it you get the message "invalid token" which is really annoyin. My idea is to write a script that checks the token's time to live every 10sec for example and then if it's invalid I can disable the forms for example or display a box to force the user to reload the page. Maybe you guys have better ideas , so please don't hesitate to share.

  • 写回答

1条回答 默认 最新

  • douxing1969 2015-03-27 19:35
    关注

    You can do this in a very simple way, I would guess you have an "expiration time" set on your server side for your token. You can just return this time and have a JS function check that value every x seconds or on every required action (open a pop/form/etc).

    Probably when you return the token your json can look something like

    {
        token:"1234567890",
        expire: "1427484624" //timestamp in seconds, easier to compare
    }
    

    Then your JS function can be something as simple as:

    function isValidToken(token){
        cTs=Math.floor(Date.now() / 1000);
        return (token>=cTs);
    }
    

    and that way you can know very easily.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了