How to obtain a token from WordPress.com API, using Axios, from an Electron/node

  • Unknown's avatar

    I hope you are all well.

    I would like to say that I am not asking questions about WordPress.org but for wordpress.com.

    I have an Electron App, and I want to make a Oauth2 authentication.

    with the wordpress.com api using Axios.

    I am able to obtain “the code”. I need to send it back with some more information to obtain the Token.

    I was mainly using this example of implementation: How to access WordPress authentication token

    This other example look like to be useful also but I am not competent in this language.

    Here is my code for the call to the API. And bellow the part that is not working.

    .then(async (code) => {
            console.log('En dehors de la loop ' + code);
        
            const options={
                grant_type: 'authorization_code',
                code: code,
                client_id: codes.client_idWP,
                client_secret: codes.client_secretWP,
                redirect_uri: codes.urlWP.redirect,
            }
    
            var authOptions ={
                method: "POST",
                url:'https://public-api.wordpress.com/oauth2/token',
                form: options,
                headers: {
    
                    'Authorization': 'Basic ' + (Buffer.from(codes.client_idWP + ':' + codes.client_secretWP).toString('base64'))
                },
                json: true
            }
            await axios
                (authOptions)
                .then(async (response) => {
                    //console.log(response);
                })
                .catch((err) => {
                    //console.log(err);
                });
        });

    I hope you are all well.

    I have an Electron App, and I want to make a Oauth2 authentication.

    with the wordpress.com api using Axios.

    I am able to obtain “the code”. I need to send it back with some more information to obtain the Token.

    I was mainly using this example of implementation: How to access WordPress authentication token

    This other example look like to be useful also but I am not competent in this language. https://since1979.dev/snippet-014-setup-axios-for-the-wordpress-rest-api/

    Here is my code for the call to the API. And bellow the part that is not working.

    .then(async (code) => {
    console.log(‘En dehors de la loop ‘ + code);

    const options={
    grant_type: ‘authorization_code’,
    code: code,
    client_id: codes.client_idWP,
    client_secret: codes.client_secretWP,
    redirect_uri: codes.urlWP.redirect,
    }

    var authOptions ={
    method: “POST”,
    url:’https://public-api.wordpress.com/oauth2/token’,
    form: options,
    headers: {

    ‘Authorization’: ‘Basic ‘ + (Buffer.from(codes.client_idWP + ‘:’ + codes.client_secretWP).toString(‘base64’))
    },
    json: true
    }
    await axios
    (authOptions)
    .then(async (response) => {
    //console.log(response);
    })
    .catch((err) => {
    //console.log(err);
    });
    });

    The error message is quite long. You can find it here. It is a
    Error: Request failed with status code 400

    You will be extremely nice to help me on finding a solution to this problem.

    The blog I need help with is: (visible only to logged in users)

  • Hi @tsurubaso,

    I suggest checking out our developer resources: https://developer.wordpress.com/

  • Unknown's avatar

    Hello Metabreakr, I hope you are well.
    I am having a look to your doc, for one week, 2 to 3 hour a day.
    With pleasure I will read and read again those docs.
    My main problem is that I am not a born engineer, and I don’t find a previous working example for my configuration.
    I know that you have an Official app using Electron.
    May be a reason why no body wants to try Electron with WordPress.com, and why there is so few tutorial.
    There is also difficulties in the naming, but I am sure you are aware of that.
    I also post in stackoverflow.com on Specialzed Electron Discord.
    I am patient.
    Then I will look at the doc again and will report here.

  • Hi there!

    I’m afraid this is out of the scope of our support here, so we won’t be able to assist you with further questions about this.

  • The topic ‘How to obtain a token from WordPress.com API, using Axios, from an Electron/node’ is closed to new replies.