当我运行我得到的代码时,我正在尝试使用用户名和密码向 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();
}