12/10(火)にTwitter公式アプリ(iOS/Android版)でDMに画像を添付できるようになりました。
この影響で、拙作のTwitPaneをはじめとする非公式アプリではDMの画像が取得できなくて空画像が表示されたり、レイアウトが崩れたりしてめんどくさいことになっています。
で、どうすりゃいいのってことで調べてみたんですが、下記で回答されていました。
@episod Taylor Singletary
We have no announced plans yet for providing a media upload endpoint for direct messages.We'll be releasing some documentation this week or early next on how to retrieve images attached to direct messages -- it's pretty simple, using OAuth 1.0A to retrieve the image instead of unauthenticated requests.
ドキュメント準備中ってことですが、つまりOAuth 1.0Aで取れるってことですね。
早速試してみたところちゃんと取れました。
Twitter4J的には内部メソッドのTwitterImpl.getを呼ぶだけでOAuth 1.0A経由になるのでこんな感じになりました。
// DM画像認証対応 MyLog.d("doDownloadImage, download image with twitter4j"); final Twitter twitter = TPUtil.getTwitterInstance(context); try { final Class<? extends Twitter> c = twitter.getClass(); final Method m = c.getDeclaredMethod("get", String.class); m.setAccessible(true); final HttpResponse res = (HttpResponse) m.invoke(twitter, rawImageUrl); // twitter.get(rawImageUrl); final InputStream is = res.asStream(); image = BitmapFactory.decodeStream(is); if (image == null) { // ダウンロード失敗 MyLog.w(" download twitter image, image load error [" + rawImageUrl + "]"); return null; } } catch (Throwable th) { MyLog.e(th); return null; }
Twitter4J v3.0.5でDM添付画像を取得してみた · GitHub
リフレクション使ってるのが汚いのでTwitter4Jにいい感じのインタフェースができるといいなぁ(メソッド名が思いつかない)。
それより(DM送信時の)画像添付APIが早く公開されるといいですねぇ。
ちなみにmixiのメッセージAPIが画像添付に未だに対応してない件、僕は忘れていませんからねw
(2013/12/17追記)
公式にアナウンスがありました。