To get the access token in Node.js, you can use the 'request' module to make a POST request to the Azure AD OAuth2 token endpoint. Here's an example code snippet:

const request = require('request');

const tenantId = '<your-tenant-id>';
const clientId = '<your-client-id>';
const clientSecret = '<your-client-secret>';
const redirectUri = '<your-redirect-uri>';
const code = '<the-code-you-received>';

const tokenEndpoint = `https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token`;

const options = {
    method: 'POST',
    uri: tokenEndpoint,
    form: {
        grant_type: 'authorization_code',
        client_id: clientId,
        client_secret: clientSecret,
        redirect_uri: redirectUri,
        code: code
    },
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
};

request(options, (error, response, body) => {
    if (error) {
        console.error(error);
    } else {
        const accessToken = JSON.parse(body).access_token;
        console.log(accessToken);
    }
});

Replace the '', '', '', '', and '' placeholders with the corresponding values for your application.

This code sends a POST request to the token endpoint with the authorization code and other required parameters, and receives a JSON response that contains the access token. The access token is extracted from the response and printed to the console. You can use the access token to make API calls to Office 365 services.

Get Access Token in Office 365 with Node.js - OAuth2 Flow

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

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