LoginSignup
4
4

More than 5 years have passed since last update.

twitter API で update_with_media を成功させる

Last updated at Posted at 2014-07-12

今回はこちらを使いました

Python 3.x のとき

Python 3.x で update_with_media( ) を行う場合は api.py に以下の修正が必要です。

 bod.append('Content-Disposition: form-data; name="%s"' % mediafield)
+ bod.append('Content-Transfer-Encoding: base64')

- body = '\r\n'.join(bod)
+ body = '\r\n'.join(bod).encode('utf-8')

https://github.com/sixohsix/twitter/issues/233

また投稿するときにファイルを base64 で decode している場合は data = base64.b64encode(f.read()).decode("utf8", "ignore") のような操作が必要でした

ふつうの画像であれば必要ないかもしれません # 未確認

投稿する際のパラメータ

メッセージのみの投稿の際はキーワード引数で出来たのですが update_with_media( ) で同様のことを行ったときにエラーがでました

この場合は辞書型で渡す必要がありました

params = {"media[]": data,
          "status": msg
         }
tw.statuses.update_with_media(**params)
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4