Okay
  Public Ticket #1901173
Consuming JSON API with authentication
Closed

Comments

  •  2
    VITGT started the conversation

    I'm interested to use this plugin but the API I'm working on must use authentication, since the data is sensitive.

    is there any way to get data from an API endpoint that requires authentication or token?

    Thanks in advance!

  •  472
    Isidora replied

    Hi VITGT,
    Thank you for your interest in wpdatatables.

    When working with JSON format please note: Currently, wpDataTables accepts this, and only this, format of JSON (see example): a one-level array of same-structured objects where each object will be parsed as a row, and each field inside of the object as a cell. If your software does not return this format, you would need to prepare, as an example some PHP adapter to return one of the formats that wpDataTables would accept.

    You can read more about this in our documentation on this link.

    We have Lite version of wpdatatables which you can try for free.

    wpDataTables Lite is a basic version of a popular best-selling WordPress table plugin. While some premium features are reduced, wpDataTables Lite is still quite a handy tool that allows you to quickly create tables in WordPress from different sources. You can download it from this link.

    You can try our sandbox site: Front-end & Back-end - you can find a fully functional version there to try out all plugin features.

    Best regards.

    Kind Regards, 

    Isidora Markovic

    wpDataTables: FAQFacebookTwitterFront-end and back-end demoDocs

    Amelia: FAQFacebookTwitter |  Amelia demo sites | Docs

    You can try our wpDataTables add-ons before purchase on these sandbox sites:

    Powerful FiltersGravity Forms Integration for wpDataTablesFormidable Forms Integration for wpDataTables

  •  2
    VITGT replied

    Thank you Milan, but does it support authentication headers?  how should I pass it to your plugin?

    Thanks again!

  •  472
    Isidora replied

    Hi VITGT,

    Unfortunately at the moment it is not possible with option Create table from JSON format.  However, it appears in requests from time to time, so we will add to our idea list and look into some possible solutions on plugin side in the future.

    Best regards.

    Kind Regards, 

    Isidora Markovic

    wpDataTables: FAQFacebookTwitterFront-end and back-end demoDocs

    Amelia: FAQFacebookTwitter |  Amelia demo sites | Docs

    You can try our wpDataTables add-ons before purchase on these sandbox sites:

    Powerful FiltersGravity Forms Integration for wpDataTablesFormidable Forms Integration for wpDataTables

  •  1
    scoutin replied

    Any progress on this feature? Security is definitely needed.

  •  472
    Isidora replied

    Hi scoutin,

    Unfortunately not. This feature for now was not requested from users from time to time so it is not added on our TO DO list. Like I said in previous post if a lot of users request such features we will decently add it on our TO DO list in the future.

    Thank you for understanding.

    Best regards.

    Kind Regards, 

    Isidora Markovic

    wpDataTables: FAQFacebookTwitterFront-end and back-end demoDocs

    Amelia: FAQFacebookTwitter |  Amelia demo sites | Docs

    You can try our wpDataTables add-ons before purchase on these sandbox sites:

    Powerful FiltersGravity Forms Integration for wpDataTablesFormidable Forms Integration for wpDataTables

  • Jurgen replied

    I'll add my voice to this one as well - can't believe that it doesn't support authentication. 

    We're even looking to use SAS tokens, however my developer tells me that they are revealed in the sourcecode of the site ?

  •  1
    cpUSA replied

    We devs MUST be able authenticate external feed requests... this is a fairly critical use of any external datasource.  There are so many uses for this outstanding plugin, but most external feeds of any critical nature will require authentication - even more so if we do not own the JSON feed.  

    I saw a very old ticket from 2017 even asking about this.  You pointed the user to build the construct in this ticket. https://tmsplugins.ticksy.com/ticket/1362224/.  However, that would apply to every table, as I read it.  We need to be able to hack this together on a per-table/JSON source basis if you aren't going to build the interface to handle a fairly important feature like this.

    There must be something we can edit in a server side file or db to find and reconstruct a specific JSON feed referenced by a table's datasource provided when building the table in your wpDataTables plugin.  

    This is important, and while maybe you don't want to build a UX for it just yet, I think it's fair for us paying customers to ask for details on the architecture of your plugin and a workaround until you prioritize this as a feature.  It's critical for any serious work.

    Would you please tell us techies how to find the JSON feed request construct for a specific wpDataTables table in the database (or plugin configuration files) so we can add the authentication header for that specific table's feed request:

    1. How do we find the construct for a specific table's JSON feed?
    2. What do we edit there to add an authentication header to the request?

    Much appreciated - whatever you can help with here.  Please don't ignore this important request.

  •  472
    Isidora replied

    Hi cpUSA,

    Thank you for your interest in wpdatatables.

    When you create wpdatatables based on JSON in database in table wpdatatable wll be saved that URL in column content. That content then use wpdatatables engine  to get data over method jsonBasedConstruct in which we use CURL and you can find in file wp-content/plugins/wpdatatables/source/class.wpdatatable.php

    You will need to customize method curlGetData which is helper function that returns curl data where you will set authentication header(in file wp-content/plugins/wpdatatables/source/class.wdttools.php) Please note that this function is used for other tables type so if you have only tables from JSON then that's fine, but if not then you need  to adopt for other table types as well that use this method.

    Other option is to use PHP adopter option Creating Tables in WordPress based on serialized PHP arrays, where you can create your own logic to get data from  JSON API with authentication and adopt in format for wpdatatables.

    We carefully follow customers requirements and try to implement most of them if they appear in requests from time to time.

    We constantly strive to make our product better, and deliver the most value for our end users - so please share your feedback on what features you need in next versions on this link.

    Best regards.

    Kind Regards, 

    Isidora Markovic

    wpDataTables: FAQFacebookTwitterFront-end and back-end demoDocs

    Amelia: FAQFacebookTwitter |  Amelia demo sites | Docs

    You can try our wpDataTables add-ons before purchase on these sandbox sites:

    Powerful FiltersGravity Forms Integration for wpDataTablesFormidable Forms Integration for wpDataTables

  • Marcus replied

    I am also interested in getting JSON from an Authenticated source. I would go as far as to say that without the ability to Authenticate within the plugin, it is not usable for me, and probably many other potential customers. If you add this, you may find that more people buy your plugin.

    Hope this helps


  • David Johnson replied

    Hi 

    Just to add more customer feedback that it is important to us that wpdatatables supports authenticated JSON API.

    Hope this helps.

  •  4
    Daniel Lewis replied

    I was just about to submit a ticket for this same thing.

    Has anyone figured out a way to pass authentication via a request header? I really need this feature or the code to work around the lack of support.

  •  4
    Daniel Lewis replied

    I found a rough workaround. It will apply to all CURL requests, but this could probably be done more intelligently. Unfortunately, it requires editing the plugin file source/class.wdttools.php

    Find the function curlGetData, and in the section of "curl_setopt" declarations, add this:

    curl_setopt($ch, CURLOPT_HTTPHEADER, array('API-Key: YOUR-API-KEY'));

    Change "API-Key" and "YOUR-API-KEY" to what you need.

    If you need some other authentication method, check out the options on https://www.php.net/manual/en/function.curl-setopt.php.

    This is a very messy way to do this since a plugin update would overwrite the changes.

  •  472
    Isidora replied

    Hi to all,

    Thank you for your interest in wpdatatables.

    This will be added on our road map, but I can not tell you when it will be exactly, because there are priority features that have to be done first. Until that you can use a hook for filtering  curl options  that will be available in next update, so you will not have to replace your custom data again.

    What you need to do is to replace function curlGetData in file wp-content/plugins/wpdatatables/source/class.wdttools.php around line 195 with this

    /**
         * Helper function that returns curl data
         * @param $url
         * @return mixed|null
         * @throws Exception
         */
        public static function curlGetData($url)
        {
            $ch = curl_init();
            $timeout = 5;
            $agent = 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0';
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            curl_setopt($ch, CURLOPT_USERAGENT, $agent);
            curl_setopt($ch, CURLOPT_REFERER, site_url());
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            $data = apply_filters('wpdatatables_curl_get_data', null, $ch, $url);
            if( null === $data ) {
                $data = curl_exec($ch);
                if (curl_error($ch)) {
                    $error = curl_error($ch);
                    curl_close($ch);
                    throw new Exception($error);
                }
                if (strpos($data, '<title>Moved Temporarily</title>')) {
                    throw new Exception(__('wpDataTables was unable to read your Google Spreadsheet, probably it is not published correctly. <br> You can publish it by going to <b>File -> Publish to the web</b> ', 'wpdatatables'));
                }
                $info = curl_getinfo($ch);
                curl_close($ch);
                if ($info['http_code'] === 404) {
                    return NULL;
                }
            }
            return $data;
        }
    

    and then in functions.php of your theme or child theme to insert your custom code

    function filterCURLOptions($data, $ch, $url){
        //
        // $data (null) is set to null if there are available filter
        //
        // $ch cURL handle returned by curl_init().
        //
        // $url (string) is URL of existing source(JSON, Google Sheet or PHP serialized array)
        //
        // Here you will insert your logic, because there are different JSON authentication
        // https://www.php.net/manual/en/function.curl-setopt.php
        //
        // Please note that this function is used for creating tables from JSON,Google sheets and PHP serialezed array
        // So you will need to filter it with some if statement based on $url parametar
        // 
        // Useful links
        //
        // https://stackoverflow.com/questions/30426047/correct-way-to-set-bearer-token-with-curl
        // https://stackoverflow.com/questions/60183353/php-how-to-make-a-get-request-with-http-basic-authentication
        // https://stackoverflow.com/questions/44331346/php-api-call-with-curl-and-authentication/44332142
        // https://stackoverflow.com/questions/2140419/how-do-i-make-a-request-using-http-basic-authentication-with-php-curl
        
        // Your code....
       
        return $data;
    }
    add_filter('wpdatatables_curl_get_data','filterCURLOptions', 10, 3);

    Best regards.

    Kind Regards, 

    Isidora Markovic

    wpDataTables: FAQFacebookTwitterFront-end and back-end demoDocs

    Amelia: FAQFacebookTwitter |  Amelia demo sites | Docs

    You can try our wpDataTables add-ons before purchase on these sandbox sites:

    Powerful FiltersGravity Forms Integration for wpDataTablesFormidable Forms Integration for wpDataTables

  • Puru991 replied

    Hi, just wanted to know if any progress has been made since this ticket was created, consuming data from authenticated sources is a must have. Please prioritize this over the next updates, thanks!

  •  1
    scoutin replied

    Great to see some progress here and that its on the roadmap for being built into the service.

    Our clients have been happy to use JSON via MS Azure blob stores and CORS policies to manage requesting URLs.

    Ultimately the data is public on their sites anyway, they just don't want other sites using the data.

    Maybe this approach will help someone.

  •  2,572
    Aleksandar replied

    Hello scoutin and Puru991.

    I just checked with our lead developer, and he said that this should be included in one of our next updates.

    Our team is busy preparing a big update, so we're not sure if they'll be able to squeeze this in, but if not - it is definitely planned for one of the following updates.

    Sorry for the delay.

    Kind Regards, 

    Aleksandar Vuković
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables

  • Marty Desilets replied

    Any updates here? Is this a planned feature? 

    It looks like customers have been asking for this feature for years.

    WPDataTables is a fantastic tool, but Authentication is a requirement to do anything serious.

  •  2,572
    Aleksandar replied

    Hello Marty

    We're still working on JSON authentication. It's almost ready, but I can't promise that it will be included in the next update. It should be, but if something goes wrong during the testing phase, it will be postponed.

    Kind Regards, 

    Aleksandar Vuković
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables

  • Rhianna Chambers-Dowe replied

    Any news on this feature making its way into production? Looks like this was close to being completed over a month ago, but I'm unable to see anything in https://features.wpdatatables.com/ to suggest that this is still being worked on. 

  •  2,572
    Aleksandar replied

    Hello Rhianna

    It's still in the testing phase. As soon as it's ready, it will be released. We hope it will be in the next update, but we can't make any promises. We are doing our best to test all use-cases so there aren't any bugs when this is released. Thank you for your patience.

    Kind Regards, 

    Aleksandar Vuković
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables

  • Frank replied

    Hi Team, I have been following this thread for quite some time. Any ETA yet on the progress of this integration? I purchased wp datatables some time ago and had the same misunderstanding about http requests with headers. I look forward to the feedback on this. Thank you so much as I can see it is a feature many are looking for. 

    Frank R. 

  •  2,572
    Aleksandar replied

    Hello Frank.

    JSON Authentication and Nested JSON has been released in wpDataTables 5.0, released in late September.

    Feel free to check out the documentation and let me know if you have any further questions.

    Kind Regards, 

    Aleksandar Vuković
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables