I’ve had a few requests to explain how to use the Nuget WordPress REST API beyond authentication.
In order to do this, I added a Demo App under source control.
The Nuget package source code is available at http://wordpressrestapi.codeplex.com/ and if you download the code, you’ll see there are 3 projects:
- WordPressRestApi: essentially the Nuget package
- ApiClientTest: Unit tests on the previous project
- WordPressDemo: The new Web App project
The demo project shows you a way to use the package.
Last year I posted an article on WordPress authentication, explaining how it works. I would first read that article.
When you start the demo, it will take you into a sort of Authentication Wizard when you hit the web app root (e.g. http://localhost:58373/).
You need to go to https://developer.wordpress.com/apps/ and create yourself a WordPress App. Give it a name & a description. This is purely to run the demo on your laptop / desktop. The redirect URL should be of the shape http://localhost:58373/SignIn but where you replaced 58373 by whatever port number the web app run on your laptop.
Once you created the app, at the bottom of the screen you should have access to the two elements of information you’ll need to run the demo.
On the demo web site you should be prompted to enter the client ID.
Usually we would have the Client ID and other app’s information in the web.config. But to simplify the demo setup, I have the user keypunch them.
Once you’ve enter the client ID, click submit. The app should give you a link to sign in WordPress. Click it.
Your browser will have you navigate to https://public-api.wordpress.com/oauth2/authorize. From there, WordPress will request your consent. Click Approve.
FYI, the demo App only does read operations: it won’t modify or delete anything.
From there you should be brought back to the redirect URL you configured.
You need to again enter the client ID. You also need to enter the client secret.
You can the click on the Fetch Token button. This will allow the web app to fetch a token for the WordPress App.
From there you should land on http://localhost:58373/DemoRead.
You can follow the authentication code from HomeController & SignInController classes (and models).
The class DemoReadController does the read operations. Here we demo a query on posts & one on tags.
Everything flows from the WordPressClient.
We got rid of the tentative of building LINQ queries around WordPress and instead went for a thin interface on top of its REST API.
The surface of the REST API exposed is quite limited at this point: posts and tags.
A particularity of the interface is the use of IAsyncEnumerable<T>, which is a custom interface allowing us to add a filter (where clause) and / or projection (select). Those aren’t sent to the API, a la LINQ SQL, but they are at least processed as the objects are hydrated from the requests. This interface also respects the async semantic hence allowing us to build more scalable application on top of it.
This demo app is by no mean a base for your WordPress applications. Rather, it illustrates how to use the Nuget Package.
I hope this gives you a better idea on how to use it.