【问题标题】:Error: credentials argument needs to implement signRequest method?错误:凭据参数需要实现 signRequest 方法?
【发布时间】:2020-07-14 19:48:22
【问题描述】:

当我使用“ms-rest-azure”库获取 Azure 凭据时。

let credentials = await msRestAzure.loginWithServicePrincipalSecret(id, secret, tanent);
let newCred = JSON.stringify(credentials);
let duplicateCred = JSON.parse(newCred);  // Create duplicate credentials object

现在,如果我要在进一步的 Azure 函数调用中使用“duplicateCred”,则会收到上述错误。 但是,如果我使用“凭据”,那么一切都很好。 那么如何将“凭据”对象分配给其他变量?这样我就可以在未来的天蓝色 API 调用中使用该变量。

例子:

let credentials = await msRestAzure.loginWithServicePrincipalSecret(id, secret, tanent);
let newCred = JSON.stringify(credentials);
let duplicateCred = JSON.parse(newCred); // Create duplicate credentials object

// Okay, here I'm getting the proper client object. Because I am using "credentials" in the below line of code.
// I'm getting the results from the below lines of code.
const client = new MonitorManagementClient(credentials, subscription);  
const results = await client.operations.list();
context.log('results==> ', results);

// Error, here not getting the proper client object. Because I am using "duplicateCred" as credentials in the below line of code.
// I'm not getting the results from the below lines of code.
// At the below line I'm getting the above error.
const client = new MonitorManagementClient(duplicateCred, subscription);  
const results = await client.operations.list();
context.log('results==> ', results);

如何从实际凭据对象创建重复的凭据对象?

【问题讨论】:

    标签: javascript node.js azure azure-functions azure-sdk


    【解决方案1】:

    这是不正确的方法。 msRestAzure.loginWithServicePrincipalSecret 调用返回的凭据不是普通对象,它是 ApplicationTokenCredentials 的实例。当您调用JSON.stringify 时,您会丢失此类引用并将整个值转换为Object。所以克隆的凭证对象会使用ApplicationTokenCredentials 方法丢失原型链。

    另外ApplicationTokenCredentials 没有克隆逻辑,这可能意味着它不应该被克隆。使用你自己的克隆逻辑可能会与 Azure 库的未来版本发生冲突。需要克隆的方法最好在官方Azure SDK repository加个issue。

    注意! ⚠️ 这种克隆方式可能很危险并导致意外行为。你应该强烈避免它,除非你真的需要擦除值的原型链!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      相关资源
      最近更新 更多