从R上传图片至WordPress媒体库

3

有人尝试使用R中的POST命令通过REST API将图像上传到WordPress吗?

我以为这样做可以,但没有成功。

POST(paste0(site,"wp/v2/media"),              
add_headers(
          'Authorization' = paste("Bearer", token, sep = " "), 
          'cache-control' = "no-cache", 
          'content-disposition' = "attachment; filename=test.png",
          'content-type' = "image/png"), 
      body = list(
        title = "page_title", 
        status = "published", 
      encode = "json") 
)

你好,我在这个问题上遇到了困难...你最终解决了吗? - Charles Stangor
我可以通过Postman选择一个二进制文件来完成这个任务。 - Charles Stangor
这个很接近了,但是返回了500错误。不知何故我没有正确指定文件。POST("https://example.org/wp-json/wp/v2/media", add_headers('Authorization' = paste("Bearer", token, sep = " "), 'cache-control' = "no-cache", 'content-disposition' = "form-data; filename=Capture.png", 'content-type' = "image/png"), body=list(name="new_file", filedata=upload_file("c:/users/cases.png", "image/png"))) - Charles Stangor
我可以看到问题与R的POST请求生成有关。这里有一个PHP示例代码,通过WordPress REST API上传图像 https://gist.github.com/ahmadawais/0ccb8a32ea795ffac4adfae84797c19a#file-upload-a-file-md-readme 你可以检查代码并使用R模仿相同的curl头和正文数据。 - Intelligent Web Solution
2个回答

3
通过 POST /wp/v2/media 端点 创建新附件可以有两种方式:
  1. 设置 Content-Disposition 请求头为 attachment; filename=<file name>,设置 Content-Type 请求头为正确的 MIME 类型,例如 image/jpeg,然后将请求正文设置为二进制文件内容。

  2. 设置 Content-Type 请求头为 multipart/form-data; boundary=<boundary>,然后将请求正文设置为多部分数据,类似于您可以在这里看到的,其以 ---------------------------<boundary> 开始并以 ---------------------------<boundary>-- 结束。

    而对于 WordPress,使用此选项的优点是可以设置自定义附件文章数据,例如文章标题和别名。但请确保媒体文件的表单数据名称确切为 file —— 而不是 filedatafile_content 或其他名称。

在 R 中,您可以轻松地执行上述操作,只需设置 Authorization 请求头为 Bearer <token>,并上传 JPEG 图像,如下所示:

基于上述选项 1 的示例

r <- POST(
    'https://example.com/wp-json/wp/v2/media',
    add_headers(
        'Authorization' = paste('Bearer', token, sep = ' '),
        'Content-Disposition' = 'attachment; filename="test-using-R-4.0.4.jpg"',
        'Content-Type' = 'image/jpeg'
    ),
    body = upload_file('test.jpg', 'image/jpeg')
)
d <- content(r)

cat( ifelse(! is.null(d[['id']]),
    paste('success - id:', d['id']),
    paste('error:', d['message']))
)

基于上述选项2的示例

r <- POST(
    'https://example.com/wp-json/wp/v2/media',
    add_headers(
        'Authorization' = paste('Bearer', token, sep = ' '),
    ),
    body = list(
        file = upload_file('test.jpg', 'image/jpeg'),
        title = 'custom title',
        slug = 'custom-slug'
    )
)
d <- content(r)

cat( ifelse(! is.null(d[['id']]),
    paste('success - id:', d['id']),
    paste('error:', d['message']))
)

如果您正在使用WordPress 5.6中引入的默认应用程序密码功能,则应使用authenticate()而不是上述选项2中的add_headers()

# Replace this:
add_headers(
    'Authorization' = paste('Bearer', token, sep = ' '),
),

# with this one:
authenticate('<your WordPress username>', '<your WordPress Application Password>'),

我希望这些示例在 R-4.0.4 中经过了尝试和测试,并且对你也有效。


1
这正是我所需要的!请注意,您可能需要在您的网站上禁用或重新配置Modsecurity才能让REST API正常工作。 - jafelds

1
我有同样的问题...你是否设法解决了它?
有一个名为wordpressr的软件包正在开发中...然而上传媒体尚未被纳入:wordpressr 所以目前我正在尝试一些设置,但也有点迷失。
但是我看到的是,您必须在代码中某个地方进行身份验证才能上传媒体。这可以通过Application Password Plugin完成。应将其纳入您的代码中,类似于以下内容:
user = "yourusername"
password = "XXXX XXXX XXXX XXXX XXXX XXXX" (generated with the plugin)

然后将其添加到您的代码中:(并排除“Authorization”=粘贴(“Bearer”,token,sep =“ ”我猜?)

POST(paste0(site,"wp/v2/media"),
authenticate(user, password, type = "basic"),              
add_headers(
           
          'cache-control' = "no-cache", 
          'content-disposition' = "attachment; filename=test.png",
          'content-type' = "image/png"), 
      body = list(
        title = "page_title", 
        status = "published", 
      encode = "json") 
)

此外,正文中有一个“upload_file(“path /”)”,我认为也应该被确定,对吗?

我可能会回到这个问题。我在想为什么它这么难? - Charles Stangor
你能分享一下吗?我已经在我的问题上设置了赏金! - Charles Stangor
是的,我会看一下如何把它封装成一个包。 - H. berg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接