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开发过程中,经常会用到的就是改变某些元素的颜色已区别显示.比如根据某些属性做不同颜色的专题显示,或者用不同颜色表示施工进度,或者只是简单的以颜色变化来提醒用 ...
随机推荐
- Mesh Algorithm in OpenCascade
Mesh Algorithm in OpenCascade eryar@163.com Abstract. Rendering a generic surface is a two steps pro ...
- Android数据存储之SharedPreferences及如何安全存储
前言: 最近一直在学习ios的数据存储,当学习到NSUserDefaults的时候让我回想起了SharedPreferences,今天闲来无事,想着总结一下SharedPreferences的使用. ...
- Angular.js Services
Angular带来了很多类型的services.每个都会它自己不同的使用场景.我们将在本节来阐述. 首先我们必须记在心里的是所有的services都是singleton(单例)的,这也是我们所希望得到 ...
- multipart数据结构
--[boundary]\r\n [headers]\r\n \r\n [content]\r\n --[boundary]\r\n [headers]\r\n \r\n [content]\r\n ...
- java中hashMap的排序
hashMap排序,示例: private void test(){ Map<String, List<String>> unSupportedDatesMap=new Has ...
- C语言版flappy bird黑白框游戏
在此记录下本人在大一暑假,2014.6~8这段时间复习C语言,随手编的一个模仿之前很火热的小游戏----flappy bird.代码bug基本被我找光了,如果有哪位兄弟找到其他的就帮我留言下吧,谢谢了 ...
- C/C++:提升_指针的指针和指针的引用
C/C++:提升_指针的指针和指针的引用 写在前面 今天在使用指针的时候我发现了一个自己的错误.
- Linux添加用户(user)到用户组(group)
将一个用户添加到用户组中,千万不能直接用: usermod -G groupA 这样做会使你离开其他用户组,仅仅做为 这个用户组 groupA 的成员. 应该用 加上 -a 选项: usermod - ...
- 【Java心得总结五】Java容器上——容器初探
在数学中我们有集合的概念,所谓的一个集合,就是将数个对象归类而分成为一个或数个形态各异的大小整体. 一般来讲,集合是具有某种特性的事物的整体,或是一些确认对象的汇集.构成集合的事物或对象称作元素或是成 ...
- 整合struts2+hibernate详细配置步骤及注意事项
刚刚学完这两个框架,就迫不及待的做了一个例子,在整合两个框架的时候,也碰到了一些小问题,下面介绍一下配置的步骤: 1.创建一个自定义的struts2和hibernate的类库 因为之前写例子都是直接将 ...