英文:
How do I grab access_token value from the httpclient reponse?
问题
var responseJson = await response.Content.ReadAsStringAsync();
var responseObject = JsonSerializer.Deserialize<Dictionary<string, string>>(responseJson);
var bearToken = responseObject["access_token"];
这段代码将从响应中提取 "access_token" 的值存储在变量 bearToken 中。
英文:
I have a C# method a calling service to retrieve the access_token, how do I pull that value from response returned?
try {
var client = new HttpClient();
client.BaseAddress = new Uri(_serviceConfig.DataGapsBaseUrl);
var request = new HttpRequestMessage(HttpMethod.Post, "/dataopssecurity/oauth2/token");
var formData = new List<KeyValuePair<string, string>>();
formData.Add(new KeyValuePair<string, string>("grant_type", _serviceConfig.grantType));
formData.Add(new KeyValuePair<string, string>("username", _serviceConfig.username));
formData.Add(new KeyValuePair<string, string>("password", _serviceConfig.password));
formData.Add(new KeyValuePair<string, string>("scope", _serviceConfig.scope));
request.Content = new FormUrlEncodedContent(formData);
var authenticationString = $"{Username}:{Password}";
request.Headers.Authorization = new AuthenticationHeaderValue(
scheme: "Basic",
parameter: Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(authenticationString)));
//client.DefaultRequestHeaders.Add($"Authorization", $"Basic {Convert.ToBase64String($"{Username}:{Password}")}");
var response = await client.SendAsync(request);
How do I something like var bearToken = Response["access_token"] ?
Json Returned:
{
"access_token": "eyJraWQiOiIxNDA2ZGQ3MC0zOWQ4LTRjOGItYjI1Mi0yM2RhZjk0Mjg0NTMiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJjZGlzcG9sbGluZ3N2YyIsImF1ZCI6ImRhdGFvcHNzdWl0ZS1jbGktY2xpZW50IiwidXNlci1hdXRob3JpdGllcyI6WyJ2aWV3LXRlbXBsYXRlIiwiZWRpdC1waXBlbGluZSIsImVkaXQtZGF0YS1zb3VyY2UiLCJ2aWV3LWRhdGFzZXQiLCJlZGl0LWF6dXJlLWNsdXN0ZXIiLCJlZGl0LWRvbWFpbnMiLCJkYXRhZmxvdy1kYXRhLXJ1bGUiLCJ2aWV3LWRhdGEtcXVhb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9GE8gTPCv1iJFQWJd5qxMbcL6DFs-2jP8SQ8UvJ6MiVFsTw5e09zspNgeV77Q_1dGgkghNHxU8r164eDI6Cp9GtTLFpwyAgW4o9HBE0A7jXk7w_vGJVEEpT-Wl7WpyHDCXBPOb2_yxIfLH1qhA00SrORTt555DPLM1ZkpRf1njeBTHRNVahReD5r3Bigp1pxWftFBFPoNMDhTpZrFqMQ1reKsmZOKUoOHCi2AtEDPEAzPwYS31i-R8Rd4wbZE3moJtj9SPN70YgT1yngt-HT4zYSaIfxG1wodhSAz5Izrw",
"refresh_token": "KJSh8AkLmdOPxD2nzMAyeDel4Dai7UxWiftS4wUxjNw_LjGvuQK5dj2cTKgWLjeRkxzkLoQKmng5iLpUMSKVrZ_lzGSPHeedaAI6GBdKcraLzKdm7t69hA1q6jVvLGn7",
"scope": "openid",
"token_type": "Bearer",
"expires_in": 86400
}
答案1
得分: 2
Possible solution is to deserialize the response into an object and read the property of the object.
string content = !response.IsSuccessStatusCode
? null
: response.Content.ReadAsStringAsync().Result;
TokenResponse tokenResponse = JsonConvert.DeserializeObject<TokenResponse>(content);
string accessToken = tokenResponse.access_token;
The 'TokenResponse' object can be as simple as an object with just the 'access_token' property.
As an alternative you can use Json.net or the MS Json libraries to read the value from the Json.
英文:
Possible solution is to deserialize the response into an object and read the property of the object.
string content = !response.IsSuccessStatusCode
? null
: response.Content.ReadAsStringAsync().Result;
TokenResponse tokenResponse = JsonConvert.DeserializeObject<TokenResponse>(content);
string accessToken = tokenResponse.access_token;
The 'TokenResponse' object can be as simple as an object with just the 'access_token' property.
As an alternative you can use Json.net or the MS Json libraries to read the value from the Json.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论