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开发过程中,经常会用到的就是改变某些元素的颜色已区别显示.比如根据某些属性做不同颜色的专题显示,或者用不同颜色表示施工进度,或者只是简单的以颜色变化来提醒用 ...
随机推荐
- android知识点随笔
android project中Manifest.xml中的<uses-sdk>标签元素决定的. 此标签包含如下3个属性: android:minSdkVersion —— 此属性决定你的 ...
- Unity 文件读取
存储: 在程序发布后文件的存放有两种,第一种是打包到Uniyt的资源包中(*.unity3D),第二种就是把文件存放在一个特殊的目录如:StreamingAssets,这个目录的东西Unity不会打包 ...
- ELF文件
ELF文件格式是一个开发标准,各种UNIX系统的可执行文件都采用ELF格式,它有三种不同的类型: 可重定位的目标文件 可执行文件 共享库 现在分析一下上一篇文章中经过汇编之后生成的目标文件max.o和 ...
- MVC4做网站Demo进行重写的问题。
自从学习MVC4开始,边学边写这个demo,写了也有一年多了.开始觉得是一个小例子把所有的代码都写在一个项目中,边写边改越写越混乱,越到后来很多东西自己都理不清了.后来在群里跟 @怒放 在讨论这个问题 ...
- Android 之 ProgressDialog用法介绍
布局文件测试: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" androi ...
- 配置 DHCP 服务 - 每天5分钟玩转 OpenStack(89)
前面章节我们看到 instance 在启动过程中能够从 Neutron 的 DHCP 服务获得 IP,本节将详细讨论其内部实现机制. Neutron 提供 DHCP 服务的组件是 DHCP agent ...
- innerHTML与innerText的异同
在一道面试题中看到的. 1.功能讲解: innerHTML 设置或获取位于对象起始和结束标签内的 HTML outerHTML 设置或获取对象及其内容的 HTML 形式 innerText 设置或获取 ...
- IDDD 实现领域驱动设计-一个简单的 CQRS 示例
上一篇:<IDDD 实现领域驱动设计-CQRS(命令查询职责分离)和 EDA(事件驱动架构)> 学习架构知识,需要有一些功底和经验,要不然你会和我一样吃力,CQRS.EDA.ES.Saga ...
- [c++] Operator overloading
c++的操蛋属性:自己为一档,空一档,其他随意. UB_stack a; UB_stack b = a; // copy auto c = a; auto d {a}; // (or auto d = ...
- php后管理分类导航菜单
<!DOCTYPE> <html> <head> <meta http-equiv="Content-type" content=&quo ...