对于云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. C#设计模式系列:桥接模式(Bridge)

    1.桥接模式简介 1.1>.定义 当一个抽象可能有多个实现时,通常用继承来进行协调.抽象类定义对该抽象的接口,而具体的子类则用不同的方式加以实现.继承机制将抽象部分与它的实现部分固定在一起,使得 ...

  2. WPF根据Oracle数据库的表,生成CS文件小工具

    开发小工具的原因: 1.我们公司的开发是客户端用C#,服务端用Java,前后台在通讯交互的时候,会用到Oracle数据库的字段,因为服务器端有公司总经理开发的一个根据Oracle数据库的表生成的cla ...

  3. WPF 后台读取样式文件

    ResourceDictionary dic = new ResourceDictionary { Source = new Uri("Styles.xaml",UriKind.R ...

  4. 平衡二叉树AVL删除

    平衡二叉树的插入过程:http://www.cnblogs.com/hujunzheng/p/4665451.html 对于二叉平衡树的删除采用的是二叉排序树删除的思路: 假设被删结点是*p,其双亲是 ...

  5. Windows Server 2008 R2 WEB服务器配置系列文章索引

    最近这段时间趁天翼云1元主机活动,购买了一个1元主机,主要是为了写一些服务器配置的教程. 已经完成如下几篇文章,送给大家. 国内云主机比较 天翼云/阿里云/腾讯云 Windows Server 200 ...

  6. 在Linux客户机与Windows宿主机之间建立共享(VitrualBox)

    VirtualBox中,如果客户机和宿主机都是Windows的话,共享相对是比较方便的.一般是通过\\vboxsvr\shared 这样的路径访问即可. 但是如果客户机是Linux的话,就略微麻烦一点 ...

  7. IOS开发之自动布局显示网络请求内容

    在上一篇博客中详细的介绍了IOS开发中的相对布局和绝对布局,随着手机屏幕尺寸的改变,在App开发中为了适应不同尺寸的手机屏幕,用自动布局来完成我们想要实现的功能和效果显得尤为重要.本人更喜欢使用相对布 ...

  8. Objective-C中的内存管理

    在编程语言中是少不了对内存的管理的,内存对于计算机来说是宝贵的资源,所以对使用不到的资源进行回收是很有必要的.OC中使用引用计数和垃圾回收来管理内存,在OC中为每个对象分配一个引用计数器,当对象刚刚被 ...

  9. T-Sql(六)触发器(trigger)

    不知不觉讲到触发器了,一般我们做程序的很少接触到触发器,触发器的操作一般是DB人员来完成. 然而有的时候一些简单的业务需要我们自己去完成,不能每次都去麻烦DB人员,所以说,编程人员要全才,除了编程以为 ...

  10. 10.Struts2连接数据库

    链接数据库的两种方式: 1.在Action中连接数据库 2.使用工具类连接数据库 1.在Action中连接数据库 源码文档目录如图所示: 1.建立数据库 数据库建立语句: create databas ...