lelylan / simple-oauth2

A simple Node.js client library for Oauth2
http://lelylan.github.com/simple-oauth2/
1.62k stars 292 forks source link

Authentication failed for microsoft account. #391

Closed azamsolix closed 2 years ago

azamsolix commented 2 years ago

Context

How can we help?

Hi sir, I am getting access token and refresh token both and by using access token i am trying to connect to IMAP but it is returning authentication error.I don't know what i am doing wrong.Below is the complete source code

const app = require('express')();
var cors = require('cors');
var bodyParser = require('body-parser');
app.use(cors());
app.use(bodyParser.json());

const { ClientCredentials, ResourceOwnerPassword, AuthorizationCode } = require('simple-oauth2');
const { Base64Encode } = require('base64-stream');
app.get('/callback', async function (req, res) {
     let credentials = {
  client: {
     id: "id",
      secret: "secret",   
  },
    auth: {
        tokenHost: 'https://login.live.com',
        tokenPath: '/oauth20_token.srf',
        authorizePath: '/oauth20_authorize.srf',
      }
    };
    const client = new AuthorizationCode(credentials);
    console.log('code', req.query);
     const tokenParams = {
    code: req.query.code,
    redirect_uri: 'http://localhost:3000/callback',
    scope:[
    'openid',
    'offline_access',
        'https://graph.microsoft.com/mail.read',
    "https://graph.microsoft.com/IMAP.AccessAsUser.All"
  ],
  };

  try {
      const accessToken = await client.getToken(tokenParams);
      console.log('accesstoken', accessToken, accessToken.token.access_token);
            let token=Buffer.from("user=email2021@hotmail.com" + "\x01auth=Bearer " + accessToken.token.access_token + "\x01\x01").toString('base64');
      console.log('ans', token)
      let Imap = require('imap');

             var imap = new Imap({
            xoauth2 :token,
        host: 'outlook.office365.com',
        port: 993,
        tls: true,
        authTimeout: 3000,
    tlsOptions: { rejectUnauthorized: false }
});
imap.connect();
imap.once('ready', function() {

    //   }
    });     
   imap.once('end', function() {
            console.log('Connection ended');
            // res.redirect('/name');
});          
  } catch (error) {
    console.log('Access Token Error', error);
  }
});
app.get('/auth', async function (req, res) {

    let credentials = {
  client: {
     id: "id",
      secret: "secret",   
  },
    auth: {
        tokenHost: 'https://login.live.com',
        tokenPath: '/oauth20_token.srf',
        authorizePath: '/oauth20_authorize.srf',
      },
      options: {
      authorizationMethod: 'body',
    }
    };
    const client = new AuthorizationCode(credentials);

   const authorizationUri = client.authorizeURL({
    redirect_uri: 'http://localhost:3000/callback',
    scope:  [
    'openid',
    'offline_access',
        'https://graph.microsoft.com/mail.read',
        'profile',
    "https://graph.microsoft.com/IMAP.AccessAsUser.All"
  ],
    state: 'azam'
  });
    console.log('aaaa,', authorizationUri);
    res.redirect(authorizationUri)
})

app.listen(3000, function() {
  console.log(`Express server started on  http://localhost:3000`);
});

Thanks Azam

jonathansamines commented 2 years ago

Hey @azamsolix What error are you getting back? Have you looked at this? You may find something useful there to complete your integration.

azamsolix commented 2 years ago

Sir below is the error i am getting.

Error: AUTHENTICATE failed.

I am getting both access token and refresh token and then i am encoding access token to base64 and passing it to imap npm and it is returning authentication failed.

In below link it is asking to add outlook.microsoft.com scope but that scope is depreceted and it is not available.

https://docs.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth#get-an-access-token Thanks for helping me Azam ahmed