Resource Owner Flow Authorization Example

This examples below provide code for getting an access token using the Resource Owner flow.

In this Topic Hide

  1. Prerequisites
  2. Code Examples
    1. C#
    2. Powershell

Prerequisites

Code Examples

C#

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace HotDocsAdvanceApiExamples {     public class ResourceOwnerAuthorization     {         // These are the credentials required for retrieving an access token using the resource owner flow.         // You will replace these credentials with those for a client you create yourself,         // using the Advance Client Management Application. See the API help guide for more information.         private string clientName = "ab3e4544-b595-4681-b0ea-c7c18c9b63f6";         private string servicePrincipalName = "ExampleServicePrincipalA";         private string servicePrincipalPassword = "examplePassword";
        public async Task<string> GetResourceOwnerToken()         {             // Create the access token request                         var request = CreateHttpRequestMessage();
            //Send token request to Advance             using (var client = new HttpClient())             {                 var response = await client.SendAsync(request);
                // Return the retrieved access token                 return response.Content.ReadAsStringAsync().Result;             }         }
        private HttpRequestMessage CreateHttpRequestMessage()         {             var tokenUrl = string.Format("https://yourtenancy.yourorganization.com/HdaAuth/Authorize/ServicePrincipalLogIn");
            return new HttpRequestMessage             {                 RequestUri = new Uri(tokenUrl),                 Method = HttpMethod.Post,                 Content = GetRequestContent()             };         }
        private StringContent GetRequestContent()         {             var json = "{" + string.Format("'ClientName' : '{0}', 'PrincipalName' : '{1}', 'PrincipalPassword' : '{2}'", clientName, servicePrincipalName, servicePrincipalPassword) + "}";             return new StringContent(json, Encoding.UTF8, "application/json");         }     } }

Powershell

$body = @{
    ClientName = "ExampleClientA"
    PrincipalName = "ExampleServicePrincipalA"
    PrincipalPassword = "examplePassword"
} | ConvertTo-Json
$header = @{  "Accept"="application/json"  "Content-Type"="application/json" }
$result = Invoke-RestMethod -Uri "https://yourtenancy.yourorganization.com/HdaAuth/Authorize/ServicePrincipalLogIn" -Method 'Post' -Body $body -Headers $header
Write-Host $result