Ad

Microsoft Graph API With Simple-OAuth2 And Client Credentials Flow

- 1 answer

I'm trying to log users with Client Credentials Flow with Simple-OAuth2 in a NodeJS website.

My routes/index.js is this:

var express = require('express');
var router = express.Router();
var authHelper = require('../helpers/auth');

router.get('/', async function(req, res, next) {
  let parms = { title: 'Home', active: { home: true } };
  const accessToken = await authHelper.accessToken;
  res.render('index', parms);
});

module.exports = router;

And my auth.js is this:

const credentials = {
  client: {
    id: process.env.APP_ID,
    secret: process.env.APP_PASSWORD,
  },
  auth: {
    tokenHost: 'https://login.microsoftonline.com',
    authorizePath: "common/oauth2/v2.0/authorize",
    tokenPath: "common/oauth2/v2.0/token",
  }
};
const oauth2 = require('simple-oauth2').create(credentials);

const tokenConfig = {
  username: '[email protected]',
  password: 'ppppppp',
  scope: process.env.APP_SCOPES,
};

try {
  const result = await oauth2.ownerPassword.getToken(tokenConfig);
  const accessToken = oauth2.accessToken.create(result);
} catch (error) {
  console.log('Access Token Error', error.message);
}
exports.accessToken = accessToken;

When I try to start website, nodejs shows me a sintax error:

const result = await oauth2.ownerPassword.getToken(tokenConfig);
               ^^^^^
SyntaxError: await is only valid in async function

This error does not make much sense to me since the code is provided by simple-oauth2.

Could someone shed light on my actual error?

Ad

Answer

Well you have to wrap your code into async function so you could use await key word in that function. You cna find more info here.

In your case I would wrap code into function and export that function like this:

const credentials = {
  client: {
    id: process.env.APP_ID,
    secret: process.env.APP_PASSWORD,
  },
  auth: {
    tokenHost: 'https://login.microsoftonline.com',
    authorizePath: "common/oauth2/v2.0/authorize",
    tokenPath: "common/oauth2/v2.0/token",
  }
};
const oauth2 = require('simple-oauth2').create(credentials);

const tokenConfig = {
  username: '[email protected]',
  password: 'ppppppp',
  scope: process.env.APP_SCOPES,
};

const getAccessToken = async () => {
  try {
    const result = await oauth2.ownerPassword.getToken(tokenConfig);
    const accessToken = oauth2.accessToken.create(result);
    return accessToken;
  } catch (error) {
    console.log('Access Token Error', error.message);
    return null;
  }
};

exports.getAccessToken = getAccessToken;

And then you can use that function like this:

var express = require('express');
var router = express.Router();
var authHelper = require('../helpers/auth');

router.get('/', async function(req, res, next) {
  let parms = { title: 'Home', active: { home: true } };
  const accessToken = await authHelper.getAccessToken();
  res.render('index', parms);
});

module.exports = router;
Ad
source: stackoverflow.com
Ad