question

LemonJuicer96 avatar image
0 Votes"
LemonJuicer96 asked LemonJuicer96 answered

AAD can't use xbox live api

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.


azure-ad-authentication
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @LemonJuicer96 , are you following any documents for this? What is the exact API that you're using?

Thanks,
James

0 Votes 0 ·

@JamesHamil-MSFT Forgot to mention you.

0 Votes 0 ·

Hi @LemonJuicer96 , when you say you can't add it "Request API Permissions" tab do you mean it's not there or it just doesn't work? You said it works when you use user.read. What other permissions are you trying to access?

0 Votes 0 ·
Show more comments

1 Answer

LemonJuicer96 avatar image
0 Votes"
LemonJuicer96 answered

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();
 });
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.