PayPal API oauth2请求在C#中:401未授权

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

PayPal API oauth2 request in C#: 401 unauthorized

问题

我试图使用用户名和密码向PayPal API URL发送身份验证请求,但运行代码时出现错误:> 远程服务器返回错误:(401) 未经授权的错误。

这是代码:

private static HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api-m.sandbox.paypal.com/v1/oauth2/token");

static void Main(string[] args)
{
    string username = "..client id..";
    string password = "..client secret..";

    string svcCredentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(username + ":" + password));
    request.Method = WebRequestMethods.Http.Post;
    request.Accept = "application/json";
    request.Headers.Add("Authorization", "Basic " + svcCredentials);

    string text;
    var response = (HttpWebResponse)request.GetResponse();

    using (var sr = new StreamReader(response.GetResponseStream()))
    {
        text = sr.ReadToEnd();
    }

    Console.WriteLine(text);
    Console.Read();
}
英文:

I am trying to send auth request to PayPal API URL with username and password when I run the code I get:
> The remote server returned an error: (401) Unauthorized Error.

This is the code:

private static HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api-m.sandbox.paypal.com/v1/oauth2/token");

        static void Main(string[] args)
        {
            string username = "..client id..";
            string password = "..client secret..";

            string svcCredentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(username + ":" + password));
            request.Method = WebRequestMethods.Http.Post;
            request.Accept = "application/json";
            request.Headers.Add("Authorization", "Basic " + svcCredentials);

            string text;
            var response = (HttpWebResponse)request.GetResponse();

            using (var sr = new StreamReader(response.GetResponseStream()))
            {
                text = sr.ReadToEnd();
            }

            Console.WriteLine(text);
            Console.Read();
        }

答案1

得分: 1

看起来您缺少一个请求体,其中包含内容:grant_type=client_credentials

请参阅 PayPal 的REST API身份验证指南,其中包含curl和postman示例。

英文:

It appears you're missing a request body with the contents: grant_type=client_credentials

See PayPal's guide for REST API authentication, which has curl and postman samples.

huangapple
  • 本文由 发表于 2023年3月12日 14:57:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711524.html
匿名

发表评论

匿名网友

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

确定