我有这个代码似乎可以工作(没有错误),但我希望能够以某种方式显示新令牌(Console.WriteLine 等)
private static bool CertificateValidationCallBack(HttpRequestMessage arg1, X509Certificate2 arg2, X509Chain arg3, SslPolicyErrors arg4)
{
throw new NotImplementedException();
}
private static async Task<Token> GetElibilityToken(HttpClient client)
{
string baseAddress = @"https://label.xxxx.dot.net/myToken/api/auth/token";
string grant_type = "password";
string client_id = "goldline";
string Username = "xyz";
string client_secret = "myPassword";
var form = new Dictionary<string, string>
{
{"grant_type", grant_type},
{"client_id", client_id},
{"Username" , Username },
{"client_secret", client_secret},
};
{
HttpResponseMessage tokenResponse = await client.PostAsync(baseAddress, new FormUrlEncodedContent(form));
var jsonContent = await tokenResponse.Content.ReadAsStringAsync();
Token tok = JsonConvert.DeserializeObject<Token>(jsonContent);
return tok;
如果这是应用程序的一部分,您可以添加
Trace.TraceInformation($"GetElibilityToken: the token being returned is {jsonContent}")
并检查日志记录。
或者您可以在 Linqpad 中重新创建此代码片段,设置您自己的 HttpClient 并使用您在
form
对象中设置的正确凭据发送该调用。然后转储()结果。