2016-09-19 28 views
0

我正在使用C#来使用Drupal的Rest Api。我正在使用drupal 7.5,并在各种资源之后使用它的rest services/api。用c来消费Drupal的RestApi#

我已经成功与谷歌的邮递员张贴的内容,但当我尝试复制它与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("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#代码成功登录尝试后获得的。

如果有人能够提供解决这个问题的指导,那将是非常好的。 问候

+0

这是否帮助? http://drupal.stackexchange.com/questions/97993/rest-services-module-access-denied-for-user-anonymous –

+0

我跟着链接之前,我已成功地让它在邮递员完成,但是当我写代码并尝试使用它,“拒绝匿名用户访问”的错误仍然存​​在 – shunilkarki

+1

您是否尝试将UserAgent添加到请求标头中? –

回答

1

所有你需要的是增加用户代理到HTTP请求头

0

我得到的用户信息包括饼干&令牌。

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:禁止,内容类型:应用/问题+ JSON;字符集= UTF-8,内容长度: - 1)”

和Drupal日志: 宁静的2016年9月21日16:32您没有访问权限以创建一个新的样本条码... Anonymous(未验证)

+0

登录是发布,因此您需要使用“Method.POST”并且结束点是“用户/登录”。你不需要使用x-csrf-token。执行后,您将获得有关创建帖子所需的会话ID,名称和x-csrf标记的信息。 – shunilkarki

+0

我使用Method.POST登录并发布日期以进行创建。并且我添加cookie,x-csrf-token。您知道有fail.did,您有示例来说明如何执行此操作?或指出错误。 http://stackoverflow.com/questions/39652010/c-sharp-restsharp-client-connect-drupal-rest-server-access-denied-for-user-anony它是新的。 – ewen

+0

这是关于“登录”的安宁的端点,并更新使用REST_SERVER在新的.I成功与创建文章的海报和邮递员,但在C#中失败。 – ewen

0

我用下面登录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); 

这将为您提供必要的会话和令牌信息,然后基本上就可以利用这些信息做出的帖子,类似于我已经写在问题