Consuming Drupal RestApi with c#

前端 未结 3 856
一生所求
一生所求 2021-01-24 11:49

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.

I have been successful with go

3条回答
  •  再見小時候
    2021-01-24 12:08

    I got the user information include the cookie & token .

     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(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; }               
    
        }
    

    And the Post section:

     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);
    

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

    and Drupal log : restful 2016-09-21 16:32 You do not have access to create a new Sample Barcode... Anonymous (not verified)

提交回复
热议问题