从HttpWebRequest转换为HttpClient

huangapple go评论90阅读模式
英文:

Converting from HttpWebRequest to HttpClient

问题

I am working with the Verizon ThingSpace API, found here.

我正在使用Verizon ThingSpace API,可以在这里找到。

I am attempting to generate the OAuth token. The API documentation provides a cURL example:

我正在尝试生成OAuth令牌。API文档提供了一个cURL示例:

curl -X POST -d "grant_type=client_credentials" -H "Authorization: Basic BASE64_ENCODED_APP_KEY_AND_SECRET" -H "Content-Type: application/x-www-form-urlencoded" "/api/ts/v1/oauth2/token"

I have been able to successfully replicate the cURL command in C# using the older HTTPWebRequest, but have failed to do so using the newer HttpClient.

我已成功在C#中使用较旧的HTTPWebRequest复制了cURL命令,但尝试使用较新的HttpClient时失败了。

I get a return value of:

我得到了一个返回值:

{"error":"invalid_request","error_description":"Invalid grant_type parameter or parameter missing"}

What is the proper way of replicating the cURL example in the API docs using the newer HttpClient class in C#? I have reviewed this answer, but it doesn't address the issue I am having with the grant_type.

如何使用较新的C# HttpClient类复制API文档中的cURL示例的正确方法是什么?我已经查看了这个答案,但它没有解决我在grant_type方面遇到的问题。

Edit:

Here is the HTTPWebRequest implementation, which is working as expected:

以下是HTTPWebRequest的实现,它按预期工作:

  1. public static void API_Login()
  2. {
  3. Console.WriteLine("API Request ----------------------------------");
  4. string url = @"https://thingspace.verizon.com/api/ts/v1/oauth2/token";
  5. var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
  6. httpWebRequest.Method = "POST";
  7. httpWebRequest.ContentType = "application/x-www-form-urlencoded";
  8. httpWebRequest.Headers.Add("Authorization", $"Basic {encodedKeyAndSecret}");
  9. var body = "grant_type=client_credentials";
  10. var data = Encoding.ASCII.GetBytes(body);
  11. httpWebRequest.ContentLength = data.Length;
  12. using (var stream = httpWebRequest.GetRequestStream())
  13. {
  14. stream.Write(data, 0, data.Length);
  15. }
  16. var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
  17. using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
  18. {
  19. var result = streamReader.ReadToEnd();
  20. JsonObject jsonObject = (JsonObject)JsonObject.Parse(result);
  21. apiToken = jsonObject["access_token"].ToString();
  22. }
  23. }

And here is HttpClient implementation, which is not working correctly:

以下是HttpClient的实现,它无法正常工作:

  1. public async static void HttpClient_API_Login(string encodedKeyAndSecret)
  2. {
  3. string url = @"https://thingspace.verizon.com/api/ts/v1/oauth2/token";
  4. var client = new HttpClient();
  5. var body = "grant_type=client_credentials";
  6. var request = new HttpRequestMessage()
  7. {
  8. Method = HttpMethod.Post,
  9. RequestUri = new Uri(url),
  10. Headers =
  11. {
  12. { HttpRequestHeader.ContentType.ToString(), "application/x-www-form-urlencoded" },
  13. { HttpRequestHeader.Authorization.ToString(), $"Basic {encodedKeyAndSecret}" },
  14. },
  15. Content = new StringContent(body)
  16. };
  17. var response = client.SendAsync(request).Result;
  18. var sr = await response.Content.ReadAsStringAsync();
  19. Console.WriteLine("response: " + sr);
  20. }
英文:

I am working with the Verizon ThingSpace api, found here.

I am attempting to generate the Oauth token. The API documentation provides a curl example:

  1. curl -X POST -d "grant_type=client_credentials" -H "Authorization: Basic BASE64_ENCODED_APP_KEY_AND_SECRET" -H "Content-Type: application/x-www-form-urlencoded" "/api/ts/v1/oauth2/token"

I have been able to successfully replicate the curl command in C# using the older HTTPWebRequest, but have failed to do so using the newer HttpClient.

I get a return value of:

  1. {"error":"invalid_request","error_description":"Invalid grant_type parameter or parameter missing"}

What is the proper way of replicating the curl example in the API docs using the newer HttpClient class in c#? I have reviewed this answer, but it doesn't address the issue I am having with the grant_type.

