Failed to obtain access token error when using passport-apple

237 Views Asked by At

I am a junior engineer, trying to develop apple login using passport-apple and Node.js.

The problem is as it is listed in the title, I am keep getting this error message: {"name":"InternalOAuthError","message":"Failed to obtain access token","oauthError":{}}

There are some situations here.

  1. it seems like the function in apple strategy is not working
new AppleStrategy(
      {
        clientID: APPLE_AUTH.CLIENT_ID,
        teamID: APPLE_AUTH.TEAM_ID,
        callbackURL: APPLE_AUTH.LOGIN_CALLBACK_URL,
        keyID: APPLE_AUTH.KEY_ID,
        privateKeyLocation: 'config/AuthKey_Y8BG5JY7P3.p8',
        privateKeyString: APPLE_AUTH.privateKey,
        passReqToCallback: true,
      },
      function (accessToken, refreshToken, idToken, profile, cb) {
        try {
          // console.log(profile._json.email);
          // const socialLoginEmail = req.user.emails[0].value;
          // console.log(email);
          //화면에서 백으로
          // console.log(profile._json.email);
          // const user = await UserModelService.getUser(profile._json.email);
          // console.log(user);
          const idTokenDecoded = jwt.decode(idToken);
          // console.log('strategy', req);
          cb(null, idTokenDecoded);
        } catch (error) {
          console.error(error);
          // done(error);
        }
      },
    ),
  );

-> I could not see anything printed in the log

  1. However, things are printed in the log, in the callback function, which is very strange
ROUTER.post('/apple/callback', function (req, res, next) {
  passport.authenticate('apple', function (err, user, info) {
    console.log('strategy', req);
    console.log('res 받기', res);
    console.log('user받기', user);
    console.log('info 받기', info);
    if (err) {
      if (err == 'AuthorizationError') {
        res.send(
          'Oops! Looks like you didn\'t allow the app to proceed. Please sign in again! <br /> \
                <a href="/login">Sign in with Apple</a>',
        );
      } else if (err == 'TokenError') {
        res.send(
          'Oops! Couldn\'t get a valid token from Apple\'s servers! <br /> \
                <a href="/login">Sign in with Apple</a>',
        );
      } else {
        res.send(err);
      }
    } else {
      if (req.body.user) {
        // Get the profile info (name and email) if the person is registering
        res.json({
          user: req.body.user,
          idToken: user,
        });
      } else {
        res.json(user);
      }
    }
  })(req, res, next);
});

I am struggling with this issue for the past 4 days, and I hope I could finish the work today. Thanks in advance!

1

There are 1 best solutions below

0
Mentalist On

First argument seems to be request if passReqToCallback: true

function(req, accessToken, refreshToken, idToken, profile, cb) {}