我正在使用.NET连接到PayPal以获取访问令牌。我正在关注PayPal上提供的纪录片。https://developer.paypal.com/docs/api/overview/#make-your-first-call
这就是我到目前为止所拥有的。大多从这里去。尽管它连接,但不会返回任何内容。我在Postman上尝试了它,并获得了带有访问令牌的JSON对象,但是这里什么也没有返回。
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.sandbox.paypal.com/v1/oauth2/token");
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(clientId + ":" + clientSecret));
request.Accept = "application/json";
request.Headers.Add("Accept-Language", "en_US");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Timeout = 10000;
byte[] postBytes = Encoding.ASCII.GetBytes("grant_type=client_credentials");
Stream postStream = request.GetRequestStream();
postStream.Write(postBytes, 0, postBytes.Length);
postStream.Flush();
postStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
这就是我从断点上得到的。
response.CharacterSet = ""
response.ContentLength = 899
response.StatusCode = OK
找到了答案。以下代码提供了JSON对象。
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
Console.WriteLine(responseString); // This will display the answer as JSON, now just parse it.
遵循PayPal的.NET SDK快速启动指南。您可以进行身份验证并获取访问令牌如下:
using PayPal.Api;
// Authenticate with PayPal
var config = ConfigManager.Instance.GetProperties();
var accessToken = new OAuthTokenCredential(config).GetAccessToken();