对 OData 的 Dynamics NAV 进行身份验证

Authenticating to Dynamics NAV for OData

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

我有一个 UserAccount/PW 和一个来自我的 Dynamics NAV 设置的 Web 服务访问密钥。

我终其一生都无法找到如何通过在 header 中添加内容或在 URL 查询中添加内容来正确进行身份验证。我试过使用 'username:password@server' 格式。我试过将其编码为 base64 并将其添加到 Header 中以获得 'Authentication' 值。

documentation itself 令人难以置信 non-specific。我知道如何生成密钥,但我不知道如何将该密钥正确发送到 NAV 进行身份验证。

我正在使用 'request-promise' npm 包,它带有一个 'options' 参数,我可以向其中添加任意 header key:value 对。请有人给我一些关于如何对 NAV 进行身份验证的指导。我已经研究了几个小时了。

我找到了满意的答案。

使用 node-libcurl 我能够使用格式

cURL 到 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

Tkol,你说的对,你也可以用guzzle,很简单,就是一个查询客户的示例函数table:

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
  }
}

你可以查看我的示例: