If you have an app on the Autodesk App Store then you might want to check if the user actually paid for and downloaded your app from the store or just copied it from someone else's computer. This what the Entitlement API can help you with.
In the September release the Fusion API exposed two new user specific properties under the Application object: userName and userId. The latter is needed to take advantage of the Entitlement API.
Probably it makes most sense to use this API from a C++ add-in since that is much better protected because of compilation. So that's what this article will focus on.
The Entitlement API is a simple RESTful API where you just need to send an HTTP GET request to the App Store server to find out what you need. It's up to you what library you use for that as there are many options. I was looking into using a cross-platform library since Fusion is supporting both Mac and Windows, however the ones I tried were not straight-forward to use at all. In the end I decided to go with the cURL library which is really simple to use from a MacOS add-in.
The steps I needed to take:
1) Create a new C++ Fusion add-in:
2) Add cURL support to your add-in:
Once you're in Xcode, go into the Project settings and under "Build Settings >> Linking >> Other Linker Flags" add "-lcurl"
3) Add the following statements at the top of the C++ file:
#include <curl/curl.h> #include <regex> using namespace std;
4) Also add this code which is using the Regular Expressions library:
(there are quite a few RegEx testers on the web that you can use - I played with this: https://www.myregextester.com/index.php)
// Fetch the value of a given property // inside a json text string GetValue(string text, string find) { regex IsValid("\\s*\"" + find + "\"\\s*:\\s*(.+?)[\\s,]"); smatch m; if (regex_search(text, m, IsValid)) { if (m.size() > 1) return m[1]; } return ""; } size_t callback_func(void *ptr, size_t size, size_t count, void *stream) { // ptr - your string variable. // stream - data chunck you received string reply((char*)ptr); string ret = GetValue(reply, "IsValid"); // If the app is not valid if (ret != "true") ui->messageBox("IsValid = false"); else ui->messageBox("IsValid = true"); return 0; } // Using Entitlement API to check if the // app usage is valid void CheckValidity() { // e.g. the URL for Voronoi Sketch generator is: // https://apps.autodesk.com/FUSION/en/Detail/Index?id=appstore.exchange.autodesk.com%3avoronoisketchgenerator_macos%3aen // This cotains the "id": // "appstore.exchange.autodesk.com%3avoronoisketchgenerator_macos%3aen" // so we can use that string userId = app->userId(); string userName = app->userName(); string appId = "appstore.exchange.autodesk.com%3aaddinrename_windows32and64%3aen"; string url = string("https://apps.exchange.autodesk.com/webservices/checkentitlement") + string("?userid=") + userId + string("&appid=") + appId; CURL * curl = curl_easy_init(); if(curl) { CURLcode res; curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback_func); res = curl_easy_perform(curl); curl_easy_cleanup(curl); } }
5) Now you can call CheckValidity() from inside your run() function
You can easily figure out the id of your app by finding it on the App Store and then checking the URL in the browser. That will contain an id parameter which is what you need. E.g. the MacOS English version of the Voronoi Sketch Generator has this URL and I highlighted the important part with the id:
https://apps.autodesk.com/FUSION/en/Detail/Index?id=appstore.exchange.autodesk.com%3avoronoisketchgenerator_macos%3aen
Worth noting that the Entitlement API only provides information on if the user paid for your app or not, it does not tell you in case of a Free app if it has been downloaded by the given user from the store or not.
So when I use this functionality with a free app then IsValid will always return false:
But if I test it with a priced app which I paid for then I get true:
Since the Entitlement API is based on simple HTTP requests, therefore you can easily test it in utilities like the RESTClient for Firefox.
You can find the full source code here: https://github.com/AutodeskFusion360/EntitlementAPI
At the moment the sample only contains code for MacOS, but will likely add a Windows version as well later.
-Adam