用C#使用Drupal RestApi [英] Consuming Drupal RestApi with c#
问题描述
我已经成功与谷歌的邮递员发布内容,但是当我尝试复制它c#代码我被提示与禁止错误说:访问被拒绝为匿名用户。
我正在利用rest-sharp消费这个API。我已经研究了很多,还没有找到解决方案,还没有注意到有人在c#中做这个工作。
以下是我根据邮递员编写的代码片段
var client = new RestClient(drupasitename / rest / node);
var request = new RestRequest(Method.POST);
request.AddHeader(cache-control,no-cache);
request.AddHeader(授权,授权);
request.AddHeader(x-csrf-token,token);
request.AddHeader(cookie,cookie);
request.AddHeader(content-type,application / json);
request.AddHeader(Accept,application / json);
request.AddParameter(application / json,{\r\\\
\type\:\page\,\r\\\
\title\ :\通过JSON REST \提交的页面,\r\\\
\body \:{\r\\\
\und\:[\r\\\
{\r\\\
\value\:\这是页面的正文。\\r\\\
} \r\\\
] \r\\\
} \r\\\
},ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
cookie和令牌在使用c#代码成功登录后获得。
如果任何人可以提供指导来解决这个问题,这将是巨大的。
注意
所有您需要的是将UserAgent添加到http请求头
I am working to consume Drupal Rest Api using c#. I am using drupal 7.5 and utilising it's rest services/api following various resources.
I have been successful with google's postman to post the content but when I try to replicate it with c# code I am prompted with forbidden error saying: Access denied for user anonymous. I am utilising rest-sharp to consume this API. I have researched quite a lot and haven't found solution yet,as well as haven't noticed anyone doing this work in c#. Following is the code snippet that I have written based on postman
var client = new RestClient("drupasitename/rest/node");
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("authorization", authorisation);
request.AddHeader("x-csrf-token", token);
request.AddHeader("cookie", cookie);
request.AddHeader("content-type", "application/json");
request.AddHeader("Accept", "application/json");
request.AddParameter("application/json", "{\r\n \"type\":\"page\",\r\n \"title\":\"Page submitted via JSON REST\",\r\n \"body\":{\r\n \"und\":[\r\n {\r\n \"value\":\"This is the body of the page.\"\r\n }\r\n ]\r\n }\r\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
cookie and token are obtained after successful login attempt using c# code.
It would be great if anyone could provide a guidance to solve this issues. Regards
All you need is adding UserAgent into http request header
这篇关于用C#使用Drupal RestApi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!