[英]Getting PayPal Access Token with RestSharp returning "Status Code: NotFound"
Getting PayPal Access Token not working获取 PayPal 访问令牌不起作用
I'm getting the response:我得到了回应:
"StatusCode: NotFound, Content-Type: application/json, Content-Length: 131)" “状态码:未找到,内容类型:应用程序/json,内容长度:131)”
My code, using RestSharp我的代码,使用 RestSharp
public void getToken()
{
var clientId = "{client-ID}";
var secret = "{client-secret}";
if (ServicePointManager.SecurityProtocol != SecurityProtocolType.Tls12) ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; // forced to modern day SSL protocols
var client = new RestClient("https://api.paypal.com/v1/oauth2/token") { Encoding = Encoding.UTF8 };
var authRequest = new RestRequest("oauth2/token", Method.POST) { RequestFormat = DataFormat.Json };
client.Authenticator = new HttpBasicAuthenticator(clientId, secret);
authRequest.AddParameter("grant_type", "refresh_token&refresh_token={refresh_token}");
var authResponse = client.Execute(authRequest);
}
I do have a slight feeling my grant_type
may be incorrect, as I'm not 100% sure what actually goes here, after a bit of research it still isn't clear to me - but I went with refresh_token&refresh_token={refresh_token}
.我确实有点感觉我的grant_type
可能不正确,因为我不是 100% 确定这里实际发生了什么,经过一些研究后我仍然不清楚 - 但我选择了refresh_token&refresh_token={refresh_token}
。 Although {refresh_token}
is static, I'm not sure if you're supposed to populate this with a value, or how to do so.虽然{refresh_token}
是 static,但我不确定您是否应该用一个值填充它,或者如何填充它。
Does anybody know what I'm doing wrong - and if my suspicions regarding the issue being the grant_type
being correct?有谁知道我做错了什么 - 如果我怀疑问题是grant_type
是正确的?
You've doubled up the path.你把路径翻了一番。
new RestClient("https://api.paypal.com/v1/oauth2/token")
This line sets that URL as the base address , which is prepended to your other path strings.此行将 URL 设置为base address ,它被添加到您的其他路径字符串之前。 So, when you use new RestRequest("oauth2/token", ...)
, the URL you end up fetching is因此,当您使用new RestRequest("oauth2/token", ...)
时,您最终获取的 URL 是
https://api.paypal.com/v1/oauth2/token/oauth2/token
Just remove the path string from the base address, so you have:只需从基地址中删除路径字符串,您就有:
new RestClient("https://api.paypal.com/v1")
I agree with the above but have tidied the answer up a little我同意上述观点,但已经整理了一些答案
public string GetAccessToken()
{
var clientId = "my_client_id";
var secret = "my_password";
var client = new RestClient("https://api-m.sandbox.paypal.com/") { Encoding = Encoding.UTF8 };
var authRequest = new RestRequest("v1/oauth2/token", Method.POST) { RequestFormat = DataFormat.Json };
client.Authenticator = new HttpBasicAuthenticator(clientId, secret);
authRequest.AddParameter("grant_type", "client_credentials");
var response = client.Execute(authRequest);
AccessToken token = JsonConvert.DeserializeObject<AccessToken>(response.Content);
return token.access_token;
}
public class AccessToken
{
public string scope { get; set; }
public string access_token { get; set; }
public string token_type { get; set; }
public string app_id { get; set; }
public int expires_in { get; set; }
public string nonce { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.