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中。

时间: 2024-08-01 18:48:30

View and Data API tips: 缓存Access Token的相关文章

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 enables web developers to very easily display 3D (and 2D) models on a WebGL-enabled browser. please read this one first and get a key from http://developer

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, a tree structure that represents the model hierarchy. Each element in model can be representing as a node of model tree. Each node has a dbId, this

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 framework, I can talk about this latter. I was inspired by this question on forum and started to looking at the possibilities of using View and Data AP

Autodesk View and Data API用户调查

相信大家已经听过Autodesk View and Data Web Service (API) 了吧,还没有? 下面这个就是用Autodesk View and Data Web Service嵌入的模型哦,先玩儿玩儿吧 :)   你也可以把这个模型嵌入到你自己的网站中. 我在和客户交流的时候,很多人都反应云服务器在国外,中国用户使用上就比较犹豫,那么如果把服务器架设在国内,你会用吗?做一下调查吧,让Autodesk听到你的声音.常言道,会哭的孩子有奶吃,不会哭的孩子被饿死啊,5555~  

Autodesk View and Data API练练手

大家如果参加过我们的活动,你应该已经听过看过不少关于View and Data Web Service的例子里,如果还没有的话,请看看下面这几篇: http://www.cnblogs.com/junqilian/category/594048.html 如果你已经了解了Viewer,那有没有兴趣练练手,把这样酷的三维模型嵌入到你自己的网页中呢?那么开始练练手吧. ?体验代码资料下载:http://pan.baidu.com/s/15zZMQ 在下载解压缩后你应该可以看到下面的目录结构,其中ha

初探物联网 - 基于Arduino的气象站和View and Data API的结合实例

如果你参加了上个月在北京的Autodesk 开发者日,你应该看到了我做的关于Arduino的物联网实例演示,如果你没看到,欢迎参加14号在上海的开发者日,到时候我会再演(xian)示(bai)一下. 这是个基于这样一个场景的简单演示.我的一个建筑物上面安装了这样一个温度传感器,随时把当前环境温度上传到云端,在浏览器端可以显示这个建筑物的三维模型和温度变化曲线图.如果温度到达一定的高温,比如大于40度,那可能是起火了,就需要发出高温报警,在三维模型中定位出报警的温度传感器的位置,并发出火警警报.

使用缓存Memcache存储更新微信access token

关键字:Memcache access_token 更新 存储 7200 本文介绍如何使用缓存Memcache存储及更新 access token的方法. 一.Access Token access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token.正常情况下access_token有效期为7200秒,重复获取将导致上次获取的access_token失效. 公众号可以使用AppID和AppSecret调用本接口来获取access_token.AppID和App

REST API 基于ACCESS TOKEN

REST API 基于ACCESS TOKEN 的权限解决方案 REST 设计原则是statelessness的,而且但客户端是APP时,从APP发起的请求,不是基于bowers,无法带相同的sessionid,所以比较好的方案是每次请求都带一个accesstoken进行验证.然后后台是根据token 找到用户,然后找到用户资源 但总不能每个方法都去调用token验证的方法,也不能每次验证都需要查询数据库吧! 解决办法: 为了业务层只关注业务,所以需要把token验证的方法在进入controll

personal access token 访问 git api

git api存储了很多有用的仓库信息可供大家查阅,而且最重要的是你可以把它爬下来搞一搞,用python可以轻松的做到这一点,然而访问git api的时候会有一个访问限制,没有认证的访问每小时只能进行60次...所以需要认证一下. 比较简单的办法就是在requsets中加入参数,参数为你的client_id 和 client_secret.这里用python演示: response=requsets.get('https://api.github.com/repos/your_name/your