3

We are trying to link our website to Wordpresses API using OAuth 2.0. Hoping that a client can authenticate and post to WordPress from our site. We need to receive an access token to do this.

We have successfully connected with Wordpress to receive our access code. We've followed the Wordpress api, and have it working for a single user (with secret key not with OAuth). Things we have tried are adding a headers, changing data to different names examples: params, body

This is a simplified version of the code we have been using

const axios = require('axios');
axios({
   method: "POST",
   data: {
       grant_type: 'authorization_code',
       client_id: '12345',
       client_secret: 'ABCABC1235412345',
       code: 'Abc123',
       redirect_uri: 'https://localhost:5000/wordpress/callback_wordpress'
   },
   url: 'https://public-api.wordpress.com/oauth2/token'
}).then( (response) => {
   console.log(response);
}).catch( (error) => {
   console.log(error);
});  

We expect to receive a jwt access token, but instead are getting this 400 error:

data:
    { error: ‘invalid_client’,
      error_description: ‘The required “client_id” parameter is missing.’ } } }

It seems clear that we are missing the client_id, however we have it included in our request. Is there somewhere else we need to include it?

1
  • If anyone in the future faces the same issue, just set "Content-Type": "application/x-www-form-urlencoded" header in your post request. Commented Jan 1, 2024 at 10:53

1 Answer 1

1
 var authOptions = {
        url: 'https://public-api.wordpress.com/oauth2/token',

        form: 
        {
          grant_type: 'authorization_code',
          code: code,
          client_id: client_id,
          client_secret: client_secret,
          redirect_uri: redirect_uri,
        },
        headers: {
          'Authorization': 'Basic ' + (Buffer.from(client_id + ':' + client_secret).toString('base64'))
      },

        json: true
      };

We needed to include a header with that information, and we needed to use the variable 'form' instead of 'data'.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.