View and Data API tips: 缓存Access Token
对于云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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- View and Data API 现在支持IE11了
By Daniel Du After a long time waiting, IE11 finally supports WebGL, which enables us viewing our 3D ...
- Autodesk View and Data API二次开发学习指南
什么是View and Data API? 使用View and Data API,你可以轻松的在网页上显示大型三维模型或者二维图纸而不需要安装任何插件.通过View and Data API,你可以 ...
- 使用AxisHelper帮助理解View and Data API中的坐标系统
大家使用View and Data API做三维模型开发,必然首先要理解View and Data API的坐标系统,即XYZ三个轴向分别是怎么定义的.Three.js里面提供了一个AxisHelpe ...
- 在View and Data API中更改指定元素的颜色
大家在使用View and Data API开发过程中,经常会用到的就是改变某些元素的颜色已区别显示.比如根据某些属性做不同颜色的专题显示,或者用不同颜色表示施工进度,或者只是简单的以颜色变化来提醒用 ...
随机推荐
- 那些让IE6-8羞愧的替补型js
1,html5shiv 这个js特别简单,可以让IE8识别一些新的标签,常用的比如 header footor section,就能使用更好的语义的标签了. 引入方式: <!--[if lt I ...
- 深入学习jQuery元素尺寸和位置操作
× 目录 [1]尺寸设置 [2]位置设置 前面的话 对于javascript来说,元素尺寸有scroll.offset.client三大属性,以及一个强大的getBoundingClientRect( ...
- xUnit安装及注意事项
前言 对于单元测试,想必大家都已再熟悉不过了,同时单元测试的重要性也越发突出,在招聘中也特别强调单元测试,但是对于微软内置的单元测试还是太过于繁琐,于是都在寻找一种简洁并且更加轻量的测试工具.用的最多 ...
- Android随笔之——闹钟制作铺垫之AlarmManager详解
说实话,之前写的两篇博客Android广播机制Broadcast详解.Android时间.日期相关类和方法以及现在要写的,都算是为之后要写的闹钟应用做铺垫,有兴趣的话,大家可以去看看前两篇博客. 一. ...
- Android随笔之——Activity中启动另一应用
最近在写语音交互程序,在语音打开应用这块碰到如何用代码控制应用启动的问题.百度了一下,有两种方案:1.获取应用的包名:2.获取应用的包名.入口类名. 之前对两种方案都进行了尝试,发现方案二中存在一个弊 ...
- Redis碎碎念
1. 关于Cluster cluster_known_nodes:4 cluster_size:3 说明集群中总共有4个节点:集群的size是3,相当于3个主节点参与了槽位分配 2. 如何查看key的 ...
- js 中类似时钟的显示
先上代码 <!DOCTYPE html> <html> <head> <script> function startTime() { var today ...
- 触屏touch事件记录
一.chrome中的Remote Debugging 一开始并没有用这个调试,不过后面需要多点触碰,可chrome模拟器中我没看到这个功能.突然看到了Remote Debugging,网站需要FQ才能 ...
- ReactJS分析之入口函数render
前言 在使用React进行构建应用时,我们总会有一个步骤将组建或者虚拟DOM元素渲染到真实的DOM上,将任务交给浏览器,进而进行layout和paint等步骤,这个函数就是React.render() ...
- 浅谈Hibernate入门
前言 最近打算做一个自己的个人网站,经过仔细思考,打算使用hibernate作为开发的ORM框架,因此各种找资料,由于本人是刚刚接触这技术的,所以就找了比较基础的知识来分享下 基本概述 Hiberna ...