最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

ViewandDataAPItips:缓存AccessToken

来源:动视网 责编:小采 时间:2020-11-09 07:44:09
文档

ViewandDataAPItips:缓存AccessToken

ViewandDataAPItips:缓存AccessToken:对于云API服务,常见的方式就是按照API调用次数收费,某些API调用也就有某些限制,比如在特定时间内只允许调用指定的次数以免造成滥用。虽然Autodesk的view and Data API目前还没有应用这样的限制,但我们最好也能实现这样的机制,比如对于或者Access
推荐度:
导读ViewandDataAPItips:缓存AccessToken:对于云API服务,常见的方式就是按照API调用次数收费,某些API调用也就有某些限制,比如在特定时间内只允许调用指定的次数以免造成滥用。虽然Autodesk的view and Data API目前还没有应用这样的限制,但我们最好也能实现这样的机制,比如对于或者Access


对于云API服务,常见的方式就是按照API调用次数收费,某些API调用也就有某些限制,比如在特定时间内只允许调用指定的次数以免造成滥用。虽然Autodesk的view and Data API目前还没有应用这样的限制,但我们最好也能实现这样的机制,比如对于或者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 resp = m_client.Execute(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中。

文档

ViewandDataAPItips:缓存AccessToken

ViewandDataAPItips:缓存AccessToken:对于云API服务,常见的方式就是按照API调用次数收费,某些API调用也就有某些限制,比如在特定时间内只允许调用指定的次数以免造成滥用。虽然Autodesk的view and Data API目前还没有应用这样的限制,但我们最好也能实现这样的机制,比如对于或者Access
推荐度:
标签: 缓存 and API
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top