Edit:

Here is the HTTPWebRequest implementation, which is working as expected:

  1. public static void API_Login()
  2. {
  3. Console.WriteLine("API Request ----------------------------------");
  4. string url = @https://thingspace.verizon.com/api/ts/v1/oauth2/token;
  5. var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
  6. httpWebRequest.Method = "POST";
  7. httpWebRequest.ContentType = "application/x-www-form-urlencoded";
  8. httpWebRequest.Headers.Add("Authorization", $"Basic {encodedKeyAndSecret}");
  9. var body = "grant_type=client_credentials";
  10. var data = Encoding.ASCII.GetBytes(body);
  11. httpWebRequest.ContentLength = data.Length;
  12. using (var stream = httpWebRequest.GetRequestStream())
  13. {
  14. stream.Write(data, 0, data.Length);
  15. }
  16. var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
  17. using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
  18. {
  19. var result = streamReader.ReadToEnd();
  20. JsonObject jsonObject = (JsonObject)JsonObject.Parse(result);
  21. apiToken = jsonObject["access_token"].ToString();
  22. }
  23. }

And here is HttpClient implementation, which is not working correctly:

  1. public async static void HttpClient_API_Login(string encodedKeyAndSecret)
  2. {
  3. string url = @https://thingspace.verizon.com/api/ts/v1/oauth2/token;
  4. var client = new HttpClient();
  5. var body = "grant_type=client_credentials";
  6. var request = new HttpRequestMessage()
  7. {
  8. Method = HttpMethod.Post,
  9. RequestUri = new Uri(url),
  10. Headers =
  11. {
  12. { HttpRequestHeader.ContentType.ToString(), $"application/x-www-form-urlencoded" },
  13. { HttpRequestHeader.Authorization.ToString(), $"Basic {encodedKeyAndSecret}" },
  14. },
  15. Content = new StringContent(body)
  16. };
  17. var response = client.SendAsync(request).Result;
  18. var sr = await response.Content.ReadAsStringAsync();
  19. Console.WriteLine("response: " + sr);
  20. }

答案1

得分: 0

以下是翻译好的部分:

"我已经通过以下方式自行解决了这个问题:

  1. public async static void HttpClient_API_Login(string encodedKeyAndSecret)
  2. {
  3. string url = @"https://thingspace.verizon.com/api/ts/v1/oauth2/token";
  4. var client = new HttpClient();
  5. var formData = new List<KeyValuePair<string, string>>
  6. {
  7. new KeyValuePair<string, string>("grant_type", "client_credentials")
  8. };
  9. var request = new HttpRequestMessage()
  10. {
  11. Method = HttpMethod.Post,
  12. RequestUri = new Uri(url),
  13. Headers =
  14. {
  15. { HttpRequestHeader.ContentType.ToString(), "application/x-www-form-urlencoded" },
  16. { HttpRequestHeader.Authorization.ToString(), $"Basic {encodedKeyAndSecret}" },
  17. },
  18. Content = new FormUrlEncodedContent(body)
  19. };
  20. }

问题是请求体应为表单内容,而不是字符串内容。"

英文:

I have since solved the issue on my own by changing the code as follows:

  1. public async static void HttpClient_API_Login(string encodedKeyAndSecret)

{

  1. string url = @https://thingspace.verizon.com/api/ts/v1/oauth2/token;
  2. var client = new HttpClient();
  3. var formData = new List&lt;KeyValuePair&lt;string, string&gt;&gt;
  4. {
  5. new KeyValuePair&lt;string, string&gt;(&quot;grant_type&quot;, &quot;client_credentials&quot;)
  6. };
  7. var request = new HttpRequestMessage()
  8. {
  9. Method = HttpMethod.Post,
  10. RequestUri = new Uri(url),
  11. Headers =
  12. {
  13. { HttpRequestHeader.ContentType.ToString(), $&quot;application/x-www-form-urlencoded&quot; },
  14. { HttpRequestHeader.Authorization.ToString(), $&quot;Basic {encodedKeyAndSecret}&quot; },
  15. },
  16. Content = new FormUrlEncodedContent(body)
  17. };

The issue was that the body was string content, not form content.

huangapple
  • 本文由 发表于 2023年4月11日 05:27:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定