簡體   English   中英

使用C#消費Drupal RestApi

[英]Consuming Drupal RestApi with c#

我正在使用c#消費Drupal Rest Api。 我正在使用drupal 7.5,並在各種資源之后利用它的rest服務/ api。

我已經成功地與Google的郵遞員一起發布了內容,但是當我嘗試使用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#代碼成功登錄后,將獲得cookie和令牌。

如果有人可以提供指導來解決此問題,那就太好了。 問候

您所需要的只是將UserAgent添加到http請求標頭中

我得到的用戶信息包括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; }               

    }

和Post部分:

 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:禁止,內容類型:application / problem + json; charset = utf-8,Content-Length:-1)”

和Drupal日志:Restful 2016-09-21 16:32您無權創建新的示例條形碼...匿名(未驗證)

我曾經用以下方式登錄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);

這將為您提供必要的會話和令牌信息,然后基本上您可以使用這些信息來發布帖子,類似於我在問題中所寫的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM