Planbox / planbox_api_sandbox

Single Application Sandbox for Planbox API
www.planbox.com/api
2 stars 1 forks source link

Example code to get resource #1

Open TheBrockEllis opened 9 years ago

TheBrockEllis commented 9 years ago

I was wondering if you could provide an example jQuery snippet that would be used to grab the products. I've been fiddling with the code in this repo, my own version of it as well as cURLing the the API and I can't seem to come up with the magical combination of access_token, username, password, email, GET/POST, etc.

Here is the closest thing I can (taken from this repo)

        window.Planbox = {

        // Url to Planbox. You can use https if you like.
        base_url: 'https://acceess_token_as_username:fake_password@www.planbox.com/',

        // Call the Planbox API
        api: function(fname, args, onsuccess, onerror) {
            var url = Planbox.base_url+'api/'+fname+'?callback=?';
            args = args || {};
            //args = { product_id: "123456778", email: "me@domain.com" };

            return $.post(url, args, function(data) {
                if (data && data.code == 'ok') {
                    if (onsuccess) onsuccess(data.content);
                } else if (data && data.code == 'error') {
                    if (onerror) onerror(data.content);
                } else {
                    if (onerror) onerror('Cannot reach Planbox');
                }
            }, "json");
        },

I'm just very confused about the using POST when the intention is to return data and not submit anything. Any clarification would be amazing.

mateomurphy commented 9 years ago

Hi,

Some browsers don’t like basic auth params passed in the url like that; try passing the access_token as a GET/POST parameter instead.

Regarding all API calls being POSTS, yes this is confusing, and it will change in the near future as we improve our API.

TheBrockEllis commented 9 years ago

Hey there mateomurphy!

Thanks so much for the help! Popping the access token into the args for the getJSON worked like a charm. Here's what the function looks like now (for posterity's sake)

access_token: "234df4tr9fdg0-9asd0sdf09sd9s",    

// Call the Planbox API
        api: function(fname, args, onsuccess, onerror) {
            var url = Planbox.base_url+'api/'+fname+'?callback=?';
            args = args || {};
            args.access_token = Planbox.access_token;
            return $.getJSON(url, args, function(data) {
                if (data && data.code == 'ok') {
                    if (onsuccess) onsuccess(data.content);
                } else if (data && data.code == 'error') {
                    if (onerror) onerror(data.content);
                } else {
                    if (onerror) onerror('Cannot reach Planbox');
                }
            }, "json");
        },

Thanks so much for the help. Cant wait to start extending some things to match our workflow!