To generate a login URL and get the code in response and retrieve access and refresh tokens for Microsoft Office 365 using Node.js, follow these steps:

  1. Install necessary packages:

    You need to install the following packages:

    • express: a web framework for Node.js
    • request: a package to make HTTP requests
    • dotenv: a package to read environment variables from a '.env' file

    Install these packages by running the following command:

npm install express request dotenv


2. **Set up environment variables:**

Create a '.env' file in the root directory of your project and set the following environment variables:

- `CLIENT_ID`: Your Azure AD application's client ID
- `CLIENT_SECRET`: Your Azure AD application's client secret
- `REDIRECT_URI`: Your Azure AD application's redirect URI
- `RESPONSE_TYPE`: The response type for the authorization request (should be 'code')
- `SCOPE`: The list of scopes for the authorization request (e.g., 'openid profile email')
- `AUTHORITY`: The authority URL for your Azure AD tenant (e.g., 'https://login.microsoftonline.com/common')

You can find these values in the Azure portal.

3. **Set up the server:**

Create a file named 'server.js' and add the following code:

```javascript
const express = require('express');
const request = require('request');
const dotenv = require('dotenv');

dotenv.config();

const app = express();

app.get('/', (req, res) => {
const url = `https://login.microsoftonline.com/${process.env.AUTHORITY}/oauth2/v2.0/authorize` +
 `?client_id=${process.env.CLIENT_ID}` +
 `&response_type=${process.env.RESPONSE_TYPE}` +
 `&redirect_uri=${process.env.REDIRECT_URI}` +
 `&scope=${process.env.SCOPE}`;

res.redirect(url);
});

app.get('/callback', (req, res) => {
const code = req.query.code;

const params = {
 client_id: process.env.CLIENT_ID,
 client_secret: process.env.CLIENT_SECRET,
 code: code,
 redirect_uri: process.env.REDIRECT_URI,
 grant_type: 'authorization_code'
};

request.post('https://login.microsoftonline.com/common/oauth2/v2.0/token', { form: params }, (err, resp, body) => {
 const result = JSON.parse(body);

 const access_token = result.access_token;
 const refresh_token = result.refresh_token;

 res.send(`Access token: ${access_token}<br>Refresh token: ${refresh_token}`);
});
});

app.listen(3000, () => {
console.log('Server started on port 3000');
});

This sets up a simple Express server that redirects the user to the Microsoft login page when accessing the root URL ('/'). After the user logs in and grants permissions, they are redirected back to the server at the '/callback' URL. The server then uses the code from the query parameters to request access and refresh tokens from the Microsoft token endpoint.

  1. Start the server:

    Start the server by running the following command:

node server.js


This will start the server on port 3000.

5. **Test the login flow:**

Open a web browser and go to `http://localhost:3000`. You should be redirected to the Microsoft login page. After logging in and granting permissions, you will be redirected back to the server and see the access and refresh tokens printed on the screen.

**Note:** This is a sample implementation and should not be used in production without proper security measures in place.
Node.js: Generate Login URL & Retrieve Access/Refresh Tokens for Microsoft Office 365

原文地址: https://www.cveoy.top/t/topic/lVSc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录