Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Img method for GET image from DM #1080

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

sterzuccio
Copy link

Add img method for get Image from twitter

add img method for get Image from twitter
@sterzuccio sterzuccio changed the title Update TwitterOAuth.php Img method for GET image from DM May 23, 2022
@CharlesOkwuagwu
Copy link

@sterzuccio Good day,

I made a similar implementation following Twitter docs directions from here: Message attachments#Retrieving media

I keep getting 302 redirections.
I wanted to ask if you had a similar experience and how you resolved the same.

Thanks.

@sterzuccio
Copy link
Author

I made a simple modification to be able to take the images I receive from the Twitter chat. So far everything is working fine and I have not received any 302s.

@CharlesOkwuagwu
Copy link

I made a simple modification to be able to take the images I receive from the Twitter chat. So far everything is working fine and I have not received any 302s.

Are you referring to the Direct Messages returned from Account Activity API?

@sterzuccio
Copy link
Author

Yes, Direct Messages and Twitter API

@CharlesOkwuagwu
Copy link

Yes, Direct Messages and Twitter API

I'm so confused about this

**raw response:**

 14:56:35.753 {:tw_rx,
 %{
   direct_message_events: [
     %{
       created_timestamp: "1657806995353",
       id: "1547580822294646788",
       message_create: %{
         message_data: %{attachment: %{media: %{display_url: "pic.twitter.com/J9dz8mhUqz", expanded_url: "https://twitter.com/messages/media/1547580822294646788", id: 1547580751654203394, id_str: "1547580751654203394", indices: [21, 44], media_url: "https://ton.twitter.com/1.1/ton/data/dm/1547580822294646788/1547580751654203394/T7xt1pvd.jpg", media_url_https: "https://ton.twitter.com/1.1/ton/data/dm/1547580822294646788/1547580751654203394/T7xt1pvd.jpg", sizes: %{large: %{h: 1000, resize: "fit", w: 1000}, medium: %{h: 1000, resize: "fit", w: 1000}, small: %{h: 680, resize: "fit", w: 680}, thumb: %{h: 150, resize: "crop", w: 150}}, type: "photo", url: "https://t.co/J9dz8mhUqz"}, type: "media"}, entities: %{hashtags: [], symbols: [], urls: [%{display_url: "pic.twitter.com/J9dz8mhUqz", expanded_url: "https://twitter.com/messages/media/1547580822294646788", indices: [21, 44], url: "https://t.co/J9dz8mhUqz"}], user_mentions: []}, text: "test image with keys https://t.co/J9dz8mhUqz"},
         sender_id: "771240713891618816",
         target: %{recipient_id: "2778493872"}
       },
       type: "message_create"
     }
   ],
   for_user_id: "2778493872",
   users: %{
     "2778493872": %{created_timestamp: "1409320915097", description: "Leadway Pensure provides pension funds management solutions that ensure you get more out of life. #ThePensureAdvantage #GetMoreOutOfLife", followers_count: 15075, friends_count: 33, id: "2778493872", location: "01-2800800", name: "Leadway Pensure", profile_image_url: "http://pbs.twimg.com/profile_images/1061220994319290369/8kWp2XlT_normal.jpg", profile_image_url_https: "https://pbs.twimg.com/profile_images/1061220994319290369/8kWp2XlT_normal.jpg", protected: false, screen_name: "LeadwayPensure", statuses_count: 4875, url: "https://t.co/cpCMGcuTgk", verified: false},
     "771240713891618816": %{created_timestamp: "1472713089970", followers_count: 20, friends_count: 77, id: "771240713891618816", location: "Lagos, Nigeria", name: "Charles Okwuagwu", profile_image_url: "http://pbs.twimg.com/profile_images/771243580182499329/oPn1mw4N_normal.jpg", profile_image_url_https: "https://pbs.twimg.com/profile_images/771243580182499329/oPn1mw4N_normal.jpg", protected: false, screen_name: "charles_okw", statuses_count: 92, verified: false}
   }
 }}
 
 **successful OAUTH request with 302 return**

