Remote API example (for non WP sites/apps/whatever)

front page Forums Community Remote API example (for non WP sites/apps/whatever)

This topic contains 1 reply, has 2 voices, and was last updated by Avatar fcpro 2 weeks, 1 day ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #34803
    Avatar
    mihair

    Hi,

    In the last days I really struggled to figure out how to use the Remote API to get and sync points from an Android app to a shop which is using the myCred plugin.
    Even though now I see that the documentation is pretty straight forward, this was my first WP experience, but I didn’t imagine that it will take me so much time to make it work.

    So for all those that want to use the Remote API functions from non WP shops here are the steps that I took:
    (please note, that my app was intended only for a small prototype which I was trying out, I would advise to use a server with proper implementation for live versions)

    1. Make sure the plugin is active (as stated in the official documentation)
    2. Make sure you have the “Allow remote access” active (In the plugin Management settings)
    3. Make sure you are using the correct API key
    4. Make sure you are using the correct Incoming URI – this can be tested with a simple request in the browser – it returns 404 Not Found if it’s wrong otherwise you will get an empty 200 OK response
    5. Make sure you are building the token correctly md5( $host . $action . $secret_key ) = hostactionsecret_key (simply concatenate all the properties mentioned in the documentation – because I don’t know PHP i thought that “.” or ” . ” is needed to build the hash correctly)
    6. Make sure you provide all properties as described in the documentation

    Now the fun part – I used Postman for testing the requests and here is the raw result of how a GET request for the points should look like:

    RAW REQUEST:

    POST /api-dev/ HTTP/1.1
    Host: YOUR.HOST.GOES.HERE
    Content-Length: 128
    Accept: ‘application/x-www-form-urlencoded’
    Cache-Control: no-cache
    Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
    User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
    Content-Type: application/x-www-form-urlencoded
    Accept-Encoding: gzip, deflate
    Accept-Language: en-US,en;q=0.9,ro;q=0.8
    Connection: keep-alive

    action=GET&account=YOUR.EMAIL%40gmail.com&type=mycred_default&token=YOURTOKENHERE&host=YOUR.HOST.GOES.HERE

    ——————————————————————
    RAW RESPONSE:

    HTTP/1.1 200 OK
    Content-Type: text/html; charset=UTF-8
    Transfer-Encoding: chunked
    Keep-Alive: timeout=15
    Date: Thu, 20 Sep 2018 10:04:42 GMT
    Server: Apache
    Expires: Mon, 25 Dec 2017 00:00:00 GMT
    Cache-Control: no-store, no-cache, must-revalidate
    Pragma: no-cache
    Last-Modified: Thu, 20 Sep 2018 10:04:43 GMT
    Content-Encoding: gzip
    Proxy-Connection: keep-alive

    4501

    Other observations and things that confused me:
    – in my case the API returned 200 OK for all requests even though I tried with wrong token or wrong data in the body (even empty body)
    – what I mostly did wrong was that I didn’t formatted the body correctly according to the x-www-form-urlencoded format – and because the APi didn’t complain, well it was hard to figure it out

    I added the RAW data of the request because this is actually what I was looking for, I searched through the whole forum and I mostly found only PHP examples which didn’t help me so much in determining what I was doing wrong.
    The request in android I made using Retrofit2 but didn’t do anything special there, except making sure that the requests are coming out as described above.

    In the hope that my post will help someone, don’t be discouraged the Remote APi actually works and not only for WP to WP shop communication.

    Best of luck!

    #41256
    Avatar
    fcpro

    @mihair

    Hi. Thanks for sharing. Can you confirm that your solution still works fine (Even today) !?
    Regards

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

11