在 Nodejs 的 Azure AD 中创建 AD 安全组
Creating a AD Security group in Azure AD in Nodejs
我正在尝试在我的 azure AD 目录中创建一个安全组,并将 b2c 用户(已登录)添加到该组。
<pre><code>msRestAzure.loginWithServicePrincipalSecret('72a3198b-', '-FvUZZ4G', tenantId, { tokenAudience: 'graph' }, function (err, credentials, subscriptions) {
if (err) console.log("error: " + err);
else {
console.log(util.inspect(credentials));
}
var client = new graphRbacManagementClient(credentials, tenantId);
client.groups.list({}, function(err, result){
if(err){
console.log('Could not list groups', err)
}
else {
console.log("result: " + result);
}
})
我在 Azure 上启用了 API 权限....
虽然我使用我的 SP 成功进行了身份验证,但无论我尝试创建新组还是列出现有组等,我都会收到 403 响应。
关于我在这里做错了什么有什么想法吗?
更新:我更改了代码并改为使用 msRestAzure.loginWithUsernamePassword
。我使用的用户名和密码是针对具有管理员权限的用户。更改后,代码有效并且 created/Listed 该组。所以我的猜测是,SP 需要某种我尚未启用的 Access/API 权限。只是不知道是什么..
graphRbacManagementClient
不支持调用MS图api。它用于 Azure AD Graph API.
这是您可以关注的 code sample。它使用 microsoft-graph-client 调用 Microsoft Graph。
使用客户端凭据流程获取访问令牌:
const request = require("request");
const endpoint = "https://login.microsoftonline.com/[Tenant]/oauth2/v2.0/token";
const requestParams = {
grant_type: "client_credentials",
client_id: "[ApplicationID]",
client_secret: "[Key]",
scope: "https://graph.microsoft.com/.default"
};
request.post({ url:endpoint, form: requestParams }, function (err, response, body) {
if (err) {
console.log("error");
}
else {
console.log("Body=" + body);
let parsedBody = JSON.parse(body);
if (parsedBody.error_description) {
console.log("Error=" + parsedBody.error_description);
}
else {
console.log("Access Token=" + parsedBody.access_token);
}
}
});
调用 MS 图表 API:
// list groups
function testListGroupGraphAPI(accessToken) {
request.get({
url:"https://graph.microsoft.com/v1.0/groups",
headers: {
"Authorization": "Bearer " + accessToken
}
}, function(err, response, body) {
console.log(body);
});
}
// create group
function testCreateGroupGraphAPI(accessToken) {
request.post({
url:"https://graph.microsoft.com/v1.0/groups",
headers: {
"Authorization": "Bearer " + accessToken
},
json: {
"description": "Self help community for library",
"displayName": "Library Assist",
"groupTypes": [
"Unified"
],
"mailEnabled": true,
"mailNickname": "library",
"securityEnabled": false
}
}, function(err, response, body) {
console.log(body);
});
}
有关详细信息,请参阅 。
Azure AD Graph的权限在这里:
我正在尝试在我的 azure AD 目录中创建一个安全组,并将 b2c 用户(已登录)添加到该组。
<pre><code>msRestAzure.loginWithServicePrincipalSecret('72a3198b-', '-FvUZZ4G', tenantId, { tokenAudience: 'graph' }, function (err, credentials, subscriptions) {
if (err) console.log("error: " + err);
else {
console.log(util.inspect(credentials));
}
var client = new graphRbacManagementClient(credentials, tenantId);
client.groups.list({}, function(err, result){
if(err){
console.log('Could not list groups', err)
}
else {
console.log("result: " + result);
}
})
我在 Azure 上启用了 API 权限....
虽然我使用我的 SP 成功进行了身份验证,但无论我尝试创建新组还是列出现有组等,我都会收到 403 响应。
关于我在这里做错了什么有什么想法吗?
更新:我更改了代码并改为使用 msRestAzure.loginWithUsernamePassword
。我使用的用户名和密码是针对具有管理员权限的用户。更改后,代码有效并且 created/Listed 该组。所以我的猜测是,SP 需要某种我尚未启用的 Access/API 权限。只是不知道是什么..
graphRbacManagementClient
不支持调用MS图api。它用于 Azure AD Graph API.
这是您可以关注的 code sample。它使用 microsoft-graph-client 调用 Microsoft Graph。
使用客户端凭据流程获取访问令牌:
const request = require("request");
const endpoint = "https://login.microsoftonline.com/[Tenant]/oauth2/v2.0/token";
const requestParams = {
grant_type: "client_credentials",
client_id: "[ApplicationID]",
client_secret: "[Key]",
scope: "https://graph.microsoft.com/.default"
};
request.post({ url:endpoint, form: requestParams }, function (err, response, body) {
if (err) {
console.log("error");
}
else {
console.log("Body=" + body);
let parsedBody = JSON.parse(body);
if (parsedBody.error_description) {
console.log("Error=" + parsedBody.error_description);
}
else {
console.log("Access Token=" + parsedBody.access_token);
}
}
});
调用 MS 图表 API:
// list groups
function testListGroupGraphAPI(accessToken) {
request.get({
url:"https://graph.microsoft.com/v1.0/groups",
headers: {
"Authorization": "Bearer " + accessToken
}
}, function(err, response, body) {
console.log(body);
});
}
// create group
function testCreateGroupGraphAPI(accessToken) {
request.post({
url:"https://graph.microsoft.com/v1.0/groups",
headers: {
"Authorization": "Bearer " + accessToken
},
json: {
"description": "Self help community for library",
"displayName": "Library Assist",
"groupTypes": [
"Unified"
],
"mailEnabled": true,
"mailNickname": "library",
"securityEnabled": false
}
}, function(err, response, body) {
console.log(body);
});
}
有关详细信息,请参阅
Azure AD Graph的权限在这里: