The blogger_posts.sh
Bash script adds, updates, or deletes Blogger posts or
pages through the API 3.0.
blogger_posts.sh
has been tested for Blogger on Debian Testing on WSL 2 and
uses the following packages:
Install each package as needed. For example:
sudo apt install curl
sudo apt install jq
sudo apt install gpg
The google_oauth_token.sh
authorization script
will create and encrypt the
~/.config/blogger-posts/google_oauth_token.cfg.gpg
configuration file if it
does not exist. It assumes that the default key pair of GnuPG is your OpenPGP
key pair.
- Create client credentials by selecting the “Desktop app” application type in the Google API Console.
- Replace the values of the following variables with your own in the
configuration file:
SCOPE
CLIENT_ID
CLIENT_SECRET
- Execute
google_oauth_token.sh -C
, and open the URL in your browser. Then, replace the value of theauthorization_code
variable in the configuration file with the value of thecode
of a returned URL in the address bar. - Execute
google_oauth_token.sh -T
to store tokens in the configuration file.
After completing steps 1-4 above, google_oauth_token.sh -a
refreshes and
returns the access token if it is expired when called from another script using
the Google API.
-C
: return an authorization URL for an authorization code-T
: store an access token and a refresh token-r
: refresh the access token-i
: display the status of the access token in JSON if it is valid-a
: refresh the access token if it is expired and return it
The test_blogger_posts.sh
testing script will create the
~/.config/blogger-posts/test_blogger_posts.cfg
configuration file if it does
not exist. Replace the value of the BLOG_ID
variable in it with your own.
Then, execute:
test_blogger_posts.sh
blogger_posts.sh
creates the $HOME/Downloads/test_blogger_posts
directory
if it does not exist and saves response bodies.
The blogger_posts.sh
script is a collection of the following functions. To
list resources that are posts
or pages
, pass an optional parameter as an
argument if necessary:
bp_list_resources posts status=live
To retrieve a resource:
bp_get_resource posts RESOURCE_ID
To add a resource, pass multiple property-value pairs as arguments:
bp_add_resource posts title '"Resource Title"' content '"<p>A paragraph.</p>"'
To delete a resource:
bp_delete_resource posts RESOURCE_ID
To update a resource, pass multiple property-value pairs as arguments:
bp_partially_update_resource posts RESOURCE_ID \
content '"<p>An updated paragraph.</p>"'
To transition the resource status, pass the status publish
or revert
as an
argument:
bp_transition_post_status posts RESOURCE_ID publish
The update_blogger.sh
script serves as a practical example of how to use
blogger_posts.sh
.
-n
: do not perform HTTP requests and a commit-s
: work silently
- Bash Scripting to Obtain Access Token from Google Authorization Server: a blog post for Google OAuth 2.0 authorization
- Bash Scripting to Update Blogger Posts for Local Changes through
API:
a blog post with a practical example using
blogger_posts.sh