To get the access token in Node.js for Office 365, you can use the 'request' module to make a POST request to the Microsoft authentication endpoint. Here is an example code snippet:

const request = require('request');

const clientId = 'your_client_id';
const clientSecret = 'your_client_secret';
const redirectUri = 'your_redirect_uri';
const code = 'the_code_you_received';

const authOptions = {
  url: 'https://login.microsoftonline.com/common/oauth2/v2.0/token',
  method: 'POST',
  headers: { 'content-type': 'application/x-www-form-urlencoded' },
  form: {
    grant_type: 'authorization_code',
    client_id: clientId,
    client_secret: clientSecret,
    redirect_uri: redirectUri,
    code: code,
  },
};

request(authOptions, (error, response, body) => {
  if (!error && response.statusCode === 200) {
    const accessToken = JSON.parse(body).access_token;
    console.log(`Access token: ${accessToken}`);
  } else {
    console.error(`Failed to get access token: ${error || body}`);
  }
});

In this example, you need to replace the placeholders 'your_client_id', 'your_client_secret', 'your_redirect_uri', and 'the_code_you_received' with your actual values.

The 'authOptions' object contains the necessary parameters for the POST request, including the grant type, client ID and secret, redirect URI, and authorization code.

When the request succeeds, the 'access_token' field in the response body contains the access token you need to authenticate your requests to the Office 365 API.

Get Access Token in Office 365 Using Node.js and the Microsoft Authentication Endpoint

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

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