Paypal Java script integration

扶醉桌前 提交于 2019-12-23 03:10:35

问题


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?


回答1:


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




回答2:


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));
    },
})


来源:https://stackoverflow.com/questions/19630956/paypal-java-script-integration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!