HISE Logo Forum
    • Categories
    • Register
    • Login

    Get data from Woocommerce via server api

    Scheduled Pinned Locked Moved Scripting
    authenticationserverapiwoocommerce
    109 Posts 11 Posters 9.3k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • d.healeyD
      d.healey
      last edited by

      So I'm finally at the stage where I'm tinkering with the server API. I want to do something simple, I want my user to provide their login details to my plugin, I send them up to my server, and get back a list of the things they've bought from me.

      I'm using the JWT Authentication plugin to get the initial authorization token, this works fine. But when I query a WooCommerce endpoint I get a 401. Everything I find online wants me to pass a public and secret REST API key to WooCommerce with my requests, but I'm building an open source plugin. How do I keep my secret key secret when anyone can view the source?

      Libre Wave - Freedom respecting instruments and effects
      My Patreon - HISE tutorials
      YouTube Channel - Public HISE tutorials

      orangeO 1 Reply Last reply Reply Quote 0
      • orangeO
        orange @d.healey
        last edited by orange

        @d-healey For example, to retrieve product info you can use below format. (with a button named "Get_Product_Info")

        Server.setBaseURL("https://yourwebsite.com");
        
        inline function onGet_Product_InfoControl(component, value)
        {
          if(value)
              {
        
                 Server.callWithGET("/wp-json/wc/v3/products/YourProductID?consumer_key=123456789&consumer_secret=123456789", "", function(status, response)         
                 {          
                    local v = trace(response);           
                    Console.print("heres back from the server:" + v);        
                 });        
              }
        };
        
        
        Content.getComponent("Get_Product_Info").setControlCallback(onGet_Product_InfoControl);
        
        

        develop Branch / XCode 13.1
        macOS Monterey / M1 Max

        1 Reply Last reply Reply Quote 1
        • orangeO
          orange
          last edited by orange

          For consumer key and consumer secret, go to Woocommerce > Settings > Advanced > REST API > Add Key

          These keys will be your credentials for getting data.

          Link Preview Image
          WooCommerce REST API Documentation - WP REST API v3

          favicon

          (woocommerce.github.io)

          develop Branch / XCode 13.1
          macOS Monterey / M1 Max

          d.healeyD 1 Reply Last reply Reply Quote 0
          • d.healeyD
            d.healey @orange
            last edited by d.healey

            @orange Re-read my post :p Can't put secret key in an open source project and expect it to stay secret.

            Libre Wave - Freedom respecting instruments and effects
            My Patreon - HISE tutorials
            YouTube Channel - Public HISE tutorials

            orangeO 1 Reply Last reply Reply Quote 0
            • orangeO
              orange @d.healey
              last edited by orange

              @d-healey I understand, but the title says Woocommerce, not Wordpress 😛

              Let me explain, for username and password authentication JWTAuth method is a one way to use. It's ok. But for getting which customer bought which product:

              • First choice, like you said in title, you can use Woocommerce REST API. And Woocommerce REST API doesn't use JWT Authentication. It uses it's own consumer key and consumer secret method (called OAuth).

              • Second choice, if you want to use the token anyway, then you need to go with Wordpress REST API ;)

              develop Branch / XCode 13.1
              macOS Monterey / M1 Max

              d.healeyD 1 Reply Last reply Reply Quote 1
              • d.healeyD
                d.healey @orange
                last edited by

                @orange

                In the WooCommerce docs it says

                WooCommerce includes two ways to authenticate with the WP REST API. It is also possible to authenticate using any WP REST API authentication plugin or method.

                So I thought that meant I could use the JWT to access WooCommerce endpoints too.

                Second choice, if you want to use the token anyway, then you need to go with Wordpress REST API

                Yes this is what I want, how can I get the WooCommerce orders using the token?

                Libre Wave - Freedom respecting instruments and effects
                My Patreon - HISE tutorials
                YouTube Channel - Public HISE tutorials

                orangeO 1 Reply Last reply Reply Quote 0
                • orangeO
                  orange @d.healey
                  last edited by orange

                  @d-healey said in Get data from Woocommerce via server api:

                  Yes this is what I want, how can I get the WooCommerce orders using the token?

                  I haven't tried that, but you need to use the token in the header;
                  https://docs.hise.audio/scripting/scripting-api/server/index.html#sethttpheader

                  Actually I think you don't need this. Because if the username and password is correct, the system will provide the token. If not, it will give error. After checking the JWT Authentication, then you don't need to use the token, because there is an easier way with woocommerce REST API. But it is IMO.

                  develop Branch / XCode 13.1
                  macOS Monterey / M1 Max

                  d.healeyD 1 Reply Last reply Reply Quote 0
                  • d.healeyD
                    d.healey @orange
                    last edited by

                    @orange

                    I haven't tried that, but you need to use the token in the header;

                    I've tried this but it doesn't seem to be working, however it doesn't seem to be working with regular wordpress endpoints either so I think I need to check that I@m using JWT correctly first.

                    there is an easier way with woocommerce REST API.

                    But I can't use this because the secret key will not be secret.

                    Libre Wave - Freedom respecting instruments and effects
                    My Patreon - HISE tutorials
                    YouTube Channel - Public HISE tutorials

                    orangeO 1 Reply Last reply Reply Quote 1
                    • orangeO
                      orange @d.healey
                      last edited by

                      @d-healey said in Get data from Woocommerce via server api:

                      But I can't use this because the secret key will not be secret.

                      Do you think it can be retrieved by the hackers? :)

                      develop Branch / XCode 13.1
                      macOS Monterey / M1 Max

                      d.healeyD 1 Reply Last reply Reply Quote 0
                      • d.healeyD
                        d.healey @orange
                        last edited by

                        @orange Yes, my project is open source.

                        Libre Wave - Freedom respecting instruments and effects
                        My Patreon - HISE tutorials
                        YouTube Channel - Public HISE tutorials

                        orangeO 1 Reply Last reply Reply Quote 1
                        • orangeO
                          orange @d.healey
                          last edited by

                          @d-healey Now I am trying to use the token in header but I couldn't get it work too. There should be something missing...

                          develop Branch / XCode 13.1
                          macOS Monterey / M1 Max

                          d.healeyD 1 Reply Last reply Reply Quote 0
                          • d.healeyD
                            d.healey @orange
                            last edited by

                            @orange Oo what format are you using for the header? I've tried these two

                                Server.setHttpHeader("Authorization = Bearer " + token);
                                Server.setHttpHeader("Authorization: Bearer " + token);
                            

                            Libre Wave - Freedom respecting instruments and effects
                            My Patreon - HISE tutorials
                            YouTube Channel - Public HISE tutorials

                            orangeO 1 Reply Last reply Reply Quote 0
                            • orangeO
                              orange @d.healey
                              last edited by

                              @d-healey I used that one

                                  Server.setHttpHeader("Authorization: Bearer " + token);
                              
                              

                              I guess we are using same plugin :)

                              develop Branch / XCode 13.1
                              macOS Monterey / M1 Max

                              d.healeyD 1 Reply Last reply Reply Quote 1
                              • d.healeyD
                                d.healey @orange
                                last edited by

                                @orange I've installed a REST API log plugin

                                {
                                    "data": {
                                        "code": "jwt_auth_no_auth_header",
                                        "message": "Authorization header not found.",
                                        "data": {
                                            "status": 403
                                        }
                                    },
                                    "headers": {
                                        "Allow": "POST"
                                    },
                                    "status": 403
                                }
                                

                                So the header isn't being set, I will keep digging.

                                Libre Wave - Freedom respecting instruments and effects
                                My Patreon - HISE tutorials
                                YouTube Channel - Public HISE tutorials

                                1 Reply Last reply Reply Quote 0
                                • d.healeyD
                                  d.healey
                                  last edited by d.healey

                                  @Christoph-Hart Any ideas? What format should the header be?

                                  Libre Wave - Freedom respecting instruments and effects
                                  My Patreon - HISE tutorials
                                  YouTube Channel - Public HISE tutorials

                                  1 Reply Last reply Reply Quote 0
                                  • d.healeyD
                                    d.healey
                                    last edited by d.healey

                                    The plot thickens...

                                    Setting the content-type header, like this Content-Type: text/html works. Setting authorisation header like this Authorization: Bearer 123456 doesn't work.

                                    I'm thinking it's something to do with my server configuration....

                                    Libre Wave - Freedom respecting instruments and effects
                                    My Patreon - HISE tutorials
                                    YouTube Channel - Public HISE tutorials

                                    1 Reply Last reply Reply Quote 0
                                    • d.healeyD
                                      d.healey
                                      last edited by d.healey

                                      Finally got the damn thing working. I'm using a xampp test server.

                                      These are the things I did:

                                      Followed config settings here - https://wordpress.org/plugins/jwt-authentication-for-wp-rest-api/#description
                                      Added SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1 to apache config file: instructions here - https://stackoverflow.com/a/54729344/1901367
                                      Also did what this guy said, don't know if it's necessary though - https://www.web-design-talk.co.uk/126/getting-htaccess-mod-rewrite-working-locally-with-xampp/

                                      Libre Wave - Freedom respecting instruments and effects
                                      My Patreon - HISE tutorials
                                      YouTube Channel - Public HISE tutorials

                                      orangeO 1 Reply Last reply Reply Quote 3
                                      • orangeO
                                        orange @d.healey
                                        last edited by orange

                                        @d-healey Oh yes, .htaccess file and wp-config file must be modified.

                                        Is customer purchase call working?
                                        I am trying to get user info but response is "Sorry, you are not allowed to list users." I think same header issue is here too.

                                        I am using live server by the way.

                                        var encryptionData;
                                        Server.setBaseURL("https://website.com");
                                        
                                        inline function onGetInfo_ButtonControl(component, value)
                                        {
                                           if(value)
                                              {
                                                 local LockerPW = "987654321";
                                                    
                                                 local FileDirectory = FileSystem.getFolder(FileSystem.UserPresets).getParentDirectory();
                                                 encryptionData = FileDirectory.getChildFile("credentials.dat").loadEncryptedObject(LockerPW);
                                                          
                                                 Server.setHttpHeader("Authorization: Bearer " + encryptionData.token);
                                                    
                                                 Server.callWithGET("wp-json/wp/v2/users", encryptionData.user_email, function(status, response)
                                                 {
                                                    local serv_resp = trace(response);
                                                   
                                                    Console.print("server response: " + serv_resp);
                                                 });
                                                
                                              }      
                                        };
                                        Content.getComponent("GetInfo_Button").setControlCallback(onGetInfo_ButtonControl);
                                        

                                        develop Branch / XCode 13.1
                                        macOS Monterey / M1 Max

                                        1 Reply Last reply Reply Quote 1
                                        • d.healeyD
                                          d.healey
                                          last edited by

                                          Yes I modified htaccess but it didn't work on my testing server - xampp. I had to do the extra steps I put in my last post.

                                          Libre Wave - Freedom respecting instruments and effects
                                          My Patreon - HISE tutorials
                                          YouTube Channel - Public HISE tutorials

                                          orangeO 1 Reply Last reply Reply Quote 1
                                          • Christoph HartC
                                            Christoph Hart
                                            last edited by

                                            Not sure if I understand this particular authentication system, but isn't the token supposed to be generated as a session cookie?

                                            1. User logs in with credentials (email & password)
                                            2. Server returns a token when the login succeeds (might return a cached token if the requests are within a certain time).
                                            3. User uses this token for each subsequent URL request as proof of authentication (most likely in the header).
                                            orangeO d.healeyD 2 Replies Last reply Reply Quote 1
                                            • First post
                                              Last post

                                            42

                                            Online

                                            1.7k

                                            Users

                                            11.7k

                                            Topics

                                            101.8k

                                            Posts