I have followed all instructions here: Retrieving media | Docs | Twitter Developer Platform

However The OAuth Get response is always a HTTP 302 - redirect.
I have attempted to GET the 302-location, but I still get same 302 response.

I have attempted to setup an <img> with src similar to the example, but the response body is empty and status is till 302.

Please can anyone provide clarity on this issue.

Thanks.

What library/ language are you coding in? Redirects should be followed to the actual content but sometimes the library needs some extra parameter or something to actually get the file contents

1 Like

Thanks for the reply!.

I’m coding this in Elixir, I have a full view of the HTTP response stream.

I can see all the headers returned and the status codes.

Does this help? HTTPoison — HTTPoison v1.8.2 I don’t know Elixir so I’ve no idea if this is what you’re using but this library seems to be able to follow redirects

I’m able to follow the redirects, by sending a second request once i see a HTTP302, i’m using Tesla Library, but i will see if using auto-redirect helps.

1 Like

no luck, I get a redirect cascade, and eventually a redirect overflow.

Please can you point me to an implementation of Twitters DM media download that actually works?

I can use such as a guide.

Hi , Good Morning, Please do you know of any open-source implementation for this?

I have left a complaint @twitter documentation but I have seen not mention of 302 issues or a response

1 Like

It still seems like a library or authentication issue to me - because i can successfully get the image like this in python:

from requests_oauthlib import OAuth1Session
import shutil

api = OAuth1Session(
                client_key="...",
                client_secret="...",
                resource_owner_key="...-...",
                resource_owner_secret="...",
            )

response = api.get("https://ton.twitter.com/i/ton/data/dm/.../.../....jpg", stream=True)

print(response.status_code)

with open("image.jpg", "wb") as f:
    shutil.copyfileobj(response.raw, f)

Not sure how helpful this is, but this makes an oAuth1 authenticated GET to a Media URL from a DM, and saves it as a file. There are no redirects in this request, the status is 200.

Now to complete the Retrieving media | Docs | Twitter Developer Platform guide, you would implement an endpoint on your backend, that will take an image URL, authenticate it with oAuth1, and serve it like this:

<img src="fetch_dm_image?url=https://ton.twitter.com/i/ton/data/dm/....jpg">

for example, in python:

Need these dependencies:

pip install fastapi uvicorn requests requests-oauthlib

This would be fetch_image_api.py :

from fastapi import FastAPI
from fastapi.responses import StreamingResponse
from requests_oauthlib import OAuth1Session

app = FastAPI()

@app.get("/fetch_dm_image")
def fetch_dm_image(url: str):

    api = OAuth1Session(
        client_key="...",
        client_secret="...",
        resource_owner_key="...-...",
        resource_owner_secret="...",
    )

    response = api.get(
        url,
        stream=True,
    )

    print(url)
    print(response.status_code)

    return StreamingResponse(response.raw, media_type="image/jpeg")

Run it

uvicorn fetch_image_api:app --reload

And now this should work:

http://127.0.0.1:8000/fetch_dm_image?url=https://ton.twitter.com/i/ton/data/dm/.../.../....jpg

and so will this on a page:

<img src="fetch_dm_image?url=https://ton.twitter.com/i/ton/data/dm/....jpg">
1 Like

I got similar directions here: DM Media download results in redirection loop - #4 by yfilyk

I will review the library and see

1 Like

Maybe the library I’m using is the problem, but i’ll keep investigating

1 Like

There was a mistake in my code, it all works fine now, no more 302 errors, thanks so much.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.