SchoolUtils / WebUntis

JavaScript WebUntis API Client
https://webuntis.noim.me/
MIT License
166 stars 21 forks source link

Weird error message when trying to log in anonymously #26

Closed 7RST1 closed 4 years ago

7RST1 commented 4 years ago

Hello I am trying to log in with this POST: { method: 'POST', url: "/WebUntis/jsonrpc.do?school=Heyerdahl", data: { id: this.id, method: 'authenticate', params: { user: '#anonymous#', client: this.id }, jsonrpc: '2.0' }

But I get a weird error message in return: Error: Failed to login. {"jsonrpc":"2.0","id":"DiscordBotUntisQuery","error":{"message":"Index: 1, Size: 1","code":-8998}}

Does anyone know if I am doing anything wrong, and what I am doing wrong?

TheNoim commented 4 years ago

I will check this out later.

TheNoim commented 4 years ago

One problem I have is my school doesn't support anonymous login. I can not really test why this error occurs.

7RST1 commented 4 years ago

If you need more school anonymous login schools to cross-examine, you can try "Re vgs", or "Sandefjord Vid.Skole" instead of Heyerdahl. They might have the same issue tho

dunklesToast commented 4 years ago

So I think the problem is, that the public API doens't support anonymous logins. The mobile Apps use another API (jsonrpc_internal.do) which use other routes. Maybe try contacting the Untis Support for this issue as it does not seem to be an issue with this project and more a problem with the API.

TheNoim commented 4 years ago

Because the sessions are shared between private and public apis I could implement an anonymous login. However, I am not 100% sure, but I will try it.

TheNoim commented 4 years ago

I updated the module.


const webuntis = new WebUntis.WebuntisAnonymousAuth(school, baseurl);

await webuntis.login()
7RST1 commented 4 years ago

No cigar. Error in console;

(node:20262) UnhandledPromiseRejectionWarning: Error: Failed to login. Method not found
    at WebuntisAnonymousAuth._otpLogin (/home/syver/Dokumenter/SkoletimerForSkolegården/node_modules/webuntis/index.js:614:10)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async WebuntisAnonymousAuth.login (/home/syver/Dokumenter/SkoletimerForSkolegården/node_modules/webuntis/index.js:773:10)

Login code used;

const WebUntisLib = require('webuntis');

const untis = new WebUntisLib.WebuntisAnonymousAuth(
    'Heyerdahl',
    'romres.ist-asp.com'
);

untis
        .login()
        .then(() => {
            return untis.getTimegrid();
        })
        .then(timetable => {
            console.log(timetable)
        });

Is it possible that my school uses normal login method, just with #anonymous# as the login and no password given?

TheNoim commented 4 years ago

Ok, I did more research with your school in mind. I published version 1.12.0 which should fix your problems.

First of all, I changed WebuntisAnonymousAuth to WebUntisAnonymousAuth. Just a small detail.

Now you can use your code like this:

const WebUntisLib = require('./index');

const untis = new WebUntisLib.WebUntisAnonymousAuth(
    'Heyerdahl',
    'romres.ist-asp.com'
);

untis
    .login()
    .then(() => {
        return untis.getTimegrid();
    })
    .then(timetable => {
        console.log(timetable)
    })
    .then(() => {
        return untis.getClasses();
    })
    .then(klasses => {
        const testKlasse = klasses[0];
        return untis.getTimetableForToday(testKlasse.id, WebUntisLib.TYPES.CLASS);
    })
    .then(console.log)
    .catch(console.trace);

You can not use the getOwn() functions with the anonymous login. However, you can use everything else if you have the correct permissions. You need to provide the element id and the element type like in my example. For this I added similar functions like the getOwn() functions, but with the id and type as additional argument. You can use for the type id WebUntisLib.TYPES.*

7RST1 commented 4 years ago

Seems to be working now. Thank you!