baranovskypd / goodpress

WIP!! :warning: R Package to Write Posts from R Markdown to Wordpress :warning:
https://maelle.github.io/goodpress/
Other
34 stars 5 forks source link

Authorization fail #15

Closed henrywangnl closed 4 years ago

henrywangnl commented 4 years ago

fail to run wp_post(tmp_post_folder, wordpress_url)

Error in wp_call_api(VERB = "POST", api_url = paste0(wordpress_url, "/wp-json/wp/v2/categories?name=", : Unauthorized (HTTP 401).

maelle commented 4 years ago

Thanks. Could you please tell me which steps you followed to help me identify the issue?

maelle commented 4 years ago

Does the user have author or editor rights?

henrywangnl commented 4 years ago

Thanks. Could you please tell me which steps you followed to help me identify the issue?

Hi Maelle,

I completed the setup and now I'm stuck at the workflow(https://maelle.github.io/goodpress/articles/goodpress.html#workflow-1).

Besides, the user has the editor role.

maelle commented 4 years ago

Thanks!

Did you restart R after editing .Renviron?

Can you copy-paste the YAML part of the index.m?

maelle commented 4 years ago

For info I've added more detailed info to the authentication section https://maelle.github.io/goodpress/articles/setup.html#authentication-1 (in particular I have shown that there can be space in the application password, and that it's not the main WordPress account password) and a better error message when environment variables are not found.

Could you please re-install the package so we can see whether the issue is with R not finding the WP_PWD and WP_USER? I'm not assuming that's the issue, that's just a possible one, and if we can eliminate that it'll help finding the actual problem. Thanks for your patience.

henrywangnl commented 4 years ago

Hi Maelle,

I did re-install the package, yet the problem still exists. Below is the setup and please let me if I didn't do it correctly.

1. My YAML part of the index.Rmd is:

_title: "Test RMarkdown" author: "Henry" date: "6/24/2020" output: hugodown::mddocument

2. The .Renviron file:

_WP_USER="username" WPPWD="mypassword"

3. How I call wp_post:

library(goodpress)

_post_folder <- "post_folder_path" wordpressurl <- "http://mywordpress.com"

_wp_post(post_folder, wordpressurl)

Error in wp_call_api(VERB = "POST", api_url = api_url, body = post) : Bad Request (HTTP 400).

maelle commented 4 years ago

Thanks!

wordpress_url <- # your wordpress URL

goodpress:::wp_call_api(
  "GET", 
  api_url = paste0(wordpress_url, "/wp-json/wp/v2/categories")
  )

# this could create a new category called "newcat" so if this works delete it afterwards :-)
goodpress:::wp_call_api(
  "POST", 
  api_url = paste0(wordpress_url, "/wp-json/wp/v2/categories?name=newcat")
)
maelle commented 4 years ago

Also, if your URL has https, try using https rather than http. :thinking:

maelle commented 4 years ago

last question before I wait, in your post folder, can you confirm you only have

maelle commented 4 years ago

Did I say last, oops.

henrywangnl commented 4 years ago

Also, if your URL has https, try using https rather than http. 🤔

yes, I'm using https for my real blog url.

henrywangnl commented 4 years ago

last question before I wait, in your post folder, can you confirm you only have

  • index.Rmd
  • index.md
  • a figs folder i.e. no .wordpress.yml yet?

no .wordpress.yml yet. Is it supposed to have?

henrywangnl commented 4 years ago
  • is there an empty line at the end of .Renviron? (I suppose so otherwise goodpress would error I think)

yes, I have empty lines. Should I clear those?

henrywangnl commented 4 years ago
  • It's also hugodown latest version?

my hugodown version is hugodown_0.0.0.9000, which is the latest.

henrywangnl commented 4 years ago
  • Could you try the code below with your WordPress URL? I'm trying to see if only POST-ing doesn't work whilst GET-ting does.

it seems that only POST doesn't work:

Error in goodpress:::wp_call_api("POST", api_url = paste0(wordpress_url, : Unauthorized (HTTP 401).

maelle commented 4 years ago

no .wordpress.yml yet. Is it supposed to have?

No, I was wondering but that's good.

yes, I have empty lines. Should I clear those?

No, an empty line at the end of .Renviron is needed.

my hugodown version is hugodown_0.0.0.9000, which is the latest.

Awesome.

it seems that only POST doesn't work:

Can you try re-generating the application password for the user? (then resaving it in .Renviron and re-starting).

Can you confirm you had edited your website's .htaccess?

maelle commented 4 years ago

For info this is the code that creates the token (that is sent to the API via a header)

https://github.com/maelle/goodpress/blob/55d380b303e1716ba267a42fef2b420534fbc488/R/utils-http.R#L4-L9

What's your operating system by the way?

Thanks again for your patience, I really hope we can make this work.

henrywangnl commented 4 years ago

Can you confirm you had edited your website's .htaccess?

yes I did

maelle commented 4 years ago

Can you paste the output of sessioninfo::session_info()? I'm starting to wonder whether there could be a locale problem (which would then make encoding not work as I expect, well differently than on my machine).

henrywangnl commented 4 years ago
  • Could you try the code below with your WordPress URL? I'm trying to see if only POST-ing doesn't work whilst GET-ting does.

The POST works now but still I can't call wp_post.

I made a stupid mistake that I used my WP user and password not the Application Passwords, so I changed it and now I have the new category called newcat.

However, I still got the error when I called wp_post:

Error in wp_call_api(VERB = "POST", api_url = api_url, body = post) : Bad Request (HTTP 400).

henrywangnl commented 4 years ago

Can you paste the output of sessioninfo::session_info()? I'm starting to wonder whether there could be a locale problem (which would then make encoding not work as I expect, well differently than on my machine).

here you go:

R version 4.0.0 (2020-04-24) Platform: x86_64-apple-darwin17.0 (64-bit) Running under: macOS Catalina 10.15.5

Matrix products: default BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] goodpress_0.0.0.9000

loaded via a namespace (and not attached): [1] packrat_0.5.0 digest_0.6.25 withr_2.2.0 R6_2.4.1
[5] jsonlite_1.6.1 magrittr_1.5 evaluate_0.14 httr_1.4.1
[9] rlang_0.4.6 curl_4.3 rmarkdown_2.1 tools_4.0.0
[13] glue_1.4.1 purrr_0.3.4 xfun_0.14 yaml_2.2.1
[17] compiler_4.0.0 htmltools_0.5.0 knitr_1.28

maelle commented 4 years ago

Yay! We're making progress! I actually made a similar error when starting work on the package, by writing my password as username and vice versa :wink:

I think the problem now will be something like the date format. Can you try using one of my two example posts e.g. https://github.com/maelle/goodpress/blob/main/inst/post-example2/index.Rmd?

henrywangnl commented 4 years ago

Good idea!

The second post(post-example2) works while the first one doesn't.

post_folder <- system.file(file.path("post-example"), package = "goodpress") tmp_post_folder <- file.path(tempdir(), "post") dir.create(tmp_post_folder) file.copy(dir(post_folder, full.names = TRUE), tmp_post_folder) wordpress_url <- "https://henrywang.nl/" # my website wp_post(tmp_post_folder, wordpress_url) file.remove(dir(tmp_post_folder, full.names = TRUE))

First time I got this: _Error in wp_handle_author(meta$author, wordpressurl) : admin7891 is not an existing user name.

The second time I rerun the code I got this: _Error in wp_call_api(VERB = "POST", api_url = paste0(wordpressurl, "/wp-json/wp/v2/tags?name=", : Bad Request (HTTP 400).

maelle commented 4 years ago

ah yes it's because that example has an author field with an username that only exists in my test website.

do I understand correctly that you were able to post one of the examples? it might mean that with your real actual post, the date format was wrong, so you could try tweaking it to use the same format as in my example?

maelle commented 4 years ago

note that you can use the status YAML field e.g.

status: draft
henrywangnl commented 4 years ago

Yes, Maelle! it works now! Thanks so much for your help! I can continue with your documentation now.

maelle commented 4 years ago

yay!! please post here or tag me on Twitter when/if you publish a real post with goodpress, and feel free to contribute to the docs (with suggestions of what can be made clearer / direct PRs) and code!

maelle commented 4 years ago

(I've already been able to improve the docs thanks to your issue, so thanks for that!)

henrywangnl commented 4 years ago

yay!! please post here or tag me on Twitter when/if you publish a real post with goodpress, and feel free to contribute to the docs (with suggestions of what can be made clearer / direct PRs) and code!

I just completed one post about the experience I had with goodpress but failed to publish again...

The error is: Error in wp_call_api(VERB = "POST", api_url = paste0(wordpress_url, "/wp-json/wp/v2/tags?name=", : Bad Request (HTTP 400).

My YAML header is:

title: "RMarkdown to WordPress with goodpress"
author: "Henry"
date: "2020-06-25T00:00:00"
output: hugodown::md_document
status: "publish"
slug: "rmarkdown-wordpress-goodpress"
categories: R Programming
tags:
  - R Markdown
  - goodpress
  - WordPress

Could you please check it again? It is posted to WordPress successfully at my first try but failed afterward.

maelle commented 4 years ago

I can't reproduce this, I was able to publish a post with the same tags, and to update it.

Can you try running the code below?

wordpress_url <- "https://rmd-wordpress.eu"
tags <- goodpress:::wp_call_api(
  VERB = "GET",
  api_url = paste0(wordpress_url, "/wp-json/wp/v2/tags")
)
unlist(lapply(tags, "[", "name"))

I'm wondering whether your theme stores the tags differently than what I expect.

maelle commented 4 years ago

Also, please re-install goodpress just in case.

If you look at the code below from the package source, when there are tags I get existing tags from the API, then look whether any tag from the post doesn't exist yet, and so I post the new tag. What happens is that I think the tag already exists but the code tries creating it which must be because the name is different from what I expect :thinking:

https://github.com/maelle/goodpress/blob/55d380b303e1716ba267a42fef2b420534fbc488/R/post.R#L313-L352

maelle commented 4 years ago

So maybe what could work is writing all your tags lower case. Ah no I see this is not lowercase https://henrywang.nl/tag/kindle-vocabulary-builder/

So looking forward to seeing what the code from https://github.com/maelle/goodpress/issues/15#issuecomment-650002503 outputs :-)

maelle commented 4 years ago

Here's the post I've posted https://rmd-wordpress.eu/rmarkdown-wordpress-goodpress/

henrywangnl commented 4 years ago

Can you try running the code below?

here are what I got:


                   name                    name                    name 
   "Assignment problem"     "Association Rules"           "Attribution" 
                   name                    name                    name 
"Binomial Distribution"              "Campaign"              "citation" 
                   name                    name                    name 
                 "code"                  "crul"    "data visualization" 
                   name 
               "Export" 

It seems that not all the tags of my blog are listed here.

maelle commented 4 years ago

ooooh I see there is probably some sort of limit, I'll look into it.

maelle commented 4 years ago

can you try

tags <- goodpress:::wp_call_api(
  VERB = "GET",
  api_url = paste0(wordpress_url, "/wp-json/wp/v2/tags?per_page=100")
)
unlist(lapply(tags, "[", "name"))
henrywangnl commented 4 years ago

Yes, I got all the tags now!

maelle commented 4 years ago

Ok please re-install the package, I've pushed a quick and dirty fix (it works if one doesn't have more than 100 tags).

I have a better fix in mind for later (because one can list tags that have one or certain slugs, same for categories, so I'll create the query using the tags in the post.)

henrywangnl commented 4 years ago

Yes! My new post is live now! Thanks Maelle!

henrywangnl commented 4 years ago

I have another question for you and let me create a new issue...

maelle commented 4 years ago

Thanks for finding problems before others encounter them! :grin:

maelle commented 4 years ago

@henrywangnl yes please open issues, e.g. about your idea reg tags & categories :wink: