As the error says, you can’t use that media ID, most likely because you did not upload it with the same user access token. You can’t attach any old media IDs in DMs, they have to be media IDs from media that you have uploaded as far as I remember

But i am using dm_conversation with oauth 2.0 authentication and used media upload from oauth 1.0a authentication. That’s the issue i am not allowed to attach media.


i am using oauth1.0a for dm

and for media upload also using the same authentication

Oh, that’s surprising, I would have expected that to work.

Does sending the DM with v2 API but with oAuth 1.0a instead of oAuth 2 PKCE authentication work?

When uploading the media, does it work if you set the shared parameter to true Attaching media | Docs | Twitter Developer Platform

Yes, I am using dm_conversation DM with oauth2 PKCE authentication, and it’s working fine.
And for media upload,I am referring

this URl and in this there is no parameter called shared that I need to set to true.

Yes, it works with v2 API with oAuth1.0a but still got an error if I attach media IDs in it.

The Attaching media | Docs | Twitter Developer Platform recommends using the Chunked Upload method, not the single endpoint call - but worth trying to set the single endpoint call upload media_type to dm_image - does that work?

While uploading media using the INIT method I need to pass bytes, media_type, command = INIT, and media_category. if I use this endpoint while using the given parameters, how can I identify that the uploaded media is correct which I am looking for and bytes are less than 1MB in size?

That part you have to do yourself, in your own code or another tool if you’re trying to make calls manually

No, I am asking that if i used this endpoint POST media/upload | Docs | Twitter Developer Platform to generate the media Id. Then I got the error of “You are not permitted to attach this media to a DM event”. And if I use this endpoint POST media/upload (INIT) | Docs | Twitter Developer Platform. I got the error of “service unavailable”. And I want to use the first given endpoint to upload an image. because in second (INIT) endpoint ,its not defined that which media I need to upload.

Oh, I see - service unavailable shouldn’t be happening. It’s usually a temporary error though, so worth trying again later, if the call is otherwise correct

But I got this error multiple times when i tried to use the media id which i got from the upload(INIT) endpoint.

Did you set the shared parameter to true and media_category to dm_image when using POST media/upload (INIT) | Docs | Twitter Developer Platform ? Also do you get any errors when you complete the other steps in the chunked upload flow? The media ID you get back from just the init call is not valid to use right away. POST media/upload (APPEND) | Docs | Twitter Developer Platform should be called one or more times with the image data, and finally POST media/upload (FINALIZE) | Docs | Twitter Developer Platform will give you a valid media ID.

After completing these steps without errors, does a call to GET media/upload (STATUS) | Docs | Twitter Developer Platform return a success for the upload? If successful, it should be possible to use in a DM

I follow all the steps that you provided.On the media/upload(FINALIZE). I got the given error

What exact image did you try to upload and what APPEND calls did you make? How did you split the data or was it all in 1 call?

I am using this image

also its type and size i used in upload(INIT)


And when i APPEND this with media ID i got this error

The call you show is FINALIZE, did the APPEND work? Did you send the data as Base64 or as binary?


on APPEND, its show 204 No content

I think there’s still some confusion:

As above, media_data has to be Base64 encoded image data, not a URL.

segment_index starts with 0.

Also i think another source of error is in Postman, inconsistently specifying parameters as URL parameters instead of form data and not specifying this in the request settings. Try to set parameters in Body in postman, and specify multipart/form-data when uploading media, to stay consistent.

I was able to successfully DM the image, specifying all the parameters in the docs.

For all of these requests, the Authorization is set to oAuth1.0 Request Headers, as:

Initialize the upload: POST media/upload (INIT) | Docs | Twitter Developer Platform

Note the size in bytes you specified is different for the dog picture - this may or may not be due to re-saving it from the forum, so double check that too.

Append the image data, POST media/upload (APPEND) | Docs | Twitter Developer Platform this is the image file encoded into base64, it should look like this (... is a bunch more base64 data):

/9j/4AAQSk...Jw1saJSmT//Z

The result is 204, No Content.

Then Finalize the upload: POST media/upload (FINALIZE) | Docs | Twitter Developer Platform

Now, again using oAuth1.0, but this time with v2 API to send a DM: POST /2/dm_conversations/with/:participant_id/messages | Docs | Twitter Developer Platform

Note: This request has both Params and a Body, So make sure to set the recipient user ID as participant_id in Params:

Screenshot from 2023-02-14 14-41-34

The body has the uploaded DM image ID as an attachment:

{
  "text": "Sending a DM with media!",
  "attachments": [
    {
      "media_id": "1625498642814730242"
    }
  ]
}

The DM is successfully posted:

Screenshot from 2023-02-14 14-26-41

I have tried the following steps that you shared with me. but getting this error
INIT-

APPEND-

FINALIZE


can you help me in that

Also, If I used the media ID which I got from INIT and passed it on to APPEND. and getting an error in FINALIZE. Help me in that why getting “segment do not provide total file size”.
But if I used that media id in attachments


It’s giving me an error of “service unavailable”. And I am getting this error from number of times from past few days.