Open joey-kwl opened 3 years ago
I think Reddit does not allow that
Edit: It's more than possible, in fact PRAW does support that, it's just that the API endpoints used to upload media are undocumented.
That's how I did it:
async function uploadMedia(filename, mimetype, blob) {
const uploadResponse = await requester.oauthRequest({
uri: "api/media/asset.json",
method: "post",
form: {
filepath: filename,
mimetype
}
})
const uploadURL = "https:" + uploadResponse.args.action
const formdata = new FormData()
uploadResponse.args.fields.forEach(item => formdata.append(item.name, item.value))
formdata.append('file', blob, filename)
const response = await fetch(uploadURL, {
method: "post",
mode: "no-cors",
body: formdata
})
return {
asset_id: uploadResponse.asset.asset_id,
link: uploadURL + "/" + uploadResponse.args.fields.find(item => item.name === "key").value,
websocket_url: uploadResponse.asset.websocket_url
}
}
fetch("https://i.imgur.com/LYcDAGm.png").then(async res => {
const blob = await res.blob()
console.log(await uploadMedia("LYcDAGm.png", "image/png", blob))
})
Any undocumented endpoints are subject to change at any moment as Reddit will not have a deprecation policy for them. I'm skeptical about including undocumented endpoints as part of Snoowrap itself
Any undocumented endpoints are subject to change at any moment as Reddit will not have a deprecation policy for them. I'm skeptical about including undocumented endpoints as part of Snoowrap itself
then I suppose we should remove "full featured" from wrapper description, because basically none of reddit api wrappers for js can submit images, though most of them calls itself full-featured
@SpyTec The official API docs aren't a reliable source anymore since they haven't got updated for a while. I think snoowrap should really consider switching to support all known endpoints instead of documented endpoints only.
Edit: It's more than possible, in fact PRAW does support that, it's just that the API endpoints used to upload media are undocumented.
That's how I did it:
async function uploadMedia(filename, mimetype, blob) { const uploadResponse = await requester.oauthRequest({ uri: "api/media/asset.json", method: "post", form: { filepath: filename, mimetype } }) const uploadURL = "https:" + uploadResponse.args.action const formdata = new FormData() uploadResponse.args.fields.forEach(item => formdata.append(item.name, item.value)) formdata.append('file', blob, filename) const response = await fetch(uploadURL, { method: "post", mode: "no-cors", body: formdata }) return { asset_id: uploadResponse.asset.asset_id, link: uploadURL + "/" + uploadResponse.args.fields.find(item => item.name === "key").value, websocket_url: uploadResponse.asset.websocket_url } } fetch("https://i.imgur.com/LYcDAGm.png").then(async res => { const blob = await res.blob() console.log(await uploadMedia("LYcDAGm.png", "image/png", blob)) })
This still works. Should be incorporated in the library,
If I want to upload an image I have to upload via Imgur or other images hosting, but Reddit have their own images hosting platform: