简体   繁体   English

对OData的Dynamics NAV进行身份验证

[英]Authenticating to Dynamics NAV for OData

I'm trying to write a node.js script that uses a Dynamics NAV Odata feed. 我正在尝试编写一个使用Dynamics NAV Odata Feed的node.js脚本。

I have both a UserAccount/PW and a Web Services Access Key from my Dynamics NAV setup. 我有一个UserAccount / PW和一个来自我的Dynamics NAV设置的Web服务访问密钥。

I can't for the life of my find out how to properly authenticate, either by adding something in a header or by adding something in the URL query. 我无法通过在标题中添加内容或在URL查询中添加内容来查找如何正确进行身份验证。 I've tried using the 'username:password@server' format. 我尝试过使用'username:password @ server'格式。 I've tried encoding that as base64 and adding that in the Header for the 'Authentication' value. 我已经尝试将其编码为base64并在Header中添加“Authentication”值。

The documentation itself is incredibly non-specific. 文档本身非常不具体。 I know how to generate the key, but I don't know how to properly send that key to NAV to authenticate. 我知道如何生成密钥,但我不知道如何正确地将该密钥发送给NAV进行身份验证。

I'm using the 'request-promise' npm package, which takes an 'options' argument that I can add arbitrary header key:value pairs into. 我正在使用'request-promise'npm包,它接受一个'options'参数,我可以添加任意头键:值对。 Please someone give me some direction about how to authenticate to NAV. 请有人给我一些关于如何验证资产净值的方向。 I've been on this for hours. 我已经在这几个小时了。

I found a satisfactory answer. 我找到了一个满意的答案。

Using node-libcurl I was able to cURL to a url using the format 使用node-libcurl我能够使用该格式cURL到url

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

specifically my cURL module looks like this: 特别是我的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};

But I have to explicitly ask for json in the url, like ?format=json . 但是我必须在url中明确地询问json,比如?format=json

Tkol, you're right,also you can use guzzle , it's very simple, that's a sample function that query customers table : Tkol,你是对的,你也可以使用guzzle ,这很简单,这是一个查询客户表的示例函数:

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

you can check my sample: update/delete/get from Dynamics NAV OData webservice 你可以查看我的样本: 更新/删除/获取Dynamics NAV OData webservice

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM