2016-09-19 33 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); 

這將爲您提供必要的會話和令牌信息,然後基本上就可以利用這些信息做出的帖子,類似於我已經寫在問題