14:56:36.838 %Tesla.Env{
  __client__: %Tesla.Client{adapter: {Tesla.Adapter.Hackney, :call, [[recv_timeout: 3600000, ssl_options: [verify: :verify_none]]]}, fun: nil, post: [], pre: []},
  __module__: Tesla,
  body: "",
  headers: [
    {"date", "Thu, 14 Jul 2022 13:56:37 UTC"},
    {"server", "tsa_f"},
    {"location", "https://ton.twitter.com/1.1/ton/data/dm/1547580822294646788/1547580751654203394/T7xt1pvd.jpg"},
    {"set-cookie", "guest_id_marketing=v1%3A165780699777753227; Max-Age=63072000; Expires=Sat, 13 Jul 2024 13:56:37 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None"},
    {"set-cookie", "guest_id_ads=v1%3A165780699777753227; Max-Age=63072000; Expires=Sat, 13 Jul 2024 13:56:37 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None"},
    {"set-cookie", "personalization_id=\"v1_GaO07mT0dY1mzN69QEW2jA==\"; Max-Age=63072000; Expires=Sat, 13 Jul 2024 13:56:37 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None"},
    {"set-cookie", "guest_id=v1%3A165780699777753227; Max-Age=63072000; Expires=Sat, 13 Jul 2024 13:56:37 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None"},
    {"cache-control", "no-cache"},
    {"content-length", "0"},
    {"timing-allow-origin", "https://twitter.com, https://mobile.twitter.com"},
    {"x-content-type-options", "nosniff"},
    {"strict-transport-security", "max-age=631138519"},
    {"x-response-time", "141"},
    {"x-connection-hash", "ee07adeac69d07ada31e42538b71c8e081fe30400c6f7eef89e78711d17320b9"}
  ],
  method: :get,
  opts: [],
  query: [],
  status: 302,
  url: "https://ton.twitter.com/1.1/ton/data/dm/1547580822294646788/1547580751654203394/T7xt1pvd.jpg"
}

@sterzuccio
Copy link
Author

code 302 is a temporary redirect code. I don't know why it gives you this error. But can you download / view the image? Or can't you even download it?

@CharlesOkwuagwu
Copy link

CharlesOkwuagwu commented Jul 14, 2022

code 302 is a temporary redirect code. I don't know why it gives you this error. But can you download/view the image? Or can't you even download it?

I can view it for sure on Twitter.

But via code, no luck.

Nothing in the docs helps at all...

however, this line: The endpoint must deny all cross-origin requests
I'm using NGROK to host my localhost, I'm wondering if that might be it

@sterzuccio
Copy link
Author

have you tried to fork my code? maybe you have more luck and you can start from that

@CharlesOkwuagwu
Copy link

have you tried to fork my code? maybe you have more luck and you can start from that

I have used the OAuth code here as a reference.
I'm working in Elixir Language, all the other OAuth requests work without issues.

@sterzuccio
Copy link
Author

I have no knowledge of elixir. If it can help you, the problem that the library had was that it didn't always take the file size and I solved it by putting an if in line which, in case it doesn't find the size, it just puts 0

@CharlesOkwuagwu
Copy link

I have no knowledge of elixir. If it can help you, the problem that the library had was that it didn't always take the file size and I solved it by putting an if in line which, in case it doesn't find the size, it just puts 0

I'm not sure I follow what you mean by file size, but do you mean attaching one of the size-keys from the sizes like below.

https://ton.twitter.com/i/ton/data/dm/1547580822294646788/1547580751654203394/T7xt1pvd.jpg:small

when I right-click an image in Twitter Direct Message and view in a new tab, the image URLs always have a :size eg :small attached.

IF I remove this :small, the browser says 404

@sterzuccio
Copy link
Author

does not show me the image, 401 error because I do not have the authorization. The link that opens to you from the right click has nothing to do with the link that passes you in the DM if I remember correctly. That is generated differently I think.

@CharlesOkwuagwu
Copy link

does not show me the image, 401 error because I do not have the authorization. The link that opens to you from the right click has nothing to do with the link that passes you in the DM if I remember correctly. That is generated differently I think.

Just saw these 2:

https://twittercommunity.com/t/fetching-dm-media-problems-302-response-code/131887
https://twittercommunity.com/t/dm-media-download-results-in-redirection-loop/95301

Maybe I'll get something.

Thanks again for your time.

@sterzuccio
Copy link
Author

I have read. From what I understand the problem lies in the authentication. To be able to view, and therefore to be able to download, an image from a DM you must be authenticated. You should develop the code with this in mind. I believe there are no different solutions.

@CharlesOkwuagwu
Copy link

Sure I am using Oauth authentication, as usual, what is confusing is the conversation from https://twittercommunity.com/t/dm-media-download-results-in-redirection-loop/95301

seems he had to manually modify the headers which is weird

@sterzuccio
Copy link
Author

the code suggested as a solution doesn't work for you?

@CharlesOkwuagwu
Copy link

the code suggested as a solution doesn't work for you?

I will try that and see if I Can get same or similar response, apparently something is foul in my OAuth implementation

@sterzuccio
Copy link
Author

Let me know if you can find a solution. I'm curious. Good luck with the development.

@CharlesOkwuagwu
Copy link

@sterzuccio so sorry for all this disturbance I had hard-coded post instead of get in my OAuth routine

@sterzuccio
Copy link
Author

no disturbance ☺️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants