从“授权API”获取FedEx的oAuth令牌。

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

Get FedEx oAuth token from "Authorization API"

问题

我可以获取Fedex的"access_token",当我从Postman创建一个Post请求时。我将"client_id"和"client_secret"添加到请求体中作为文档中给出的原始字符串。

Postman创建的C#代码如下:

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://apis-sandbox.fedex.com/oauth/token");
request.Headers.Add("content-type", "application/x-www-form-urlencoded");
request.Headers.Add("Cookie",.......some cookie....);
var content = new StringContent("grant_type=client_credentials&client_id=...clientid...&client_secret=....client secret.....", null, "application/x-www-form-urlencoded");
request.Content = content;
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();

当我在Visual Studio中使用此代码进行测试时,在request.Headers.Add("content-type", "application/x-www-form-urlencoded");处收到错误消息:

错误的头部名称,'content-type'。确保请求头使用HttpRequestMessage,响应头使用HttpResponseMessage,并且内容头部使用HttpContent对象。

在搜索答案后,我将我的代码恢复为:

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://apis-sandbox.fedex.com/oauth/token");
HttpContent content = new FormUrlEncodedContent(
new List<KeyValuePair<string, string>> {
new KeyValuePair<string, string>("client_id", "...clientid..."),
new KeyValuePair<string, string>("client_secret", "..client secret....."),
new KeyValuePair<string, string>("grant_type","client_pc_credentials")});
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
content.Headers.ContentType.CharSet = "UTF-8";
client.DefaultRequestHeaders.ExpectContinue = false;
HttpResponseMessage response = await client.PostAsync(new Uri("https://apis-sandbox.fedex.com/oauth/token"), content);

return await response.Content.ReadAsStringAsync();

我调用这个方法像这样:var oAuth = MakeApiCallAsync(); 这段代码运行时没有错误,但它没有触发最后的代码行(return await response.Content.ReadAsStringAsync();),流程继续到调用方法,变量oAuth的值为(Result = Yet not computed, ..... other properties)

我尝试了几乎所有的方法,我筋疲力尽,有什么想法是什么出了问题?

英文:

I can get the Fedex "access_token" when I create a Post request from Postman. I add the "client_id" and "client_secret" to request body as Raw string as given in the Documentation (Fedex).

PostMan creates C# Code as follows :

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, &quot;https://apis-sandbox.fedex.com/oauth/token&quot;);
request.Headers.Add(&quot;content-type&quot;, &quot;application/x-www-form-urlencoded&quot;);
request.Headers.Add(&quot;Cookie&quot;,.......some cookie....&quot;);
var content = new StringContent(&quot;grant_type=client_credentials&amp;client_id=...clientid...&amp;client_secret=....client secret.....&quot;, null, &quot;application/x-www-form-urlencoded&quot;);
request.Content = content;
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();

When I use this code in Visual Studio for testing, on request.Headers.Add(&quot;content-type&quot;, &quot;application/x-www-form-urlencoded&quot;); I get the error message :

Misused header name, &#39;content-type&#39;. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.&#39;

After searching answers I revert my code to this :

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, &quot;https://apis-sandbox.fedex.com/oauth/token&quot;);
HttpContent content = new FormUrlEncodedContent(
new List&lt;KeyValuePair&lt;string, string&gt;&gt; {
new KeyValuePair&lt;string, string&gt;(&quot;client_id&quot;, &quot;...clientid...&quot;),
new KeyValuePair&lt;string, string&gt;(&quot;client_secret&quot;, &quot;..client secret.....&quot;),
new KeyValuePair&lt;string, string&gt;(&quot;grant_type&quot;,&quot;client_pc_credentials&quot;)});
content.Headers.ContentType = new MediaTypeHeaderValue(&quot;application/x-www-form-urlencoded&quot;);
content.Headers.ContentType.CharSet = &quot;UTF-8&quot;;
client.DefaultRequestHeaders.ExpectContinue = false;
HttpResponseMessage response = await client.PostAsync(new Uri(&quot;https://apis-sandbox.fedex.com/oauth/token&quot;), content);
        
return await response.Content.ReadAsStringAsync();

I call this method like var oAuth = MakeApiCallAsync(); This code runs without errors, but it doesn't hit the last code line (return await response.Content.ReadAsStringAsync();) and the flow continues to the calling method and the variable oAuth gets value of (Result = Yet not computed, ..... other properties)

I tried nearly all the methods and I am exhausted, any ideas what is wrong ?

答案1

得分: 0

以下是翻译好的部分:

你需要读取你的请求的响应代码,类似下面的方式。这将让你知道你的请求是否成功通过或是否出现任何错误。

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://apis-sandbox.fedex.com/oauth/token");
HttpContent content = new FormUrlEncodedContent(
    new List<KeyValuePair<string, string>> {
        new KeyValuePair<string, string>("client_id", "...客户端ID..."),
        new KeyValuePair<string, string>("client_secret", "..客户端秘钥....."),
        new KeyValuePair<string, string>("grant_type","client_pc_credentials")}
);
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
content.Headers.ContentType.CharSet = "UTF-8";
client.DefaultRequestHeaders.ExpectContinue = false;
HttpResponseMessage response = await client.PostAsync(new Uri("https://apis-sandbox.fedex.com/oauth/token"), content);
if (response.IsSuccessStatusCode)
{
    return await response.Content.ReadAsStringAsync();
}
else{
    //处理API请求错误的代码   
}
英文:

You need to read the response code of your request, something like below. This will let you know if your request is going through successfully or getting any errors.

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, &quot;https://apis-sandbox.fedex.com/oauth/token&quot;);
HttpContent content = new FormUrlEncodedContent(
new List&lt;KeyValuePair&lt;string, string&gt;&gt; {
new KeyValuePair&lt;string, string&gt;(&quot;client_id&quot;, &quot;...clientid...&quot;),
new KeyValuePair&lt;string, string&gt;(&quot;client_secret&quot;, &quot;..client secret.....&quot;),
new KeyValuePair&lt;string, string&gt;(&quot;grant_type&quot;,&quot;client_pc_credentials&quot;)});
content.Headers.ContentType = new MediaTypeHeaderValue(&quot;application/x-www-form-urlencoded&quot;);
content.Headers.ContentType.CharSet = &quot;UTF-8&quot;;
client.DefaultRequestHeaders.ExpectContinue = false;
HttpResponseMessage response = await client.PostAsync(new Uri(&quot;https://apis-sandbox.fedex.com/oauth/token&quot;), content);
if (response.IsSuccessStatusCode)
{
  return await response.Content.ReadAsStringAsync();
}
 else{
    //api request error handling code   
}

huangapple
  • 本文由 发表于 2023年4月17日 21:12:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035547.html
匿名

发表评论

匿名网友

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

确定