【发布时间】:2016-09-19 07:46:38
【问题描述】:
我正在使用 c# 来使用 Drupal Rest Api。我正在使用 drupal 7.5 并在各种资源之后利用它的休息服务/api。
我已经成功通过 google 的邮递员发布内容,但是当我尝试使用 c# 代码复制它时,我收到了禁止错误提示:Access denied for user anonymous. 我正在利用 rest-sharp 来使用这个 API。我已经研究了很多,还没有找到解决方案,也没有注意到有人在 c# 中做这项工作。 下面是我根据postman写的sn-p代码
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);
使用c#代码登录成功后获取cookie和token。
如果有人能提供解决此问题的指导,那就太好了。 问候
【问题讨论】:
-
我之前点击了链接,我已经成功在 Postman 中完成了它,但是当我编写代码并尝试使用它时,“:用户匿名访问被拒绝”的错误仍然存在跨度>
-
您是否尝试将 UserAgent 添加到请求标头中?
-
不,我没试过。如果可以的话,请您详细说明一下,因为我对这个主题知之甚少
-
谢谢 Rawitas Krungkaew,我再次尝试使用 useragent,它工作正常。
标签: c# rest drupal-7 restsharp