AAD can't use xbox live api

Anonymous
2021-08-25T18:42:09.057+00:00

I wanted to get Xbox user data but I cant enable it in "Request API Permissions" tab. When I try to use the link I get error in console, but it works when I use "user.read"

Edit:
Xbox works when using code similar to example on module's github page(https://github.com/dommilosz/minecraft-auth)
Code:

const fs = require('fs');
const minecraftAuth = require('minecraft-auth');
const prompt = require('prompt');
require('dotenv').config();

const account = new minecraftAuth.microsoftAccount();

async function authenticate() {
    try {
        const clientID = process.env.CLIENT_ID;
        const clientSecret = process.env.CLIENT_SECRET;
        const redirectURI = process.env.REDIRECT_URI;
        minecraftAuth.MicrosoftAuth.setup(clientID, clientSecret, redirectURI);
        console.log(minecraftAuth.MicrosoftAuth.createUrl());
        prompt.start();
        const result = await prompt.get(['code']);
        console.log('Command-line input received:');
        console.log('  code: ' + result.code);
        await account.authFlow(result.code);
    } catch (e) {
        console.error(e);
    }
}

(async () => {
    await authenticate();
    // console.log(account.accessToken);
    // await account.getProfile();
    // console.log(account.username);
    // console.log(account.uuid);
    // console.log(account.ownership);
    // console.log(account.profile);
    // console.log(account.profile.skins[0].url);

    // Same thing but with indents
    class accountsStorage extends minecraftAuth.accountsStorage {
        serialize() {
            return JSON.stringify(this.accountList, null, 4);
        }
    }

    const accounts = new accountsStorage(); // minecraftAuth.accountsStorage();

    accounts.addAccount(account);
    const data = accounts.serialize(accounts);

    fs.writeFile('accounts.json', data, err => {
        if (err) throw err;
        console.log('Data written to file');
    });
})();

but doesnt when trying to use with express web server
Code:

const express = require('express');
// const fs = require('fs');
const minecraftAuth = require('minecraft-auth');
require('dotenv').config();

const SERVER_PORT = process.env.PORT || 3000;
const app = express();

app.get('/', async (req, res) => {
    res.setHeader('Content-type', 'text/html');
    res.send(`<a href="${await getURL()}">Sign up</a>`);
});

app.get('/auth-response', async (req, res) => {
    const code = req.query.code;
    console.log(code);
    await getToken(code);
    await account.getProfile;
    res.send(account.username);
});

app.listen(SERVER_PORT, () =>
    console.log(`App listening on port ${SERVER_PORT}!`),
);

async function getURL() {
    try {
        const clientID = process.env.CLIENT_ID;
        const clientSecret = process.env.CLIENT_SECRET;
        const redirectURI = process.env.REDIRECT_URI;
        minecraftAuth.MicrosoftAuth.setup(clientID, clientSecret, redirectURI);
        return minecraftAuth.MicrosoftAuth.createUrl();
    } catch (e) {
        console.error(e);
    }
}

const account = new minecraftAuth.microsoftAccount();

async function getToken(code) {
    try {
        await account.authFlow(code);
    } catch (e) {
        console.error(e);
    }
}

Error I am getting, which is obviously not true:

invalid_client: The client does not exist or is not enabled for consumers. If you are the application developer, configure a new application through the App Registrations in the Azure Portal at https://go.microsoft.com/fwlink/?linkid=2083908.
Microsoft Security | Microsoft Entra | Microsoft Entra ID
{count} votes

Accepted answer
  1. Anonymous
    2021-08-29T13:44:14.86+00:00

    I solved the issue by putting routes inside auth function. Here is the code:
    const express = require('express');
    const minecraftAuth = require('minecraft-auth');
    require('dotenv').config();

    const SERVER_PORT = process.env.PORT || 3000;
    const app = express();
    
    const account = new minecraftAuth.microsoftAccount();
    
    async function authenticate() {
        try {
            const clientID = process.env.CLIENT_ID;
            const clientSecret = process.env.CLIENT_SECRET;
            const redirectURI = process.env.REDIRECT_URI;
            minecraftAuth.MicrosoftAuth.setup(clientID, clientSecret, redirectURI);
            const link = minecraftAuth.MicrosoftAuth.createUrl();
            app.get('/', async (req, res) => {
                res.setHeader('Content-type', 'text/html');
                res.send(`<a href=${link}>sing up</a>`);
            });
            app.get('/auth-response', async (req, res) => {
                const result = req.query;
                console.log('Code: ' + result.code);
                await account.authFlow(result.code);
                await account.getProfile();
                res.send(account.username);
            });
        } catch (e) {
            console.error(e);
        }
    }
    
    app.listen(SERVER_PORT, async () => {
        console.log(`App listening on port ${SERVER_PORT}!`);
        await authenticate();
    });
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.