We help IT Professionals succeed at work.

C# RestSharp - please help with conversion - mindful of JSON conversion of Content?

var request = new HttpRequestMessage(HttpMethod.Post, "/token"){ Content = content };
                var responseToken = httpClientToken.SendAsync(request).Result;
                var resultToken = responseToken.Content.ReadAsStringAsync().Result;
                var jsonResult = JObject.Parse(resultToken);

                var httpClientCreateSurvey = new HttpClient();
                httpClientCreateSurvey.BaseAddress = new Uri("https://clear.pandolink.com");
                httpClientCreateSurvey.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));
                httpClientCreateSurvey.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue("Bearer", jsonResult["access_token"].ToString());
                var responsePando = httpClientCreateSurvey.PostAsync("/api/extension/survey-account",
                    new StringContent(surveyJson, Encoding.UTF8, "text/json")).Result;
                var result = responsePando.Content.ReadAsStringAsync().Result;
                jsonResult = JObject.Parse(result);
                var pandoAccountId = jsonResult["AccountId"];
                return pandoAccountId.ToString() == "0" ? "" : pandoAccountId.ToString();

Open in new window




What would be the RestSharp equivalent?

I am struggling with what would be the RestSharp equivalent of ReadAsStringAsync( ).   I am glossing over that when serializing the response.Content but I am not getting away with it.


Particularly, I am struggling with the extraction of the value of "AccountId" from the jsonResult var:

 var pandoAccountId = jsonResult["AccountId"];
                return pandoAccountId.ToString() == "0" ? "" : pandoAccountId.ToString();

Open in new window




Here is my attempt so far:

  


//THESE TWO LINES ARE SUSPECT
var resultToken = response.Content.ToString();
                var jsonResult = JObject.Parse(resultToken);


    //            "access_token":"<modified by OP>us2iHZQgtoAQo7ZwF8Ky_7iBBWg GARBAGE EIAmPpNtKuW0o9Ns8AR96VmYqTBaFQ94qXhpvsewli3gV19S5VwwBLUBmUfb3oa3HEo5lqa_LMBhO2gWVGpxl3DLAtcNiH2ifipQ-vn2BOFdByajJlfzhXtIopzp06FbNTkzHsRRHj95dDac6JvYJGId5Ohq2namkTuLLBuALOSu0yyy-EjGrX3zSxOVizIubmy-GN9QG16cGXpX1MjVG3kPYMu4wN-h24QVTrKGBTvBbIABw3YL0LDkFP3r1E103-_MgkQ9D5YXEnlbUM6VJq-euoXk0OyGJ9RwqBkmy-1oenkZttt5eZeNym3ZYojmcDxcLbYyvobKwfbjNsL7XHbl5yqgJpiEisAg15co3RHCfKn5YGfyh3LFsE4o3qOF1IWhklJDtzjAUKOXwF6Ayvfkpmw6P6DMRNEFyvqHKl-zm7N2_8QUoPqlBzKKweg04oJEoN4DrIkpZe4ksY_gcXM3GomFTDSdqgGXc1NpfewFxjjcc2mQ3kMrw41fk8lxVwzgcsgGfc5nLtYijzFSl1DvxnP2l8irjQ",
    //"token_type": "bearer",
    //"expires_in": 71999,
    //"refresh_token": "cceb0a1a21f2<corrupted by OP> GARBAGE 1d31e300479",
    //"userName": "<hidden by OP>",
    //"as:client_id": "0A71EFCA-1429-47BF-83AD-27B5C982A63D",
    //"userId": "2005",
    //"p": "",
    //"fullName": "Clear System",
    //"user_id_num": "<hidden by OP>",



                var clientCreateSurvey = new RestClient("https://clear.pandolink.com/api/extension/survey-account");

                var request2 = new RestRequest(Method.POST);
                request2.AddHeader("Content-Type", "application/json");
                request2.AddHeader("Bearer", jsonResult["access_token"].ToString());
                IRestResponse responsePando = clientCreateSurvey.Execute(request2);
                var result2 = responsePando.Content.ToString();
                var jsonResult2 = JObject.Parse(result2);
                var pandoAccountId = jsonResult2["AccountId"];
                
                return pandoAccountId.ToString() == "0" ? "" : pandoAccountId.ToString();

Open in new window

Comment
Watch Question

Web developer
Commented:
My eventual answer:

var surveyJson = JsonConvert.SerializeObject(survey);

                var httpClientToken = new HttpClient();
                httpClientToken.BaseAddress = new Uri("https://[b]hidden[/b].com");
                httpClientToken.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));

                var content = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair<string, string>("grant_type", "password"),
                    new KeyValuePair<string, string>("username", username),
                    new KeyValuePair<string, string>("password", password),
                    new KeyValuePair<string, string>("client_id", "[b]hidden[/b]1429-47BF-83AD-27B5C982A63D"),
                    new KeyValuePair<string, string>("client_secret", "[b]hidden[/b]")
                });

                var request = new HttpRequestMessage(HttpMethod.Post, "/token"){ Content = content };
                var responseToken = httpClientToken.SendAsync(request).Result;
                var resultToken = responseToken.Content.ReadAsStringAsync().Result;
                var jsonResult = JObject.Parse(resultToken);

                var httpClientCreateSurvey = new HttpClient();
                httpClientCreateSurvey.BaseAddress = new Uri("https://[b]hidden[/b]");
                httpClientCreateSurvey.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));
                httpClientCreateSurvey.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue("Bearer", jsonResult["access_token"].ToString());
                var responsePando = httpClientCreateSurvey.PostAsync("/api/[b]hidden[/b]t",
                    new StringContent(surveyJson, Encoding.UTF8, "text/json")).Result;
                var result = responsePando.Content.ReadAsStringAsync().Result;
                jsonResult = JObject.Parse(result);
                var pandoAccountId = jsonResult["[b]hidden[/b]"];
                return pandoAccountId.ToString() == "0" ? "" : pandoAccountId.ToString();

Open in new window

Tom KnowltonWeb developer

Author

Commented:
sorry for the other posts...  I just need the last one ... my solution.