英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论