如何获取 Azure 的 Auth Token 以编程方式创建 azure 资源
How to get Auth Token of Azure to create azure resources programmatically
我想创建 azure 资源,但在生成用于创建此类资源的令牌凭据时遇到问题。
下面是创建azure postgres的方法SQL但是我需要通过credentials
但我不知道如何生成该令牌程序。
我需要帮助。
const msRestAzure = require('ms-rest-azure');
const PostgreSQLManagementClient = require('azure-arm-postgresql');
const subscriptionID = '<subscription id>';
const resourceGroup = '<resource group name>';
const serverName = '<server name>'; // must be globally unique
msRestAzure.interactiveLogin().then((credentials) => {
let client = new PostgreSQLManagementClient(credentials, subscriptionID);
return client.servers.createOrUpdate(resourceGroup, serverName, {
location: 'eastus',
properties: {
createMode: 'Default',
administratorLogin: 'postgres',
administratorLoginPassword: 'F00Bar!!'
}
});
}).then((server) => {
console.log('Server:');
console.dir(server, {depth: null, colors: true});
}).catch((err) => {
console.log('An error ocurred');
console.dir(err, {depth: null, colors: true});
});
有谁知道如何生成它?使用什么?我猜这是一个签名令牌,使用客户端 ID 和租户 ID 但是如何生成它。
没有提供以编程方式生成它的文档。有什么办法吗?
interactiveLogin()
将提供 link 和允许用户从浏览器进行身份验证的代码。
以编程方式使用 Node.js SDK 时,有 2 种方法。
1)您可以使用您的 用户名和密码 通过您的 Azure 帐户向 API 进行身份验证。 不推荐,因为它需要对应用程序的高度信任,并且会带来其他流程中不存在的风险。
const Azure = require('azure');
const MsRest = require('ms-rest-azure');
MsRest.loginWithUsernamePassword(process.env.AZURE_USERNAME, process.env.AZURE_PASSWORD, (err, credentials) => {
if (err) throw err;
let client = new PostgreSQLManagementClient(credentials, subscriptionID);
// ...
});
2)您可能希望使用服务主体身份验证,而不是提供您的帐户凭据。它基于没有用户帐户的服务主体。首先,create service principal 在 Azure 门户中。
const Azure = require('azure');
const MsRest = require('ms-rest-azure');
MsRest.loginWithServicePrincipalSecret(
'clientId/appId',
'client-secret',
'tenantId',
(err, credentials) => {
if (err) throw err
let client = new PostgreSQLManagementClient(credentials, subscriptionID);
// ...
}
);
有关使用 ms-rest-azure 库的 JavaScript 身份验证示例的更多详细信息,请参阅 here。
更新:
如果使用服务主体,请尝试添加 Azure OSSRDBMS 数据库应用程序权限。导航到 Azure AD > 你的应用 > API 权限。并记得为您的租户授予管理员许可。
我想创建 azure 资源,但在生成用于创建此类资源的令牌凭据时遇到问题。
下面是创建azure postgres的方法SQL但是我需要通过credentials
但我不知道如何生成该令牌程序。
我需要帮助。
const msRestAzure = require('ms-rest-azure');
const PostgreSQLManagementClient = require('azure-arm-postgresql');
const subscriptionID = '<subscription id>';
const resourceGroup = '<resource group name>';
const serverName = '<server name>'; // must be globally unique
msRestAzure.interactiveLogin().then((credentials) => {
let client = new PostgreSQLManagementClient(credentials, subscriptionID);
return client.servers.createOrUpdate(resourceGroup, serverName, {
location: 'eastus',
properties: {
createMode: 'Default',
administratorLogin: 'postgres',
administratorLoginPassword: 'F00Bar!!'
}
});
}).then((server) => {
console.log('Server:');
console.dir(server, {depth: null, colors: true});
}).catch((err) => {
console.log('An error ocurred');
console.dir(err, {depth: null, colors: true});
});
有谁知道如何生成它?使用什么?我猜这是一个签名令牌,使用客户端 ID 和租户 ID 但是如何生成它。
没有提供以编程方式生成它的文档。有什么办法吗?
interactiveLogin()
将提供 link 和允许用户从浏览器进行身份验证的代码。
以编程方式使用 Node.js SDK 时,有 2 种方法。
1)您可以使用您的 用户名和密码 通过您的 Azure 帐户向 API 进行身份验证。 不推荐,因为它需要对应用程序的高度信任,并且会带来其他流程中不存在的风险。
const Azure = require('azure');
const MsRest = require('ms-rest-azure');
MsRest.loginWithUsernamePassword(process.env.AZURE_USERNAME, process.env.AZURE_PASSWORD, (err, credentials) => {
if (err) throw err;
let client = new PostgreSQLManagementClient(credentials, subscriptionID);
// ...
});
2)您可能希望使用服务主体身份验证,而不是提供您的帐户凭据。它基于没有用户帐户的服务主体。首先,create service principal 在 Azure 门户中。
const Azure = require('azure');
const MsRest = require('ms-rest-azure');
MsRest.loginWithServicePrincipalSecret(
'clientId/appId',
'client-secret',
'tenantId',
(err, credentials) => {
if (err) throw err
let client = new PostgreSQLManagementClient(credentials, subscriptionID);
// ...
}
);
有关使用 ms-rest-azure 库的 JavaScript 身份验证示例的更多详细信息,请参阅 here。
更新:
如果使用服务主体,请尝试添加 Azure OSSRDBMS 数据库应用程序权限。导航到 Azure AD > 你的应用 > API 权限。并记得为您的租户授予管理员许可。