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.
This article will focus on the Windows OS. If you wish to achieve this on a MacOS, please navigate to the below link:
https://modthemachine.typepad.com/my_weblog/2015/10/use-entitlement-api-from-fusion.html
The Entitlement API is a simple RESTful API where you just need to send an HTTP GET request to the App Store server.
Since the Fusion API supports both C++ and Python, this article shall be covering both, should the need arise.
1. C++:
There are many library options that you could choose to make an HTTP request using C++. For this example, I installed this RestClient (which is a wrapper for the cURL library) and installed it using vcpkg.
Tip: When using vcpkg, it usually defaults to 32-bit platform. To set it to a 64-bit platform, you can set the environment variable as below:
VCPKG_DEFAULT_TRIPLET=x64-windows
I have also used this JsonCPP library to help parse the HTTP response.
Below are the steps that I needed to follow for implementation:
> Create a C++ Fusion add-in and edit it so it opens in Visual Studio. For detailed steps on how to create an add-in please refer: Creating a Script or Add-In.
> In Visual Studio, navigate to Project –> Properties -> Configuration Properties -> VC++ Directories -> Include directories.
Here add the full path to the Include directories where you have installed the libraries. For e.g.
C:\vcpkg\packages\restclient-cpp_x64-windows\include
C:\vcpkg\packages\jsoncpp_x64-windows\include
Here, add the full path to the Lib folder. For e.g.
C:\vcpkg\packages\restclient-cpp_x64-windows\lib
C:\vcpkg\packages\jsoncpp_x64-windows\lib
> Now navigate to Linker > Input > Additional Dependencies, and add the full path to the required lib files:
C:\vcpkg\packages\restclient-cpp_x64-windows\lib\restclient-cpp.lib
C:\vcpkg\packages\jsoncpp_x64-windows\lib\jsoncpp.lib
> Now add the following statements at the top of the CPP file.
#include <restclient-cpp/restclient.h>
#include <json/json.h>
using namespace std;
Followed by:
extern "C" XI_EXPORT bool run(const char* context)
{
try
{
app = Application::get();
if (!app)
return false;ui = app->userInterface();
if (!ui)
return false;string userId = app->userId();
string appId = "1006119760063675415"; // App ID
string url ="https://apps.exchange.autodesk.com/webservices/checkentitlement"
+ string("?userid=") + userId + string("&appid=") + appId;
RestClient::Response response = RestClient::get(url);
Json::Reader reader;
Json::Value root;bool isparseSuccessful = reader.parse(response.body, root);
if (!isparseSuccessful)
{
ui->messageBox("Error parsing the string");
}
if ((root["IsValid"].asString()) == "true")
{
ui->messageBox("IsValid is True");
}
else
{
ui->messageBox("IsValid is False");
}catch (exception ex)
{
}
return true;
}
> 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 in the appId variable used in the above code.
Now based on whether the app has been downloaded or not, you should either see:
2. Python:
To achieve the same using Python, download the Requests module. I have also used the built-in JSON package to help parse the HTTP response.
Add the below lines to import the packages at the top of your .py file.
from .modules import requests
import json
Followed by:
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
userId = app.userId
appId ="1006119760063675415"
url = "https://apps.exchange.autodesk.com/webservices/checkentitlement" +
"?userid=" + userId + "&appid=" + appId
r = requests.get(url)
resp_dict = json.loads(r.text)
val = resp_dict.get('IsValid')
if val:
ui.messageBox("IsValid is True")
else:
ui.messageBox("IsValid is False")
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
You can find the full source code here:
https://github.com/sajith-subramanian/EntitlementAPI-for-Fusion-360