0

I am building an app which uses Drupal (9.4.8) as the API. I am using the Simple OAuth module to authorise the client. I have got almost everything working fine, I am receiving the Access Token and the Refresh Token as expected. But, after a lot of searching, I still can't find out which path I need to refresh the tokens.

According to this video tokens can be refreshed on the path "/simple-oauth/refresh". But that path doesn't exist and I guess the video refers to an old version of the module...

Can anyone shed any light on this?

3
  • 1
    What is the precise version of the module you are using and can you confirm the module is this one? drupal.org/project/simple_oauth
    – mona lisa
    Commented Oct 20, 2022 at 15:53
  • Yes, that's the module, version 5.2.0
    – dfd
    Commented Oct 21, 2022 at 16:46
  • Ok, I have made some progress. Got it to working on Talend API Tester. By following these instructions here: link. The refresh url should be /oauth/token (same as login). You also need to send the grant_type and client_id.
    – dfd
    Commented Oct 21, 2022 at 17:16

1 Answer 1

2

I have got it working with the following steps:

In the Auth strategy in nuxt.config.js

  • Set the refresh path to /oauth/token (I also added a query string, ?refresh to distinguish out from the login path)
  • Add this to the header 'Content-Type': 'application/x-www-form-urlencoded'

Here's the code:

    refresh: {
     url: '/oauth/token?refresh',
     method: 'post',
     headers: { 'Accept': '*/*','Content-Type': 'application/x-www-form-urlencoded' },
    },

I still had problems sending the body in the x-www-form-urlencoded format, so I then created an Axios interceptor plugin to alter the request. This plugin (~/plugins/axios.js) intercepts request made to /oauth/token?refresh and puts the grant type, the refresh token and the client id in the correct format.

Here's the plugin:

var qs = require('qs');

export default function ({ $axios, redirect }) {
  $axios.onRequest(config => {
    if (config.url == '/oauth/token?refresh') {
      const params = new URLSearchParams(config.data);
      const refreshToken params.get('refresh_token');
      config.data = qs.stringify({'grant_type': 'refresh_token', 'refresh_token': refreshToken,'client_id':'xxxxx-your-client-id-XXXX'})
    }
  })
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.