C# 中的 PayPal API oauth2 请求:401 unauthorized

问题描述 投票:0回答:1

当我运行我得到的代码时,我正在尝试使用用户名和密码向 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();
        }
c# paypal
1个回答
0
投票

仔细看,好像少了个身体

grant_type=client_credentials

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

© www.soinside.com 2019 - 2024. All rights reserved.