Fetching media by urls with Rest API

只谈情不闲聊 提交于 2020-01-06 04:18:12

问题


I would like to retrieve media inside statuses, but as I could read in the docs:

For media in direct messages, media_url_https must be accessed via an authenticated twitter.com session or by signing a request with the user’s access token using OAuth 1.0A. It is not possible to directly embed these images in a web page

Now my question is, how to do that kind of request using Java API? or however what is the correct request with RestAPI, using App only auth?

another question: what is the difference for media in direct messages and media shared in Tweets? how can I see the difference inside json object representing a status?


回答1:


what is the difference for media in direct messages and media shared in Tweets?

If an image is posted to a Twitter timeline, any Internet user can see it. For example on this Tweet the image is available to anyone, whether or not they're logged into Twitter. See https://pbs.twimg.com/media/CSGwFynWoAA2GGG.jpg

If an image is posted into a Direct Message, it is only possible for the sender and receiver to see the image. In order to enforce that, the user must either be signed in to their Twitter account, or send a signed request via an app.

For example, this image can only be seen by me & the person I sent it to https://ton.twitter.com/i/ton/data/dm/639737026624024579/639737026682691584/tg-4St-1.jpg

how can I see the difference inside json object representing a status?

The only difference (other than the API call you used to get the data) is the URl of the media. In a DM it starts with ton. e.g.

media_url": "https://ton.twitter.com/1.1/ton/data/dm/......

Whereas in a normal Tweet, it will look like e.g.

"media_url": "http://pbs.twimg.com/media/.......

Now my question is, how to do that kind of request using Java API? or however what is the correct request with RestAPI, using App only auth?

If the user is already logged in to Twitter, they will be able to download the image. For example, if you have written a web-app and the browser already contains the Twitter cookies etc.

If not - or you're using an app - you will need to sign the request on the user's behalf.

For REST - you need to send OAuth headers with the request. There's example code in the Twitter documentation

The extra header will look something like

Authorization: OAuth oauth_consumer_key="xvz1evFS4wEEPTGEFPHBog", oauth_nonce="kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg", oauth_signature="tnnArxj06cWHq44gCs1OSKk%2FjLY%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1318622958", oauth_token="370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb", oauth_version="1.0"

For Java, I'd suggest you take a look at how Twidere does it.



来源:https://stackoverflow.com/questions/33364391/fetching-media-by-urls-with-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!