Home > Uncategorized > Using the #HTTP #API in #WordPress with C#

Using the #HTTP #API in #WordPress with C#

wordpress-http-api

This is NOT how you are supposed to use the WordPress API. this is a very specific use case, and unless you’ve exhausted everything else, you’re probably in the wrong place. But caveat’s aside here’s what I was trying to achieve.

I wanted to automatically search for wordpress sites with a particular search term, and auto like the posts. It’s a technique that works well on twitter, so I wanted to try the same on WordPress.

Now, this application is to run unattended, so I don’t want the OAuth prompt to appear, I just want it to log in as me. My credentials are removed from the code examples below.

First off, create a wordpress app, and get your client id and secret. The redirect url does not matter, you won’t use it. but a valid url is required. You will also need to get your blog id, you can get this from the developer console.

Here’s the login code in C#

private static string Login()
{
var http = new HTTPRequest();

var strUrl = “https://public-api.wordpress.com/oauth2/authorize?”;
strUrl += “client_id=*****”;
strUrl += “&redirect_uri=******”;
strUrl += “&response_type=code”;
strUrl += “&blog=*******”;

var strLoginPage = http.Request(strUrl);

var strUrl2 = “https://wordpress.com/wp-login.php?action=login-endpoint”;

var strPostdata = “username=*********”;
strPostdata += “&password=**********”;
strPostdata += “&remember_me=false”;
strPostdata += “&redirect_to=”;

strPostdata += HttpUtility.UrlEncode(“https://public-api.wordpress.com/oauth2/authorize?” +
“client_id=********” +
“&redirect_uri=http://*********” +
“&response_type=code” +
“&blog=*********”);

strPostdata += “&client_id=39911”;
strPostdata += “&client_secret=cOaYKdrkgXz8xY7aysv4fU6wL6sK5J8a6ojReEIAPwggsznj4Cb6mW0nffTxtYT8”;

var strResponse = http.Request(strUrl2, “POST”, strPostdata);

var strNextUrl = “https://public-api.wordpress.com/oauth2/authorize?”;
strNextUrl += “client_id=*********”;
strNextUrl += “&redirect_uri=http://*************”;
strNextUrl += “&response_type=code”;
strNextUrl += “&blog=*********”;

var strResponse2 = http.Request(strNextUrl);
var strNonceRegex = @”nonce..value..(?<Nonce>[\w]+)”;
var strNonce = Regex.Match(strResponse2, strNonceRegex).Groups[“Nonce”].Value;

var strNextUrl2 = “https://public-api.wordpress.com/oauth2/login/?&#8221;;
strNextUrl2 += “client_id=*********”;
strNextUrl2 += “&redirect_uri=http%3A%2F%2F**********”;
strNextUrl2 += “&response_type=code”;
strNextUrl2 += “&action=oauth2-login”;
strNextUrl2 += “&redirect_to=https%3A%2F%2Fpublic-api.wordpress.com%2Foauth2%2Fauthorize%2F%3Fclient_id%3D*********%26redirect_uri%3Dhttp%253A%252F%252F********%26response_type%3Dcode%26jetpack-code%26jetpack-user-id%3D0%26action%3Doauth2-login”;
strNextUrl2 += “&blog_id=***********”;
strNextUrl2 += “&_wpnonce=” + strNonce;

var strResponse3 = http.Request(strNextUrl2);

var qs = HttpUtility.ParseQueryString(http.PageUri.Query);

var strCode = qs[“Code”];

var strExchangeUrl = “https://public-api.wordpress.com/oauth2/token&#8221;;

strPostdata = “client_id=******”;
strPostdata += “&redirect_uri=http://*******”;
strPostdata += “&client_secret=KgNwl6lETO227GEarjZWFDtiCRihpIU8gMqBky4P5srG0Z4p9WnUFCmR3XidUin5”;
strPostdata += “&code=” + strCode;
strPostdata += “&grant_type=authorization_code”;

var strAuthTokenData = http.Request(strExchangeUrl,”POST”, strPostdata);

var json = JObject.Parse(strAuthTokenData);

var strAccessToken = json[“access_token”].ToString();

return strAccessToken;
}

Once you have the access_token then you can make requests to the WordPress API,  by setting  Authorization: Bearer + strAccessToken  in the HTTP headers.

There was no API for searching all WordPress sites, but I used their search page as follows:

var strSearchUrl = “https://en.search.wordpress.com/?src=organic&q=****&s=date&t=post&#8221;;
var strSearchResults = http.Request(strSearchUrl);
var strBlogRegex = @”span…a.href=.https://(?<Blog&gt;\w+.wordpress.com)”;

where q is the search term,

Now, for example, to get all posts from a blog, call;

var strPostsEndpoint = “https://public-api.wordpress.com/rest/v1/sites/{0}/posts/”;
strPostsEndpoint = string.Format(strPostsEndpoint,strBlog);
var strPostsJson = http.Request(strPostsEndpoint);
var jPosts = JObject.Parse(strPostsJson);

and to like a post, I called

var strLikeEndpoint = “https://public-api.wordpress.com/rest/v1/sites/{0}/posts/{1}/likes/new”;
strLikeEndpoint = string.Format(strLikeEndpoint, strBlog, strPostId);
var strResponse = http.Request(strLikeEndpoint, “POST”, “”);

 

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment