wp-net / WordPressPCL

This is a portable library for consuimg the WordPress REST-API in (almost) any C# application
MIT License
337 stars 129 forks source link

code stops - Wordpress CPL using JWT Token (C#) #227

Closed Vitaliy-Steffensen closed 3 years ago

Vitaliy-Steffensen commented 3 years ago

I'm trying to connect to the REST API using the wordpressCPL plugin using the jwt auth. Tho when ever I try to request my JWT token, it just seems like the code stops. It doesn't get any errors, it just doesn't get to run any code after the request line. (ofc i don't have "###" as my user & pass)

`private static async Task GetClient() { // JWT authentication var client = new WordPressClient("http://zoly.dk/wp-json/"); client.AuthMethod = AuthMethod.JWT; await client.RequestJWToken("###", "###"); //The code never gets to here!!! return client; }

    public static async Task CreatePost()
    {
        try
        {
            WordPressClient client = await GetClient();
            if (await client.IsValidJWToken())
            {
                var post = new Post
                {
                    Title = new Title("New post test"),
                    Content = new Content("Content for new post.")
                };
                await client.Posts.Create(post);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Error:" + e.Message);
        }
    }`
ThomasPe commented 3 years ago

have you checked if you can get a token through the rest api, e.g. with Postman? Seems your server isn't returning a token at that point.

Trades-King commented 3 years ago

Hi, same here. I used for api new WordPressClient("https://traders-view.services/api/");

also by chaning to https://traders-view.services/?rest_route=/ but i dont get a var isValidToken = await client.IsValidJWToken();

Back so i cant go fw. Before (3 months ago) it was working.

Br Sniper

RetiredQQ commented 3 years ago

Hi, @ThomasPe

When debugging with fiddler if you use http://yourdomain.com instead of https://yourdomain.com httpclient method seems changed to GET but it should be POST.

GET https://yourdomain.com/wp-json/jwt-auth/v1/token HTTP/1.1 Content-Type: application/x-www-form-urlencoded Host: yourdomain.com

{"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}

Reference: https://stackoverflow.com/questions/37373138/httpclient-post-generates-get-instead

ThomasPe commented 3 years ago

Well if I read that post correctly the redirect is happening on the server side when switching from www to non-www (or vice versa). Nothing the httpclient can do about.

ThomasPe commented 3 years ago

Added a note in the Troubleshooting Wiki, a WPException should be thrown in this case. https://github.com/wp-net/WordPressPCL/wiki/Troubleshooting