Maor Cohen
Maor Cohen

Reputation: 956

Get request for my Paypal AccessToken

I am trying to get my access token of paypal.

I have the next parameters: EndPoint, Client Id, secret, api username, api signature, api password, application Id.

should I need a paypal client in order to do it?

I followed this link: https://developer.paypal.com/docs/integration/direct/make-your-first-call/

and tried:

private string getAccessToken() {
    var ppClient; // = new paypalClient(); // create a paypal client

    Dictionary<string, object> parameters = new Dictionary<string, object>();

    parameters.Add("Accept", "application/json");
    parameters.Add("Accept-Language", "en_US");
    parameters.Add("grant_type", "client_credentials");

    var result = ppClient.Get("https://api.sandbox.paypal.com/v1/oauth2/token", parameters);

    string accessToken = result["access_token"];
    return accessToken;
}

thank you all!

Upvotes: 5

Views: 4162

Answers (2)

DubMan
DubMan

Reputation: 460

Came to this a little late, but ran into similar problem. In the end I had to use a "Basic" authentication implementation to get the details back from PayPal. This is my Token Collector class. The Only thing you really need to do is substitute my PayPal options injection with your 2 paypal client_Id and Secret strings generated in your paypal set up.

Ignore my AccessToken return type, its just a PoCo I made up and the ReadToObject is just a simple deserializer using newtonsoft. The real good stuff is returned to the 'result' string.

Hope this helps!

    public sealed class PaymentTokenServer : IPaymentTokenServer
{
    private readonly List<KeyValuePair<string, string>> tokenServerPairs = new List<KeyValuePair<string, string>>();

    private PayPalOptions payPalOptions;

    public PaymentTokenServer(IOptions<PayPalOptions> paypalOptions)
    {
        this.payPalOptions = paypalOptions.Value;

       this.tokenServerPairs.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));
    }

    public AccessToken GetToken()
    {
        var content = new FormUrlEncodedContent(this.tokenServerPairs);
        ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri(this.payPalOptions.TokenServerUrl);
            client.DefaultRequestHeaders.AcceptLanguage.Add( new StringWithQualityHeaderValue("en_US"));

            var base64String = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{this.payPalOptions.ClientId}:{this.payPalOptions.Secret}"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64String);

            var response = client.PostAsync("", content).Result;

            var result = response.Content.ReadAsStringAsync().Result;

            return result.ReadToObject<AccessToken>();
        }
    }
}

Upvotes: 0

Cueball 6118
Cueball 6118

Reputation: 547

I would recomend using RestSharp (just grab the NuGet package): -

        if (ServicePointManager.SecurityProtocol != SecurityProtocolType.Tls12) ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; // forced to modern day SSL protocols
        var client = new RestClient(payPalUrl) { Encoding = Encoding.UTF8 };
        var authRequest = new RestRequest("oauth2/token", Method.POST) {RequestFormat = DataFormat.Json};
        client.Authenticator = new HttpBasicAuthenticator(clientId, secret);
        authRequest.AddParameter("grant_type","client_credentials");
        var authResponse = client.Execute(authRequest);

Upvotes: 4

Related Questions