【问题标题】:Authenticating to Dynamics NAV for OData对 OData 的 Dynamics NAV 进行身份验证
【发布时间】:2016-10-21 16:43:33
【问题描述】:

我正在尝试编写一个使用 Dynamics NAV Odata 提要的 node.js 脚本。

我的 Dynamics NAV 设置中同时拥有 UserAccount/PW 和 Web 服务访问密钥。

我终其一生都无法弄清楚如何正确地进行身份验证,无论是通过在标头中添加内容还是在 URL 查询中添加内容。我试过使用“用户名:密码@服务器”格式。我已经尝试将其编码为 base64 并将其添加到“身份验证”值的标题中。

documentation itself 非常不具体。我知道如何生成密钥,但我不知道如何正确地将该密钥发送到 NAV 进行身份验证。

我正在使用“request-promise”npm 包,它带有一个“选项”参数,我可以将任意标题键:值对添加到其中。请有人给我一些关于如何验证 NAV 的指导。我已经做了好几个小时了。

【问题讨论】:

标签: node.js web-services dynamics-nav


【解决方案1】:

我找到了满意的答案。

使用node-libcurl 我能够使用以下格式将 URL 卷曲到一个 url

http://username:password@<server>/ODATA_table

特别是我的 cURL 模块如下所示:

var Curl = require('node-libcurl').Curl;

var curl = new Curl(),
    close = curl.close.bind(curl);

function getOData(url) {
    return new Promise((resolve, reject) => {
        curl.setOpt(Curl.option.URL, url);
        curl.setOpt(Curl.option.HTTPAUTH, Curl.auth.NTLM);
        curl.setOpt(Curl.option.SSL_VERIFYPEER, false);
        curl.setOpt(Curl.option.SSL_VERIFYHOST, false);
        curl.setOpt(Curl.option.POST, 0);


        curl.on('end', function (statusCode, body, headers) {

            var retObj = JSON.parse(body);
            resolve(retObj);
            close();
        });

        curl.on( 'error', function(e){
            reject(e);
            close();
        });

        curl.perform();
    })
}

module.exports = {getOData: getOData};

但我必须在 url 中明确要求 json,例如 ?format=json

【讨论】:

【解决方案2】:

Tkol,你是对的,你也可以使用guzzle,很简单,就是一个查询customers表的示例函数:

public function ReadCustomer($identifier=0)
{
  try {

       $client = new GuzzleHttpClient();

       $apiRequest = $client->request('GET', 'http://server:port/ServiceName/WS/CompanyName/Page/Customer?$filter=No eq \''.$identifier.'\'',[
            'auth' =>'username','password', 'NTLM' ],       //NTLM authentication required
            'debug' => true                                  //If needed to debug   
      ]);


      $content = json_decode($apiRequest->getBody()->getContents());
      return $content;

  } catch (RequestException $re) {
      //For handling exception
  }
}

您可以查看我的示例: update/delete/get from Dynamics NAV OData webservice

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-01
  • 2018-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多