View and Data API tips: Caching access Tokens

Source: Internet
Author: User

For cloud API services, the common approach is to charge for API calls, and some API calls have some limitations, such as allowing only the specified number of times to be invoked to avoid abuse at a given time. Although the view and Data API for Autodesk is not currently applied, it is best to implement such a mechanism, such as for operations such as access token, where an access token has a certain validity period, Within the validity period of this Token, there is no need to repeat the API call to get the new acces token, only to return the token that is still valid. Here's a simple logic for C # implementation that caches access tokens with a global static variable:

 PublicclassUtil
{
PrivateStaticReadOnlyILogLogger =Logmanager. GetLogger (typeof(Util));

stringBASEURL ="";
restclientM_client;


PublicStaticAccesstokenToken
PublicStatic DateTimeIssuedatetime;
//refresh Token If the token is about-expire in 5 seconds
PublicStaticintAbout_expired_seconds = 5;


PublicUtil (stringBASEURL)
{
This. baseUrl = BASEURL;
M_client =Newrestclient(BASEURL);
}

PublicAccesstokenGetaccesstoken (stringClientId,stringClientsecret)
{
//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))
{
restrequestreq =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 the if you just need to get access tokens 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)
{
AccesstokenAR = 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, with the saved access token in static Var
}

returnToken
}


}

Of course, depending on your needs, you can choose other ways, such as storing tokens in a database, or memcache.

View and Data API tips: Caching access Tokens

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.