Get OAuth 2.0 access token of Paypal using HttpClient in c#
Here called paypal Rest API using System.Net.Http.HttpClient for OAuth 2.0 access token
try
{
// ClientId of your Paypal app API
string APIClientId = "ASCxaD7FzuvGbX-xxxxxxxx.....xxxxxxxxxxx-6RZDgwLuOyrCsy3";
// secret key of you Paypal app API
string APISecret = "ENldyFXuW46R7Wv0xxxxxxxx.....xxxxxxxxxxxee8pyH8";
using (var client = new System.Net.Http.HttpClient())
{
var byteArray = Encoding.UTF8.GetBytes(APIClientId+ ":" + APISecret );
client.DefaultRequestHeaders.Authorization = new Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var url = new Uri("https://api.sandbox.paypal.com/v1/oauth2/token", UriKind.Absolute);
client.DefaultRequestHeaders.IfModifiedSince = DateTime.UtcNow;
var requestParams = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("grant_type", "client_credentials")
};
var content = new FormUrlEncodedContent(requestParams);
var webresponse = await client.PostAsync(url, content);
var resp = await webresponse.Content.ReadAsStringAsync();
// response will deserialized using Jsonconver
//return JsonConvert.DeserializeObject<PayPalGetTokenResponse>(resp);
}
}
catch (System.Exception ex)
{
}
try
{
// ClientId of your Paypal app API
string APIClientId = "ASCxaD7FzuvGbX-xxxxxxxx.....xxxxxxxxxxx-6RZDgwLuOyrCsy3";
// secret key of you Paypal app API
string APISecret = "ENldyFXuW46R7Wv0xxxxxxxx.....xxxxxxxxxxxee8pyH8";
using (var client = new System.Net.Http.HttpClient())
{
var byteArray = Encoding.UTF8.GetBytes(APIClientId+ ":" + APISecret );
client.DefaultRequestHeaders.Authorization = new Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var url = new Uri("https://api.sandbox.paypal.com/v1/oauth2/token", UriKind.Absolute);
client.DefaultRequestHeaders.IfModifiedSince = DateTime.UtcNow;
var requestParams = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("grant_type", "client_credentials")
};
var content = new FormUrlEncodedContent(requestParams);
var webresponse = await client.PostAsync(url, content);
var resp = await webresponse.Content.ReadAsStringAsync();
// response will deserialized using Jsonconver
//return JsonConvert.DeserializeObject<PayPalGetTokenResponse>(resp);
}
}
catch (System.Exception ex)
{
}
Thank you!!
ReplyDeleteOne question whats is the "client.DefaultRequestHeaders.IfModifiedSince = DateTime.UtcNow;" for?