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