Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

c# - Getting PayPal Access Token with RestSharp returning "Status Code: NotFound"

Getting PayPal Access Token not working

I'm getting the response:

"StatusCode: NotFound, Content-Type: application/json, Content-Length: 131)"

My code, using 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}. Although {refresh_token} is static, I'm not sure if you're supposed to populate this with a value, or how to do so.

Does anybody know what I'm doing wrong - and if my suspicions regarding the issue being the grant_type being correct?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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. So, when you use new RestRequest("oauth2/token", ...), the URL you end up fetching is

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") 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...