2

REST API 経由で WordPress.com ブログに画像をアップロードしようとしています。彼らのドキュメントによると、new postエンドポイントを使用してそうすることができるはずです。現在、Rails とrest-clientを使用して、次のコードを使用してテキスト投稿を正常に作成しています (post_hashすべての投稿情報が含まれています)。

  url = "https://public-api.wordpress.com/rest/v1/sites/" + @blog.external_id + "/posts/new"
  params = {
      pretty = true,
      title: post_hash[:post_title],
      content: post_hash[:post_content],
      categories: post_hash[:terms_names][:category],
      tags: post_hash[:terms_names][:post_tag]
  }
  headers = { authorization: "Bearer " + @oauth_token, content_type: :json }
  RestClient.post(url, params, headers)

前述したように、これは完全に機能します。ここで、画像が添付された新しい「投稿」を作成して、ブログのギャラリーに新しい画像をアップロードできるようにしたいと考えています。このコードを使用してそうしようとしています ( Amazon S3 に保存されてimageいるCarrierWaveファイルです):

  url = "https://public-api.wordpress.com/rest/v1/sites/" + @blog.external_id + "/posts/new"
  image_data = open(image.to_s, "rb").read
  params = {
      format: "image", 
      media: [image_data], 
      multipart: true
  }
  headers = { authorization: "Bearer " + @oauth_token, content_type: image.content_type }
  RestClient.post(url, params, headers)

これは機能せず、400 Bad Requestエラーが発生し続けます。私が間違っていることや、これを行うためのより良い方法についてのアドバイスについて誰かが何か提案があれば、私はそれを大いに感謝します. ありがとう!

4

0 に答える 0