• Resolved stigzler

    (@stigzler)


    hi – firstly neat plugin. Love it.

    I’m developing a .net library for this. Have implemented all the GET operations without a hitch. However, I’m struggling to interface with the API for PUT operations (and possibly, by extension, POST). I’m getting a http 404 error from the server, despite the license key being right. I don’t know if you work with .net, but some code:

    Public Function HttpPut(netquery As String, json As String) As WebClientResponse
    
            Dim response As New WebClientResponse
    
            Using client As New CustomWebClient(WebClientTimeout)
    
                With client
                    .Encoding = Encoding.UTF8
                    .Credentials = Credentials
                    .Headers(HttpRequestHeader.ContentType) = "application/json"
                End With
         
    	    With response
    	       .ReturnedString = client.UploadString(netquery, "PUT", json)
    	       .Success = True
                End With         
    
    	End Using
    		
    	Return response
    		
        End Function

    CustomWebClient is just an extension allowing custom time outs. Credentials are exactly the same as for the working GETs. The content of netquery is right (same as the successful GETS). The client.UploadString params:

    netquery (with changes to preserve security): https://mysite.com/wp-json/lmfwc/v2/licenses/OGZL-3DDTF-8HTFS-SQET5-0MQ2E-87TFS

    json:
    {"id":4,"orderID":224,"productID":205,"LicenseKey":"OGZL-3DDTF-8HTFS-SQET5-0MQ2E-87TFS","ExpiresAt":null,"ValidFor":null,"Source":"1","Status":3,"TimesActivated":1,"TimesActivatedMax":2,"RemainingActivations":null,"CreatedAt":"2021-04-28T10:37:39","CreatedBy":1,"UpdatedAt":"2021-06-15T18:06:19","UpdatedBy":1}

    Difficulty being, I only have the HTTP error to go on so it’s hard to figure what’s going on with the API. Could you assist?

    • This topic was modified 3 years, 5 months ago by stigzler.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter stigzler

    (@stigzler)

    P.S. I also tested it with this tool to take it away from my code. It returned this:

    {
        "code": "lmfwc_rest_data_error",
        "message": "The license key could not be updated.",
        "data": {
            "status": 404
        }
    }

    Not sure why the update is not allowed? Are there any fields that are off limits for updates or throw the update?

    EDIT: It does appear to do with what json you place in content. This produces the above error:

    {"id":null,"orderID":null,"productID":null,"LicenseKey":"","ExpiresAt":null,"ValidFor":null,"Source":"","Status":2,"TimesActivated":null,"TimesActivatedMax":null,"RemainingActivations":null,"CreatedAt":null,"CreatedBy":null,"UpdatedAt":null,"UpdatedBy":null}

    But this works:
    {"Status":1}

    • This reply was modified 3 years, 5 months ago by stigzler.
    • This reply was modified 3 years, 5 months ago by stigzler.

    Hello @stigzler

    This is the method that handles the “Update license” request:

    https://github.com/drazenbebic/license-manager-for-woocommerce/blob/master/includes/api/v2/Licenses.php#L441

    As you can see the error “The license key could not be updated” happens near the end of the function, when the plugin tries to actually update the license key in the database.

    I Suspect that the JSON you sent is invalid, because of the “casing” of your body. Check out the docs here:

    https://www.licensemanager.at/docs/rest-api/developer-documentation/update-license

    Try to use snake case in your JSON body, i.e. order_id instead of `orderID.

    Let me know if that works.

    PS: If you’re willing to submit the library, I would be happy to feature it on my website ??

    Thread Starter stigzler

    (@stigzler)

    Hi Drazen – thanks for this. Yeah – missed that detail! All good now, thanks

    Happy to share my library with you. I’m afraid I’m not a professional developer or anything – I’m just a hobby coder but take a look at it and do with it as you see fit. I will forward it when it’s finished.

    There are a couple of things that would be helpful in its dev which I can’t find in the docs. Mainly, what are the constraints around Update and Create Licence? I.e. what are the mandatory fields for each and what causes exceptions? (I think I’ve established that barest minimum for Create is Key and Status)

    I could trawl through your code, but my WP coding knowledge is poor (hooks/filters etc) and you’ll know where to look of course!

    Thanks again for your plugin – it’s awesome!

    @stigzler

    That’s good info, I’ll add a task to update the REST API docs for constraints. It would also be very helpful if you could find an example on how these constraints are documented on any other project, because I’m not sure how to format it.

    Here’s a link to the GitHub issue for this: https://github.com/drazenbebic/licensemanager.at/issues/13

    Thread Starter stigzler

    (@stigzler)

    Great stuff + gosh – I’ve no idea about how to set it out. I just wing stuff and make it up as I go along ??

    @stigzler

    I will try it your way then ??

    No worries though, I’m sure that I’ll find plenty of examples online.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Struggling with License Update in API with .net’ is closed to new replies.