0

Before i get 100 people telling me that automating user accounts is against the Terms of Service, I know that and i am willing to take the risk, i just came here for help because i've been trying for hours.

I checked how to change my profile picture on Discord's API Reference but when i tried, i got a '400: Bad Request' response. here is my code:

const fetch = require('node-fetch');
const fs = require('fs');
const tokens = fs.readFileSync('./tokens.txt', 'utf8').split('\n');

console.log('Changing profile pictures...');
let pfp = fs.readFileSync('./pfp.png', 'base64');
tokens.forEach(token => {
    fetch('https://discord.com/api/v8/users/@me', { method: 'PATCH', headers: { 'content-type': 'application/json', authorization: token.trim() }, body: { "avatar": `data:image/png;base64,${pfp}` } } )
    .then(res => res.json())
    .then(json => console.log(json));
});

I have checked and the tokens are correct when the request is send, so is the base64 encoded image, but i get a '400: bad request' response, so something must be wrong, but I unfortunately can't figure out what that thing is. If anyone could help i would appreciate it a lot

c8h_
  • 143
  • 1
  • 9
  • i think you have to read it as a binary buffer then convert it to base64, `openFileSync` wont do that. – Daniel A. White Oct 25 '20 at 18:09
  • @DanielA.White i am using `readFileSync` and it does support an encoding option, i checked that it does convert correctly and i decoded it back to the image correctly – c8h_ Oct 25 '20 at 18:10
  • Does the response contain any error message besides the status code? – derpirscher Oct 25 '20 at 18:29
  • @derpirscher nope, here's the response i get: `{ message: '400: Bad Request', code: 0 }` – c8h_ Oct 25 '20 at 18:31

2 Answers2

0

According to the docs, the Authorization Header has to look as follows

Authorization: TOKEN_TYPE TOKEN

so for instance

Authorization: Bot MTk4NjIyNDgzNDcxOTI1MjQ4.Cl2FMQ.ZnCjm1XVW7vRze4b7Cq4se7kKWs

or

Authorization: Bearer CZhtkLDpNYXgPH9Ml6shqh2OwykChw

In your request you only seem to send

Authorization: token

and missing the token type.

derpirscher
  • 14,418
  • 3
  • 18
  • 35
  • i have made loads of api requests to discord's api and just having `authorization: token` works just fine. i tried making other requests today too and they work fine, i just get a bad request when i try to change my avatar. – c8h_ Oct 25 '20 at 18:53
0

I just found out that i have to do JSON.stringify() for the body of the response.

c8h_
  • 143
  • 1
  • 9