使用 c# 使用 Drupal RestApi

Consuming Drupal RestApi with c#(使用 c# 使用 Drupal RestApi)
本文介绍了使用 c# 使用 Drupal RestApi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 c# 来使用 Drupal Rest Api.我正在使用 drupal 7.5 并在各种资源之后利用它的休息服务/api.

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.

我已经成功通过 google 的邮递员发布内容,但是当我尝试使用 c# 代码复制它时,我收到了禁止错误提示:Access denied for user anonymous.我正在使用 rest-sharp 来使用这个 API.我已经研究了很多,还没有找到解决方案,也没有注意到有人在 c# 中做这项工作.以下是我基于postman编写的代码片段

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", "{
  "type":"page",
  "title":"Page submitted via JSON REST",
  "body":{
    "und":[
      {
        "value":"This is the body of the page."
      }
    ]
  }
}", ParameterType.RequestBody);
        IRestResponse response = client.Execute(request);

使用c#代码登录成功后获取cookie和token.

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

推荐答案

只需将 UserAgent 添加到 http 请求头中

All you need is adding UserAgent into http request header

这篇关于使用 c# 使用 Drupal RestApi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)