Search code examples
javascriptoauthkhan-academy

Javascript Khan Academy OAuth can't override default_callback


I've got this express app going;

const oa = new OAuth(
  'https://www.khanacademy.org/api/auth2/request_token',
  'https://www.khanacademy.org/api/auth2/access_token',
  process.env.CONSUMER_KEY,
  process.env.CONSUMER_SECRET,
  '1.0',
  'http://localhost:3000/oauth/callback',
  'HMAC-SHA1',
  64
)

app.get('/oauth', (req, res) => {
  oa.getOAuthRequestToken(function requestTokenCallback(err, token, token_secret, results) {

    if (err) {
      console.log('requestTokenCallback', err)
      return
    }

    req.session.oauth = req.session.oauth || {}
    req.session.oauth.token = token
    req.session.oauth.token_secret = token_secret
    res.redirect(`https://www.khanacademy.org/api/auth2/authorize?oauth_token=${token}`)
    // I've tried res.redirect(`https://www.khanacademy.org/api/auth2/authorize?oauth_token=${token}&oauth_callback=${encodeURIComponent('http://localhost:3000/oauth/callback')}`)
  })
})

However, clicking accept once the user is redirected does not redirect them to my callback, 'http://localhost:3000/oauth/callback'.

This fellow seems to be having the same issue: oauth_callback for Khan Academy API not woring. However, I followed the instructions of the answer and redirected with an oauth_callback param, with no avail.

I'm super new to oauth in general, but I don't believe that this is my fault. I just need some sort of workaround.


Solution

  • The khan npm package https://www.npmjs.com/package/khan allows for the oauth_callback to be specified when calling .requestToken(). For example:

    khan.requestToken('www.myapp.com/my_callback')
    

    Passing in an oauth_callback parameter into the .requestToken() function ensures that the user being authenticated will be redirected to the specified site. This is a much lovelier solution than using the node-oauth due to the curried functionality, promises, and expressive semantics.