简体   繁体   中英

Paypal Java script integration

Want to integrate Paypal with my mobile web application. I tried to get the access token using client id and secret id but unable to get the access token.

Below is the sample Ajax call with I am making to retrieve the access token.

function getAccessToken(){
        $.ajax({
        url:"https://api.sandbox.paypal.com/v1/oauth2/token/",
        type:"POST",
        data : {"grant_type":"client_credentials"},
        beforeSend: function (request)
                {
                    request.setRequestHeader("Accept", "application/json");
                    request.setRequestHeader("Accept-Language", "en_US");
                    request.setRequestHeader("Authorization", "abc XXXXX:XXXXXXXXXXXXX");
                },
            success: function(data) {    
                alert(data);
            },
            error: function(e, messgae,type){
                alert("Error" + e +"          "+messgae+"         type         "+type);
            }
     });

I am unable to retrive the access token from the server. Can anyone please tell me how can I integrate Paypal with my mobile web application using java script?

after a series of try and fail I found the correct AJAX call:

$.ajax({
        headers: {
             "Accept": "application/json",
             "Accept-Language": "en_US",
             "Authorization": "Basic "+btoa("**<Client ID>:<Secret>**")
        },
        url: "https://api.sandbox.paypal.com/v1/oauth2/token",
        type: "POST",
            data: "grant_type=client_credentials",
        complete: function(result) {
            alert(JSON.stringify(result));
        },
});

You need to replace Client ID:Secret with the one that you find on your Developer Dashboard, for example AxxhGDh:hudh-X-h8qi

Above example doesn't works, below works for me:

var parameter = {
    "grant_type": "client_credentials",
    "username": "<username>",
    "password": "<password>"
}

$.ajax({
    headers: {
        "Accept": "application/json",
        "Accept-Language": "en_US",
        "Authorization": "Basic <your auth key>"
    },
    url: "https://api.sandbox.paypal.com/v1/oauth2/token",
    type: "POST",
    data: parameter,
    complete: function (result) {
        alert(JSON.stringify(result));
    },
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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