Getting 401 (access denied) when trying to use the REST API

I found the problem. WordPress.com has two different application passwords. The one in the 2FA set up page is the wrong one and that’s the one I was setting up. To set the right one you need to go to /wp-admin/profile.php. Once I did that, this code started working.

I wrote a blog post with lots of details about all the moving parts in case you want more details: https://pablofernandez.tech/2022/07/31/creating-blog-posts-in-wordpress-com-programatically/