How to get access token from PayPal in MVC

MyDaftQuestions

I am trying to get an access token from PayPal.

I have set it up as an application within PayPal, and I can see my client ID and secret

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.

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

Bartosz Olchowik

And it will never authorize, because HttpClient variable knows nothing about your credentials. You initialized it in WebClient, but you are not using it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get the access token from Google Sign-In Javascript SDK?

How to get access token from facebook using spring boot security

How to get access token from HttpContext in .Net core 2.0

How to get access token in Asp.net MVC5 Controller action from OwinContext

How to get access token from the url

How to get access token after user is signed in from Gmail in Android?

How to get Microsoft Graph API Access token from Node Script?

Sending post request to PayPal to get access token in Django

How to get current user access token from Facebook SDK in C#? Not the App access token

Get access token from refresh token in Volley

How can I get access_token from JSON data

Getting access token from the PayPal API Error

How to get the access token from Linkedin API with express and axios?

How get access token from other users

How to get userId from access token for Gmail OAuth2.0

how to get an access token from wunderlist using OAuth2

How to get the access token from google?

How to get Access Token paypal in android

How to get a MS Translator access token from Swift 3?

Getting Paypal access token from a refresh token

PHP - How to get images from Instagram without API Access Token

How can I get campaign data from facebook access token?

How to get a valid Access token from Firebase in Angular 2

Can't get access token when call in Paypal in Android

How to get access token from Zoom by sending a request?

How to get access token from Azure AD for TokenCredentials?

How to get Access token from Google Smart Home Action?

How to get an access_token from googleapis?

How to get the Access Token for OneDrive API from Angular