APIs & Integrations

GLopez13
Participant

Get "PERMISSIONS SET" or "ACCESS" from private app code

SOLVE

Hello, 

Currently I am developping a private app using a React repo, so I am able to get the "Teams" from a user using the "CrmContext", doing something like:

ctx.user.teams

My question is... Is it possible to get the "PERMISSIONS SET" or the "ACCESS" this user has? Guess the CrmContext does not have these options.

Thanks in advance!

0 Upvotes
1 Accepted solution
zach_threadint
Solution
Guide

Get "PERMISSIONS SET" or "ACCESS" from private app code

SOLVE

Hi @GLopez13 👋

 

Here are the Node examples supplied in the relevant HubSpot Developer docs (1. User Details, 2. User Provisioning😞

 

1. Read User

Note that this example doesn't seem to use HubSpot's Node client library, but rather interacts with the HubSpot API via the "https" package.

var http = require("https");

var options = {
  "method": "GET",
  "hostname": "api.hubapi.com",
  "port": null,
  "path": "/crm/v3/objects/users/userId?archived=false",
  "headers": {
    "accept": "application/json",
    "authorization": "Bearer YOUR_ACCESS_TOKEN"
  }
};

var req = http.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });
});

req.end();

 2. Retrieve the roles on an account

const hubspot = require('@hubspot/api-client');

const hubspotClient = new hubspot.Client({"accessToken":"YOUR_ACCESS_TOKEN"});

try {
  const apiResponse = await hubspotClient.settings.users.rolesApi.getAll();
  console.log(JSON.stringify(apiResponse, null, 2));
} catch (e) {
  e.message === 'HTTP request failed'
    ? console.error(JSON.stringify(e.response, null, 2))
    : console.error(e)
}

 I haven't tested these examples -- I simply copied and pasted directly from HubSpot API docs. Sometimes the examples supplied by HubSpot don't work, so you might need to perform a little trial and error.

 

I hope that proves helpful. Please let me know if you have any questions.

All the best,

Zach

--

Zach Klein
HubSpot Integrations & App Developer
Meanjin / Brisbane, Australia



Say g'day


If my post helped answer your query, please consider marking it as a solution.


View solution in original post

0 Upvotes
4 Replies 4
zach_threadint
Guide

Get "PERMISSIONS SET" or "ACCESS" from private app code

SOLVE

Hi @GLopez13 👋

 

It's possible to retrieve the "Permission Set(s)" assigned to individual HubSpot Users via API. The User property that stores this data is "hs_assigned_permission_sets". For example, when retrieving an individual HubSpot User:

curl --location 'https://api.hubapi.com/crm/v3/objects/users/:USER_ID?properties=hs_assigned_permission_sets' \
--header 'Authorization: Bearer pat-redacted'

 The "hs_assigned_permission_sets" property contains User Permission Set (or "Role") IDs. You can match up your HubSpot Portal's Permission Set IDs and Labels via the following call:

curl --location 'https://api.hubapi.com/settings/v3/users/roles' \
--header 'Authorization: Bearer pat-redcated'

I hope that proves helpful. Please let me know if you have any follow up questions.

All the best,

Zach

--

Zach Klein
HubSpot Integrations & App Developer
Meanjin / Brisbane, Australia



Say g'day


If my post helped answer your query, please consider marking it as a solution.


0 Upvotes
GLopez13
Participant

Get "PERMISSIONS SET" or "ACCESS" from private app code

SOLVE

Hello @zach_threadint  :),

Thank you very much for your response, that approach could help, just for curiosity, do you know if there is a way to do it using the SDK? I mean using some component of the "

@hubspot/ui-extensions"... currently using the SDK, so maybe could avoid the api requests 😅
 
Thank you very much in advance! 😁
0 Upvotes
zach_threadint
Solution
Guide

Get "PERMISSIONS SET" or "ACCESS" from private app code

SOLVE

Hi @GLopez13 👋

 

Here are the Node examples supplied in the relevant HubSpot Developer docs (1. User Details, 2. User Provisioning😞

 

1. Read User

Note that this example doesn't seem to use HubSpot's Node client library, but rather interacts with the HubSpot API via the "https" package.

var http = require("https");

var options = {
  "method": "GET",
  "hostname": "api.hubapi.com",
  "port": null,
  "path": "/crm/v3/objects/users/userId?archived=false",
  "headers": {
    "accept": "application/json",
    "authorization": "Bearer YOUR_ACCESS_TOKEN"
  }
};

var req = http.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });
});

req.end();

 2. Retrieve the roles on an account

const hubspot = require('@hubspot/api-client');

const hubspotClient = new hubspot.Client({"accessToken":"YOUR_ACCESS_TOKEN"});

try {
  const apiResponse = await hubspotClient.settings.users.rolesApi.getAll();
  console.log(JSON.stringify(apiResponse, null, 2));
} catch (e) {
  e.message === 'HTTP request failed'
    ? console.error(JSON.stringify(e.response, null, 2))
    : console.error(e)
}

 I haven't tested these examples -- I simply copied and pasted directly from HubSpot API docs. Sometimes the examples supplied by HubSpot don't work, so you might need to perform a little trial and error.

 

I hope that proves helpful. Please let me know if you have any questions.

All the best,

Zach

--

Zach Klein
HubSpot Integrations & App Developer
Meanjin / Brisbane, Australia



Say g'day


If my post helped answer your query, please consider marking it as a solution.


0 Upvotes
GLopez13
Participant

Get "PERMISSIONS SET" or "ACCESS" from private app code

SOLVE

Hi @zach_threadint ,

your approach helped me a lot! Just had to use 

hubspotClient.settings.users.usersApi.getById(userId) api to get the "user role id (here the role id is the permission set id the user has and only one is possible),then get all the "roles" (permissions sets) using 
hubspotClient.settings.users.rolesApi.getAll() api. Finally, check if the roleId the user has match with some role in the roles list (to gets its name).

Thank you very much!

Regards,
Gabriel Lopez.