Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么在创建令牌时会创建错误_C#_Asp.net_Api_Httpclient_Access Token - Fatal编程技术网

C# 为什么在创建令牌时会创建错误

C# 为什么在创建令牌时会创建错误,c#,asp.net,api,httpclient,access-token,C#,Asp.net,Api,Httpclient,Access Token,为什么响应。IsSuccessStatusCode显示状态代码401?什么原因导致故障? 什么操作会导致成功?指定应使用传递用户名和密码,并应传递包含grant\u type=client\u凭据的表单编码正文 此时,您的代码添加了grant_type作为标题,并将用户名和密码作为JSON对象发布到正文中 按照文档所述的方式修改代码,我们得到: public static void CreateToken() { HttpClient client = new Htt

为什么响应。IsSuccessStatusCode显示状态代码401?什么原因导致故障? 什么操作会导致成功?

指定应使用传递用户名和密码,并应传递包含
grant\u type=client\u凭据的表单编码正文

此时,您的代码添加了
grant_type
作为标题,并将用户名和密码作为JSON对象发布到正文中

按照文档所述的方式修改代码,我们得到:

 public static void CreateToken()
    {

        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Add("grant_type", "client_credentials");
        var UserPassJson = "{\"username\": \"mucode\",\"password\": \"mypassword\"}";

        HttpContent content = new StringContent(UserPassJson, Encoding.UTF8, "application/json");

        var response = client.PostAsync(new Uri("https://api.sandbox.paypal.com/v1/oauth2/token"), content).Result;
        if (response.IsSuccessStatusCode)
        {
            var responseContent = response.Content;
            string responseString = responseContent.ReadAsStringAsync().Result;
            Console.WriteLine(responseString);
        }
    }
HttpClient=newhttpclient();
byte[]authBytes=System.Text.Encoding.ASCII.GetBytes(“mucode:mypassword”);
string base64Auth=Convert.ToBase64String(authBytes);
client.DefaultRequestHeaders.Authorization=new System.Net.Http.Headers.AuthenticationHeaderValue(“Basic”,base64Auth);
HttpContent=newformurlencodedcontent(new[]{newkeyvaluepair(“授权类型”、“客户端凭据”)});
var response=client.PostAsync(新Uri(“https://api.sandbox.paypal.com/v1/oauth2/token)结果;
if(响应。IsSuccessStatusCode)
{
var responseContent=response.Content;
string responseString=responseContent.ReadAsStringAsync().Result;
控制台。写入线(响应线);
}

附言:我建议大家阅读并跟进。我还建议将此方法设置为异步
,并将链设置为异步。

缺少什么?在文档中,用于传递用户名和密码,正文是指定
授权类型=客户端凭据的表单编码数据
。相比之下,您添加了一个标题
grant\u type
,并将用户名和密码作为JSON对象传递。非常感谢它的工作!问题:只要不使用access_令牌,它就不会改变吗?我不确定它对PayPal有多好(我没有使用他们的API)。您应该在响应中检查
expires\u以查看它何时过期。通常,很多OAuth API允许您请求一个“offline_access”作用域,该作用域还将在您的访问令牌过期时提供一个刷新令牌(您使用刷新令牌请求一个新的访问令牌),尽管我不确定它是否与PayPal类似。在文档页面上搜索“刷新”似乎并没有显示任何信息,所以在PayPal上可能不可能,如果您获得401,您只需要重新验证?
HttpClient client = new HttpClient();
byte[] authBytes = System.Text.Encoding.ASCII.GetBytes("mucode:mypassword");
string base64Auth = Convert.ToBase64String(authBytes);
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", base64Auth);

HttpContent content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("grant_type", "client_credentials") });

var response = client.PostAsync(new Uri("https://api.sandbox.paypal.com/v1/oauth2/token"), content).Result;
if (response.IsSuccessStatusCode)
{
    var responseContent = response.Content;
    string responseString = responseContent.ReadAsStringAsync().Result;
    Console.WriteLine(responseString);
}