简体   繁体   English

使用 RestSharp 获取 PayPal 访问令牌返回“状态代码:未找到”

[英]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.

相关问题 RavenDb - 状态代码:NotFound - RavenDb - Status Code: NotFound 使用 RestSharp 获取 ExactOnline 的访问令牌时出现 400 Bad Request - 400 Bad Request when getting access token for ExactOnline using RestSharp 刷新访问令牌失败,状态码为:401 - Refresh access token failed with status code: 401 Microsoft Graph OnlineMeeting API 返回状态:NotFound (404) 错误 - Microsoft Graph OnlineMeeting API returning Status: NotFound (404) error RestSharp + Xamarin - “请求失败,状态码 InternalServerError” - RestSharp + Xamarin - "Request failed with status code InternalServerError" “向您的机器人发送此消息时出错:HTTP 状态代码未找到” - “There was an error sending this message to your bot: HTTP status code NotFound” 订阅C#中未找到的Outlook / Office 365 API状态代码 - Subscribe to Outlook/Office 365 API Status Code NotFound in C# CustomVision API返回“操作返回了无效的状态码:&#39;NotFound&#39;” - CustomVision API returns “Operation returned an invalid status code: 'NotFound'” 获取NotFound尝试使用WebClient访问google.com - Getting NotFound trying to access google.com with WebClient OAuth2 承载令牌未通过 RestSharp 调用发送 - OAuth2 Bearer Token not getting sent with RestSharp call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM