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开发过程中,经常会用到的就是改变某些元素的颜色已区别显示.比如根据某些属性做不同颜色的专题显示,或者用不同颜色表示施工进度,或者只是简单的以颜色变化来提醒用 ...
随机推荐
- lua中的数据类型
lobject.h: lobject.h: 其中使用GCObject表示的数据类型是需要lua 的gc记录的. lstate.h: lobject.h:
- WPF datagrid 加入图片
<DataGridTemplateColumn Header="图像" Width="SizeToCells"> <DataGridTempl ...
- Util应用程序框架公共操作类(十一):表达式生成器
本篇介绍的表达式生成器,用于动态创建表达式. 在Util项目Lambdas目录中,添加ExpressionBuilder,代码如下. using System; using System.Linq.E ...
- Jenkins+SVN+tomcat持续集成发布
有代码更新后重新打包到tomcat再发布,是不是很烦? 看了下面的东西你就不会烦了. SVN或者git等代码版本控制工具不说了,如果是本地开发,也可以安装一个svn server端 jenkins下载 ...
- Objective-C中小怪兽的逻辑
学习Objective-C的面向对象也有一段时间了,为了犒劳自己的学习成果,写个小怪兽来犒劳一下自己把.在LOL中有怪兽和英雄的角色吧,接下来就先写一个小怪兽的类吧.从小怪兽的角度来讲,怪兽都有那些行 ...
- 恢复SharePoint Server 2013 中的“新颖快建视图”
初始化后就很漂亮的"新颖快建视图" 在配置好了OWA(Office Web Apps)的环境下,可以使用独特的"新颖快建视图"新建文件与文件夹,如下图所示: F ...
- FineUI Grid控件高度自适应
引言 页面里使用f:Grid控件,添加分页功能,然后高度填充整个页面. 如何使用 使用FineUI 控件的每个页面都有一个f:PageManager控件,它包含属性:AutoSizePanelID,设 ...
- Android之消息机制Handler,Looper,Message解析
PS:由于感冒原因,本篇写的有点没有主干,大家凑合看吧.. 学习内容: 1.MessageQueue,Looper,MessageQueue的作用. 2.子线程向主线程中发送消息 3.主线程向子线程中 ...
- Java 枚举用法详解
概念 enum 的全称为 enumeration, 是 JDK 1.5 中引入的新特性. 在Java中,被 enum 关键字修饰的类型就是枚举类型.形式如下: enum Color { RED, GR ...
- WCF学习系列三--【WCF Interview Questions – Part 3 翻译系列】
http://www.topwcftutorials.net/2012/10/wcf-faqs-part3.html WCF Interview Questions – Part 3 This WCF ...