cyperdark / osu-api-extended

Package for advanced work with "osu" api
MIT License
49 stars 15 forks source link

Cannot authenticate for the 'chat.write' scope. #26

Closed CookyNdi closed 3 months ago

CookyNdi commented 3 months ago

Hello there, It might just be a simple oversight, but I'm encountering some issues with authentication while attempting to send a chat message.

Here's the code :

const result = await v2.chat.new(userId, message);

And i'm getting this error:

{
     "error": "A required scope is missing."
}

Then i'm tried adding authentication:

// chat.ts
await osuAuth(['chat.write']);
const result = await v2.chat.new(userId, message);

// auth.ts
export const osuAuth = async (scope: auth_scopes) => {
  const res = await auth.login(OSU_CLIENT_ID, OSU_CLIENT_SECRET, scope);
  console.log(res);
};

And received this response:

{
    "authentication": "basic"
},

note : I encounter no issues when using the function for a beatmap with "public" scope, but when I attempt to add a scope for chat, some problems arise.

cyperdark commented 3 months ago

you need to login via login_lazer

CookyNdi commented 3 months ago

I attempted to log in via lazer just now it worked, but nothing happened with chat v2.chat.new.

const message = 'hi';
const userId = '16983379';
await lazerLogin();
const result = await v2.chat.new(userId, message);

If I console the result, it is just an empty string.

note : every time i make a request with lazer auth i keep getting a verification email from osu

cyperdark commented 3 months ago

every time i make a request with lazer auth i keep getting a verification email from osu

cache auth token and reuse it later on, until it expires

If I console the result, it is just an empty string.

is message sended to user?

CookyNdi commented 3 months ago

is message sended to user?

No, no new messages coming into the id i entered

cyperdark commented 3 months ago

will look tomorrow what is wrong

for now, can you try making a token caching, and after verify latest auth, and then try running request again, maybe it'll fix it

CookyNdi commented 3 months ago

I tried to save the previous token in a file, I also verified it by clicking the link in the email

// chat.ts
const token = readTokenFromFile();
if (!token || token.expires_in < Date.now()) {
  await lazerLogin();
}
console.log({ token });
const result = await v2.chat.new(userId, message);

// auth.ts
export const lazerLogin = async () => {
  const res = await auth.login_lazer('', '');
  saveTokenToFile(res);
  console.log('lazer', res);
};

but still getting this error "error": "A required scope is missing."

i not really understand how auth work in this part

GabuTheDev commented 3 months ago

but still getting this error "error": "A required scope is missing."

Hi! Iirc all auth methods require a scope array. In auth.login_lazer() it's the last parameter. ["public"] should do just fine.

CookyNdi commented 3 months ago

In auth.login_lazer() it's the last parameter. ["public"] should do just fine.

it's getting error image

cyperdark commented 3 months ago

i just checked, and it's working

  1. auth with caching token
  2. confirm account verification in email
  3. restart the code
const { auth, v2 } = require('osu-api-extended');
const fs = require('fs');

const lazer_auth = async () => {
  const txtPath = 'token2.txt';
  if (fs.existsSync(txtPath)) {
    const token = fs.readFileSync(txtPath, 'utf8');

    auth.set_v2(token);
    return
  };

  const cli = await auth.login_lazer(nickname, password);
  console.log(cli);

  fs.writeFileSync(txtPath, cli.access_token, 'utf8');
};

async function main() {
  try {
    await lazer_auth();

    const result = await v2.chat.new(2070907, 'hi');
    console.log(result);

  } catch (error) {
    console.log(error);

  };
};

main();
CookyNdi commented 3 months ago

thank you so much, i'm just missing auth.set_v2(token);