wp-net / WordPressPCL

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

403 from RequestJWToken in spite of valid htaccess/server config #168

Closed erikdietrich closed 5 years ago

erikdietrich commented 5 years ago

Hi there. I was hoping someone might be able to help me.

I have the following code, appropriated from the example.

var client = new WordPressClient("{mysite}/wp-json/");
client.AuthMethod = AuthMethod.JWT;
await client.RequestJWToken("myvalidusername", "mvalidpassword");

var isValidToken = await client.IsValidJWToken();

When I run it, however, it kicks back a 403, with "WordPressPCL.Models.WPUnexpectedException: 'Server returned HTTP status Forbidden'" This happens in the call to RequestJWToken().

This isn't an htaccess issue nor is it anything on the server side. I'm able to use Postman to get a token, validate it, and use it to access drafts, delete posts, etc. So everything is working on the server and I'm able to navigate the REST API.

I'm not sure what I'm missing... any ideas would be very much appreciated.

gocasals commented 5 years ago

Try using client = new WordPressClient(baseUrl,"wp-json/wp/v2/"); and I have installed/configured JWT Authentication for WP-API plugin

erikdietrich commented 5 years ago

Thanks for the suggestion. Unfortunately, I gave it a try, and no luck. Same result.

gocasals commented 5 years ago

what error you receive? Sorry, i forget but i have modified the WordPressClient.cs file in the line 21, change the code from: private const string _jwtPath = "jwt-auth/v1/"; to: private const string _jwtPath = "wp-json/jwt-auth/v1/";

erikdietrich commented 5 years ago

The error is "Server returned HTTP status Forbidden."

Here's the stack trace:

at WordPressPCL.Utility.HttpHelper.<PostRequest>d__201.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at WordPressPCL.WordPressClient.d29.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at ElDorado.Menu.MainMenu.d8.MoveNext() in C:\Users\Erik\Documents\Visual Studio 2017\Projects\ElDorado\ElDorado\Menu\MainMenu.cs:line 117`

and the response:

{StatusCode: 403, ReasonPhrase: 'Forbidden', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:{ Transfer-Encoding: chunked Connection: keep-alive Vary: X-Forwarded-Proto Host-Header: 192fc2e7e50945beb8231a492d6a8024 Referrer-Policy: Access-Control-Allow-Origin: * Cache-Control: public, must-revalidate, no-cache, max-age=0 Date: Sun, 24 Mar 2019 16:12:11 GMT Server: nginx Content-Type: text/html; charset=utf-8 Expires: Wed, 11 Jan 1984 05:00:00 GMT}}

image

erikdietrich commented 5 years ago

I'm closing this for you. I appreciate the plugin and I like the API, but I wound up just implementing the functionality, myself, in my own codebase.

abjelosevic commented 4 years ago

I got this also, any solution?