Entitlement API for Revit Exchange Store Apps

Revit 2016 has added two properties to help Exchange Store app publishers check if the current user has purchased the given app or not. Those two properties are:

  • Application.IsLoggedIn
  • Application.LoginUserId

IsLoggedIn checks if the user is logged in to his/her Autodesk account from the current Revit session. LoginUserId returns the user identifier of the Autodesk account that the user is currently logged in.

Note that the user identifier returned here is an internal presentation of the user and it has human unrecognizable form. It’s not the same as the User ID that the user actually types into the login dialog.

These properties are used to get the user identifier, which is in turn used in the Autodesk Exchange Store Entitlement API, a single REST call to the web service. A publisher of an Exchange Store app can verify if the current user has purchased their app from the store.

For the Entitlement API side, Daniel Du has written a blog post about the Entitlement API. Please refer to it for a more detailed explanation.

I made a small sample to demonstrate the usage in Revit 2016 and copy it below. You can also download the code from here: Download EntitlementAPIRevit

In the sample code, I’m using a library called RestSharp to take care of the REST client call. If you are not familiar with the RestSharp, you may want to take a look at my another blog; the first part of the blog explains about how to use RestSharp in Visual Studio. For more detail about RestSharp, please refer to RestSharp.org.


// Entitlement API in Revit

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net; // for HttpStatusCode
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
// Added for REST API
// We are using C# REST library called RestShap
// See http://restsharp.org/ for detail
// using RestSharp; 
using RestSharp.Deserializers;

/// 
/// Revit 2016 has added two methods to help exchange store app publishers 
/// to check a store app entitlement, i.e., to check if the user has purchase or not. 
/// This is a minimum sample to show the usage.
/// 
namespace EntitlementAPIRevit 
{
    [Transaction(TransactionMode.Automatic)]
    public class EntitlementAPI : IExternalCommand
    {
        // Set values specific to the environment
        public const string _baseApiUrl = @"https://apps.exchange.autodesk.com/";
        
        // This is the id of your app.
        // e.g., public const string _appId = @"appstore.exchange.autodesk.com:TransTips-for-Revit:en";
        public const string _appId = @"";
        
        // Command to check an entitlement
        public Autodesk.Revit.UI.Result Execute(
            ExternalCommandData commandData,
            ref string message,
            Autodesk.Revit.DB.ElementSet elements)
        {
            // Get hold of the top elements
            UIApplication uiApp = commandData.Application;
            Application rvtApp = uiApp.Application;
            
            // Check to see if the user is logged in.
            if(!rvtApp.IsLoggedIn) 
            {
                TaskDialog.Show("Entitlement API", "Please login to Autodesk 360 first\n");
                return Result.Failed;
            }
            
            // Get the user id, and check entitlement
            string userId = rvtApp.LoginUserId;
            bool isValid = CheckEntitlement(_appId, userId);
            
            if (isValid)
            {
                // The user has a valid entitlement, i.e.,
                // if paid app, purchase the app from the store.
            }
            
            // For now, display the result
            string msg = "userId = " + userId
                + "\nappId = " + _appId
                + "\nisValid = " + isValid.ToString();
            
            TaskDialog.Show("Entitlement API", msg);
            return Result.Succeeded;
        }
        
        ///========================================================
        /// URL: https://apps.exchange.autodesk.com/webservices/checkentitlement
        /// Method: GET
        /// Parameter:
        ///   userid
        ///   appid
        ///   
        /// Sample response
        /// {
        /// "UserId":"2N5FMZW9CCED",
        /// "AppId":"appstore.exchange.autodesk.com:autodesk360:en",
        /// "IsValid":false,
        /// "Message":"Ok"
        /// }
        /// ========================================================
        private bool CheckEntitlement(string appId, string userId)
        {
            // REST API call for the entitlement API.
            // We are using RestSharp for simplicity.
            // You may choose to use other library.
            
            // (1) Build request
            var client = new RestClient();
            client.BaseUrl = new System.Uri(_baseApiUrl);
            
            // Set resource/end point
            var request = new RestRequest();
            request.Resource = "webservices/checkentitlement";
            request.Method = Method.GET;
            
            // Add parameters
            request.AddParameter("userid", userId);
            request.AddParameter("appid", appId);
            
            // (2) Execute request and get response
            IRestResponse response = client.Execute(request);
            
            // (3) Parse the response and get the value of IsValid.
            bool isValid = false;
            
            if (response.StatusCode == HttpStatusCode.OK)
            {
                JsonDeserializer deserial = new JsonDeserializer();
                EntitlementResponse entitlementResponse = deserial.Deserialize(response);
                isValid = entitlementResponse.IsValid;
            }
            
            return isValid;
        }
        
        [Serializable]
        public class EntitlementResponse
        {
            public string UserId { get; set; }
            public string AppId { get; set; }
            public bool IsValid { get; set; }
            public string Message { get; set; }
        }
    }
}

To run this, please modify the app id to match your app id.

Mikako


Comments

10 responses to “Entitlement API for Revit Exchange Store Apps”

  1. How will this work (can it?) if a user buy multiple license for their organisation?

  2. Virupaksha Aithal Avatar
    Virupaksha Aithal

    Hi,
    The user (who has downloaded multiple license) can distribute the license. soon, we will make the document on how to distribute the license available in http://www.autodesk.com/developapps. For now, i am sending a draft copy of the same document to you (to your email).
    Thanks
    Viru

  3. Thanks, Viru. Looking forward to the document coming out soon.
    Russ, very good question! Thank you for raising.

  4. Perfect.

  5. Hi, can you give me a sample document to make license for add-in revit.
    my email: hienngocloveyou@yahoo.com
    Thanks

  6. Hi,
    Please refer http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=24243865 for documentation on Entitlement API.
    Thanks
    Viru

  7. Thanks for writing such an informative blog.

  8. Was this document ever uploaded to the website? I am searching for it in vain now in 2022..

  9. Mark Landis Avatar
    Mark Landis

    I am struggling with this problem. Trying to implement Entitlement into my addin in 2022. I want to verify entitlement on startup. In my app, I run a Addin Type “Application” which adds a ribbon tab and panel with buttons to execute each command.
    Thanks

  10. Mark Landis Avatar
    Mark Landis

    Have you figured this out yet? I can’t!

Leave a Reply to RussCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading