【问题标题】:Consuming Drupal RestApi with c#使用 c# 使用 Drupal Rest Api
【发布时间】: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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我得到的用户信息包括 cookie 和令牌。

     private login_user LoginAsync(string username,string password)
        {
            try
            {
                RestClient client = new RestClient(base_url);   
                var request = new RestRequest("login", Method.GET);
                request.AddHeader("Content-Type", "Application/JSON");
    
                client.Authenticator = new HttpBasicAuthenticator(username, password);
                var restResponse = client.Execute(request);
                var content = restResponse.Content;
                string context_modifytoken = content.ToString().Replace("X-CSRF-Token", "X_CSRF_Token");
                var current_login_user = JsonConvert.DeserializeObject<login_user>(context_modifytoken);
                current_login_user.data.session_name = restResponse.Cookies[0].Name;
                current_login_user.data.session_id = restResponse.Cookies[0].Value;
    
                return current_login_user;
    
            }
            catch (HttpRequestException ex) { throw ex; }               
    
        }
    

    还有帖子部分:

     RestClient client = new RestClient(base_url);
            var request = new RestRequest("v1.0/barcodes", Method.POST);
            request.AddHeader("cache-control", "no-cache");                      
            request.AddHeader("X-CSRF-Token", current_user.data.X_CSRF_Token);
            request.AddHeader("content-type", "application/json");
            request.AddHeader("Accept", "application/json");
            request.AddHeader("cookie", current_user.data.session_name+"="+ current_user.data.session_id);
            request.AddHeader("User-Agent", ver);
    
            var queryresult = client.Execute(request);
    

    有错误:queryresult = "StatusCode: Forbidden, Content-Type: application/problem+json; charset=utf-8, Content-Length: -1)"

    和 Drupal 日志: restful 2016-09-21 16:32 您无权创建新的示例条形码...匿名(未验证)

    【讨论】:

    • Login 是 Post,所以你需要使用“Method.POST”,终点是“user/login”。您不需要使用 x-csrf-token。执行后,您将获得有关会话 ID、名称和发布帖子所需的 x-csrf 令牌的信息。
    • 我使用 Method.POST 登录并发布创建日期。我添加了头 cookie,x-csrf-token 。你知道失败了。你有例子来说明如何做吗?或者指出错误。 stackoverflow.com/questions/39652010/… 是新的。
    • 那是关于“登录”的restful端点,并在new中使用REST_SERVER进行更新。我通过create article的poster和postman成功,但在C#中失败了。
    • 我发现了错误,与User_agent无关。它的cookie坏了。更改为:request.AddParameter(session_name,sessionid,parametertype.cookie);这是关键。
    【解决方案3】:

    我使用以下方式登录drupal

    var client = new RestClient("drupalsitename/user/login");
    var request = new RestRequest(Method.POST);
    request.AddHeader("cache-control", "no-cache");
    request.AddHeader("content-type", "application/json");
    request.AddHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0");
    request.AddParameter("application/json", "{\"name\":\"username\",\n\"pass\":\"password\"}", ParameterType.RequestBody);
    IRestResponse response = client.Execute(request);
    

    这将为您提供必要的会话和令牌信息,然后基本上您可以使用这些信息发布帖子,类似于我在问题中写的内容

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-19
      • 1970-01-01
      • 2012-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多