对于云API服务,常见的方式就是按照API调用次数收费,某些API调用也就有某些限制,比如在特定时间内只允许调用指定的次数以免造成滥用。虽然Autodesk的view and Data API目前还没有应用这样的限制,但我们最好也能实现这样的机制,比如对于或者Access Token这样的操作,一个Access Token是有一定的有效期的,在这个token的有效期内,我们就没必要重复发出API调用获取新的Acces Token,只有返回仍然有效的token就可以了。下面是c#实现的简单的逻辑,用一个全局静态变量来缓存Access Token:

public class Util
{
    private static readonly ILog logger = LogManager.GetLogger(typeof(Util));     string baseUrl = "";
    RestClient m_client;     public static AccessToken token;
    public static DateTime issueDateTime;
    //refresh token if the token is about to expire in 5 seconds
    public static int ABOUT_EXPIRED_SECONDS = 5;     public Util(string baseUrl)
    {
        this.baseUrl = baseUrl;
        m_client = new RestClient(baseUrl);
    }     public AccessToken GetAccessToken(string clientId, string clientSecret)
    {
        //no token or token is going to be expired
        // (less than ABOUT_EXPIRED_SECONDS)         if (token == null
            || (DateTime.Now - issueDateTime).TotalSeconds
                > (token.expires_in - ABOUT_EXPIRED_SECONDS))
        {
            RestRequest req = new RestRequest();
            req.Resource = "authentication/v1/authenticate";
            req.Method = Method.POST;
            req.AddHeader("Content-Type", "application/x-www-form-urlencoded");
            req.AddParameter("client_id", clientId);
            req.AddParameter("client_secret", clientSecret);
            req.AddParameter("grant_type", "client_credentials");
            //avoid CORS issue, do not use this if you just need to get access token from same domain             req.AddHeader("Access-Control-Allow-Origin", "*");             IRestResponse<AccessToken> resp = m_client.Execute<AccessToken>(req);
            logger.Debug(resp.Content);             if (resp.StatusCode == System.Net.HttpStatusCode.OK)
            {
                AccessToken ar = resp.Data;
                if (ar != null)
                {
                    token = ar;                     //update the token issue time
                    issueDateTime = DateTime.Now;                 }
            }
            else
            {                 logger.Fatal("Authentication failed! clientId:" + clientId);             }         }
        else
        {
            ;//Do nothing, use the saved access token in static var
        }         return token;
    }     }

 

当然,根据需要你可以选择其他的方式,比如把token保存在数据库中,或者memcache中。

View and Data API tips: 缓存Access Token的更多相关文章

  1. View and Data API Tips: Constrain Viewer Within a div Container

    By Daniel Du When working with View and Data API, you probably want to contain viewer into a <div ...

  2. View and Data API Tips: Hide elements in viewer completely

    By Daniel Du With View and Data API, you can hide some elements in viewer by calling "viewer.hi ...

  3. View and Data API Tips: how to make viewer full screen

    By Daniel Du If you have not heard of View and Data API, here is the idea, the View & Data API e ...

  4. View and Data API Tips : Conversion between DbId and node

    By Daniel Du In View and Data client side API, The assets in the Autodesk Viewer have an object tree ...

  5. Using View and Data API with Meteor

    By Daniel Du I have been studying Meteor these days, and find that Meteor is really a mind-blowing f ...

  6. View and Data API 现在支持IE11了

    By Daniel Du After a long time waiting, IE11 finally supports WebGL, which enables us viewing our 3D ...

  7. Autodesk View and Data API二次开发学习指南

    什么是View and Data API? 使用View and Data API,你可以轻松的在网页上显示大型三维模型或者二维图纸而不需要安装任何插件.通过View and Data API,你可以 ...

  8. 使用AxisHelper帮助理解View and Data API中的坐标系统

    大家使用View and Data API做三维模型开发,必然首先要理解View and Data API的坐标系统,即XYZ三个轴向分别是怎么定义的.Three.js里面提供了一个AxisHelpe ...

  9. 在View and Data API中更改指定元素的颜色

    大家在使用View and Data API开发过程中,经常会用到的就是改变某些元素的颜色已区别显示.比如根据某些属性做不同颜色的专题显示,或者用不同颜色表示施工进度,或者只是简单的以颜色变化来提醒用 ...

随机推荐

  1. SQL 里解析 XML 格式 字段 信息

    DECLARE @ItemMessage XML ),zje ),yfje ),bcje ),URL ),Remark )) SET @ItemMessage=N'<List> <i ...

  2. Boot from Volume - 每天5分钟玩转 OpenStack(61)

    Volume 除了可以用作 instance 的数据盘,也可以作为启动盘(Bootable Volume),那么如何使 volume 成为 bootable 呢? 现在我们打开 instance 的 ...

  3. Java 8新特性-2 接口定义增强

    为了解决当原有的接口中方法不足时,向原有的接口中添加新的方法,该接口下的N多实现类也需要重写该方法的问题!Java8引入了接口定义增强概念! Java8 打破了原有的接口的方法定义: 公共的.抽象的  ...

  4. T-SQL:毕业生出门需知系列(五)

    第5课 高级数据过滤 5.1 组合 WHERE 子句 第4课介绍的 WHERE 子句在过滤数据时都是用单一的条件. 5.1.1 AND 操作符 检索由供应商 DLL01 制造且价格小于等于 4 美元的 ...

  5. 前端项目构建之yeoman

    各位好啊,我又和大家见面了,也许你已经不记得大明湖畔的容嬷嬷,但是只要记得博客园中的我就好,希望我的博客能像一股清风,为你驱散炎热的酷暑,好了,废话不多说,开始上干货,我今天带给大家的是前端工程化开发 ...

  6. 相克军_Oracle体系_随堂笔记001-概述

    一.Oracle官方支持 1.在线官方文档 http://docs.oracle.com/ 2.metalink.oracle.com,如今已经改成:http://support.oracle.com ...

  7. 初次体验百度eCharts遇到的问题和解决方法

    前言 上周在厌烦Highchart下,体验了下百度的eCharts,支持IE6.7.8+外,对数据在线编辑还有工具栏支持,体验时遇到了几个小问题,最近两天在尝试得到了一个解决方法. Tooltip时单 ...

  8. SQL Server 通过重建方式还原 master 数据库

    1,备份master数据库 2,停止服务,直接删除master数据文件 3,用安装程序重建master数据文件 控制台下进入安装目录就不说了 D:\SetUp\sqlserver2012>Set ...

  9. C语言字符串匹配、goto语句、关机命令使用

    1.程序执行修改窗口字体颜色命令: 2.程序执行修改窗口标题命令: 3.程序执行关机倒计时命令: 4.根据提示输入团队名称JYHACK TEAM 根据提示输入团队网址:http://bbs.jyhac ...

  10. Linux(Centos)快速搭建SVN

    前言 项目中源码或者文档需要进行管理与版本记录,历数此类工具VSS.CVS.SVN.GIT等等,有非常多的版本控制系统.SVN现在还是很常见,把笔记总结搬上博客,SVN这个再不放以后估计只能写GIT的 ...