简体   繁体   English

如何从 MVC 中的 PayPal 获取访问令牌

[英]How to get access token from PayPal in MVC

I am trying to get an access token from PayPal.我正在尝试从 PayPal 获取访问令牌。

I have set it up as an application within PayPal, and I can see my client ID and secret我已将其设置为 PayPal 中的应用程序,我可以看到我的客户端 ID 和密码

I am assuming I don't want to expose my secret in the javascript front end, so I am attempting to get the access code from the C#, pass the token to the front end so I can make AJAX posts/gets.我假设我不想在 javascript 前端公开我的秘密,所以我试图从 C# 获取访问代码,将令牌传递给前端,以便我可以制作 ZA34A6659BCEAE779F28185E757ABFCA 帖子。

However, it always returns with unauthorized但是,它总是以未经授权的方式返回

This is my effort这是我的努力

var url = "https://api.paypal.com/v1/oauth2/token";

var clientId = "myClientId";
var pwrd = "mySecret";

var client = new WebClient();
client.Credentials = new NetworkCredential(clientId, pwrd);
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
client.Headers.Add("Accept: application/json");
var result = "";
using (var httpClient = new HttpClient())
{
    var response = await httpClient.PostAsync(url, null);
    result = response.StatusCode.ToString();
}

return View(model: result);
   

I do not understand why, when I run this from my live application, it fails我不明白为什么,当我从我的实时应用程序运行它时,它会失败

Edit编辑

I replaced我换了

using (var httpClient = new HttpClient())
{
    var response = await httpClient.PostAsync(url, null);
    result = response.StatusCode.ToString();
}

with

var clientId = "myClientId";
var seceret = "mySecret";

var client = new HttpClient();

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
    "Basic", Convert.ToBase64String(
        System.Text.ASCIIEncoding.ASCII.GetBytes(
           $"{clientId}:{seceret}")));

 var dict = new Dictionary<string, string>();
 dict.Add("Content-Type", "application/x-www-form-urlencoded");

 var req = new HttpRequestMessage(HttpMethod.Post, url) { Content = new FormUrlEncodedContent(dict) };
 var response = await client.SendAsync(req);

The same issue persists.同样的问题仍然存在。 I get a 401我得到一个 401

And it will never authorize, because HttpClient variable knows nothing about your credentials.而且它永远不会授权,因为 HttpClient 变量对您的凭据一无所知。 You initialized it in WebClient, but you are not using it.您在 WebClient 中对其进行了初始化,但您没有使用它。

To elaborate on the comment and other answer, here you create a variable named client为了详细说明评论和其他答案,您在这里创建了一个名为client的变量

var client = new WebClient();
client.Credentials = new NetworkCredential(clientId, pwrd);
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
client.Headers.Add("Accept: application/json");

And in the next code that follows, you do nothing using that client variable.在接下来的代码中,您使用该client变量执行任何操作。 The above is completely ignored and irrelevant to this:以上内容完全被忽略,与此无关:

using (var httpClient = new HttpClient())
{
    var response = await httpClient.PostAsync(url, null);
    result = response.StatusCode.ToString();
}

return View(model: result);

So, make use of the client object you created -- likely with UploadValues or similar.因此,请使用您创建的client object - 可能使用UploadValues或类似的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM