简体   繁体   English

带有Simple-OAuth2和客户端凭据流的Microsoft Graph API

[英]Microsoft Graph API with Simple-OAuth2 and Client Credentials Flow

I'm trying to log users with Client Credentials Flow with Simple-OAuth2 in a NodeJS website. 我正在尝试使用NodeJS网站中的Simple-OAuth2使用Client Credentials Flow记录用户。

My routes/index.js is this: 我的routes / index.js是这样的:

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: 我的auth.js是这样的:

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: 'uuuuuu@dddddd.com',
  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: 当我尝试启动网站时,nodejs向我显示了一个sintax错误:

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. 这个错误对我来说没有多大意义,因为代码是由simple-oauth2提供的。

Could someone shed light on my actual error? 有人可以解释我的实际错误吗?

Well you have to wrap your code into async function so you could use await key word in that function. 那么你必须将你的代码包装成async函数,这样你就可以在该函数中使用await关键字。 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: 'uuuuuu@dddddd.com',
  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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM