Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Api Id和Api密钥的RestSharp身份验证_C#_Api_Rest_Restsharp - Fatal编程技术网

C# 使用Api Id和Api密钥的RestSharp身份验证

C# 使用Api Id和Api密钥的RestSharp身份验证,c#,api,rest,restsharp,C#,Api,Rest,Restsharp,我必须调用restful API,但我仅有的细节是API ID和API密钥 我正试图像这样使用Restsharp库代码 var client = new RestClient("https://xxxxxxx"); client.Authenticator = new HttpBasicAuthenticator("xxxxx", "yyyyyy"); 我得到一个401授权要求的错误 你能给我指一下正确的方向吗 谢谢。这是我提出的解决方案 这将返回客户机对象 private RestC

我必须调用restful API,但我仅有的细节是API ID和API密钥

我正试图像这样使用Restsharp库代码

  var client = new RestClient("https://xxxxxxx");
  client.Authenticator = new HttpBasicAuthenticator("xxxxx", "yyyyyy");
我得到一个401授权要求的错误

你能给我指一下正确的方向吗


谢谢。

这是我提出的解决方案

这将返回客户机对象

private RestClient InitializeAndGetClient()
        {
            var cookieJar = new CookieContainer();
            var client = new RestClient("https://xxxxxxx")
            {
                Authenticator = new HttpBasicAuthenticator("xxIDxx", "xxKeyxx"),
                CookieContainer = cookieJar
            };

            return client;
        }
你可以使用这样的方法

            var client = InitializeAndGetClient();
            var request = new RestRequest("report/transaction", Method.GET);

            request.AddParameter("option", "value");



            //Run once to get cookie.
            var response = client.Execute(request);

            //Run second time to get actual data
            response = client.Execute(request);
希望这对你有帮助


普拉卡什。

我知道这很旧,但这是我所需要的:

        var cookieJar = new CookieContainer();
        var client = new RestClient("https://xxxxxxx")
        {
            Authenticator = new HttpBasicAuthenticator("[username]", "[password]"),
            CookieContainer = cookieJar
        };
在创建请求对象之后,我必须添加API_密钥头:

            var request = new RestRequest("[api]/[method]"); // this will be unique to what you are connecting to
            request.AddHeader("API_KEY", "[THIS IS THE API KEY]");