Using Wordpress API's with PowerShell. Associated blogpost https://blog.darrenjrobinson.com/using-wordpress-apis-with-powershell/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# V2 APIs | |
# Basic AuthN | |
$userID = 'yourWordpressAccountAlias' | |
$userPassword = 'ABCD wTUZ pIST 9jEo 99LV 1234' | |
$Bytes = [System.Text.Encoding]::utf8.GetBytes("$($userID):$($userPassword)") | |
$encodedAuth = [Convert]::ToBase64String($Bytes) | |
$header = @{Authorization = "Basic $($encodedAuth)" } | |
Invoke-RestMethod -method get -uri "https://yourwordpressURL/wp-json/wp/v2/posts" -headers $header | |
# =================================================================================================== | |
# V1 APIs | |
<# | |
Update the following snippet with the clientID from your newly registered WordPress application and the URL from your WordPress site. | |
It must be the same as you registered with the application registration. | |
Running the snippet below will generate the URL and put it in the clipboard. | |
Paste it into your browser. | |
You will then be prompted to authenticate with your WordPress account and authorize your application. | |
#> | |
$clientID = 'myClientID' | |
$replyURL = 'https://myWordPressURL.com' | |
$url = "https://public-api.wordpress.com/oauth2/authorize?client_id=$($clientID)&redirect_uri=$($replyURL)&scope=global%20stats&response_type=code" | |
$url | clip | |
<# | |
After authorization use the AuthCode to get an Access Token | |
#> | |
Function Get-WordPressAccessToken { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] | |
$clientID, | |
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] | |
$blogURL, | |
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] | |
$clientSecret, | |
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] | |
$authCode | |
) | |
try { | |
$result = Invoke-RestMethod https://public-api.wordpress.com/oauth2/token ` | |
-Method Post -ContentType "application/x-www-form-urlencoded" ` | |
-Body @{ | |
client_id = $clientId | |
client_secret = $clientSecret | |
redirect_uri = $blogURL | |
grant_type = "authorization_code" | |
code = $authCode | |
} -ErrorAction STOP | |
return $result | |
} | |
catch { | |
$_ | |
} | |
} | |
$clientID = 'yourAppClientID' | |
$clientSecret = 'yourAppClientSecret' | |
$authCode = 'code_from_browser_authorization' | |
$replyURL = 'https://myWordPressURL.com' | |
$accessToken = Get-WordPressAccessToken -ClientID $clientID -blogURL $replyURL -clientSecret $clientSecret -authCode $authCode | |
<# | |
Call the v1.1 API's. e.g Get WordPress Status | |
#> | |
Function Get-WordPressStats { | |
[Cmdletbinding()] | |
param( | |
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 0)] | |
[string] $siteID, | |
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 0)] | |
[string] $accessToken | |
) | |
$stats = Invoke-RestMethod https://public-api.wordpress.com/rest/v1.1/sites/$siteID/stats -Method Get ` | |
-Headers @{"Authorization" = "Bearer $accessToken" } | Select-object -ExpandProperty Stats | |
return $stats | |
} | |
$wpBlogID = '123514446' | |
Get-WordPressStats -siteID $wpBlogID -accessToken $accessToken.access_token |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment