我有一个问题,我现在不明白。
我有一个不同租户100+的列表,这个列表一直在增长。对于每个租户,我想要自动创建一个应用程序注册,它允许访问某些特定的api权限。
当我第一次登录到一个天蓝色的门户(每个租户)时,我有以下信息: UserName (租户所有者.)UserPassword (房主.)
这意味着我有所有的相关权利,能够创建一个应用程序注册。
我遇到的问题是让这个自动基于脚本。
当我手动这样做时,我可以在浏览器上看到使用图形API创建应用程序注册。
端点:
标头:应用程序/json+ Bearer
桩身
{
"displayName": "TestAccount12345678",
"spa": {
"redirectUris": []
},
"publicClient": {
"redirectUris": []
},
"web": {
"redirectUris": []
},
"signInAudience": "AzureADMyOrg",
"requiredResourceAccess": [
{
"resourceAppId": "00000003-0000-0000-c000-000000000000",
"resourceAccess": [
{
"id": "e1fe6dd8-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"type": "Scope"
}
]
}
]
}到目前为止,我已经尝试过这样做:我的代码:
图书馆:
import * as msRestNodeAuth from '@azure/ms-rest-nodeauth';
import { ResourceManagementClient } from '@azure/arm-resources'; let creds = await msRestNodeAuth.loginWithUsernamePassword(config.ServiceAccount, config.ServicePassword);
const ressourceCli = new ResourceManagementClient(creds, config.SubScriptionID);
let exists = await ressourceCli.resourceGroups.checkExistence(config.ResGrp);
let accessToken = await creds.getToken();
console.log(accessToken);
let body = {
displayName: 'TestAppRegistration',
spa: { redirectUris: [] },
publicClient: { redirectUris: [] },
web: { redirectUris: [] },
signInAudience: 'AzureADMyOrg',
requiredResourceAccess: [{ resourceAppId: '00000003-0000-0000-c000-000000000000', resourceAccess: [{ id: 'e1fe6dd8-xxxx-xxxx-xxxx-xxxxxxxxxxxx', type: 'Scope' }] }],
};
let rest = await fetch('https://graph.microsoft.com/v1.0/myorganization/applications', {
method: 'POST',
body: JSON.stringify(body),
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${accessToken.accessToken}` },
});当我运行这段代码时,我得到一个401错误,即承载令牌无效,作为一个快速说明,变量exist可以访问azure,因为它正确地告诉我是否存在随机资源grp。
如果我在手动创建注册时检查浏览器,并从那里对承载令牌进行硬编码,那么运行此代码和创建应用程序注册就没有问题。
我还试着使用这个库:
import { Client, ClientOptions } from '@microsoft/microsoft-graph-client';但无论我如何转换,问题似乎是,要验证到图形API,我需要一个应用程序注册.我需要一个应用程序注册来创建应用程序注册..。我试图根据我的用户名和密码来做这件事。
另外,我成功地在powershell上运行了这个程序:但我真的不想运行powershell来做到这一点。
$connectionCustomer = Connect-AzAccount
$customerContext = Get-AzContext
Set-AzContext -Context $customerContext
AzureADApp = New-AzADApplication -DisplayName $createNewAppName这里有我遗漏的东西吗?
提前感谢!
发布于 2021-05-18 20:51:43
大家好,谢谢大家的反馈。
我已经使用了一个多租户应用程序,它给了我正确的权限,我的目的是做一些管理,访问图形api,就这样,它已经完成了。
代码片段:
const msalConfig: msal.Configuration = {
auth: {
clientId: AdminClientID,
authority: `https://login.microsoftonline.com/${adalToken.tenantId}`,
clientSecret: 'secret',
},
};
let pca = new msal.PublicClientApplication(msalConfig);
// see https://learn.microsoft.com/en-us/azure/active-directory/develop/msal-authentication-flows#device-code for alternative code flows
let tokenObj = await pca.acquireTokenByUsernamePassword({ scopes: [], password: config.CustomerPassword, username: config.CustomerAccount });
if (!tokenObj) {
return;
}
let action: IManager = new AzureAppRegistrationManager(tokenObj.accessToken);
await action.verifyAndCreate();https://stackoverflow.com/questions/67515451
复制相似问题