PythonでPinterest APIを使ってPin (画像)を登録する

Pinterest API

この記事のまとめ:
  • PythonでPinterest APIを使って、Pinを投稿する。
背景:

以前Twitter APIを使ってボット的なものを作るための記事を書きましたが、Pinterestでも同じことをPythonでPinterest APIを使って行いたいと思います。

Pinterest API:

まずPinterest APIについて調べた結果からまとめます。 なお、Pinterest APIで使えるものは下記から調べることができます。

まとめて一覧にするとPinterest APIでできることは下記の通りです(2017.7.23現在)。

Scope Sub-scope Sub-sub-scope Control
Boards - - Create a Board
Delete a Board
Edit a Board
Retrieve information about a Board
Retrieve the Pins on a Board
Me - - Return the logged in user's information
Borads - Return the logged in user's Boards
Suggested Return suggestions for the logged in user
Followers - Return the users that follow the logged in user
Following Boards Get the Boards that the logged in user follows
Boards Follow a Board
Boards Unfollow a Board
Interests Return the Interests the logged in user follows
Users Return the users that the logged in user follows
Users Follow a user
Users Unfollow a user
Pins - Return the logged in user's Pins
Search Boards Search the logged in user's Boards
Pins Search the logged in user's Pins
Pins - - Create a Pin
Delete a Pin
Edit a Pin's information
Return information about a Pin
Users - - Return a user's information

とりあえずPin (画像)を投稿するには十分なAPIはありそうですので使ってみたいと思います。

Pinを投稿する

Pinを投稿するためのAPIは"Create a Pin"に記載されているAPIを使えばPinを投稿できます。

API詳細

Pinを投稿するためのAPIの詳細はこちらに記載されていますが、要するに下記のパラメーターをつけてHTTP Postをすれば良いということです。

Parameters Description
access_token (required) 認証用のトークン情報です。
board (required) 新しいPinを投稿するboard情報です。フォーマットは"<user_name>/<board_name>"です。
note (required) Pinの説明です。
link (optional) PinをクリックしたときにリンクされるURLです。
image* マルチパートフォームデータを使ったPin画像をアップロードします。
image_url* Pin画像のリンクです。
image_base64* Base64形式でエンコードさらた画像のリンクです。

*image, image_url, image_base64の内、どれかは必ず必要です。

ここで、いろいろ試してみたのですが、"image"と"image_base64"の使い方がわかりません、というかホストサーバーによって接続を拒否されてしまってどう使っていいのかわかりませんでした。

そのため、このあとは"image_url"のみについて説明します。 どなたかご存じの方がおられればコメントいただけると幸いです。

アクセストークンの取得

上記パラメーターにあるAPIを利用するために認証用のアクセストークンを取得します。

トークンは、OAuth認証で取得することもできますが、下記から直接取得することもできます。 https://developers.pinterest.com/tools/access_token/

scopeのチェックボックスは使用するAPIに合わせてチェックしてください。

サンプルコード

パラメーター類はご利用に合わせて変更してください。

import requests
 
access_token = "xxxxxx" # read_public, write_public
board        = "<user_name>/<board_name>"
note         = "Test Note"
link         = "http://www.xxxxx.jp"
image_url    = "http://www.xxxxx.jp/xxxxx.jpeg"
 
response = requests.post(
    "https://api.pinterest.com/v1/pins/",
    params={
        "access_token":access_token,
        "board":board,
        "note":note,
        "link":link,
        "image_url":image_url
    },
)

今回は以上です。 最後まで読んでいただき、ありがとうございます。


ブログランキング・にほんブログ村へ  ← 気に入っていただければ応援ポチをお願いします!

コメント

このブログの人気の投稿

LinuxでのnVidia GPUのオーバークロック・電力チューニング方法

ネットワーク越しの RTL-SDR で SDR# を使う方法