WordPress API Encounters a 400 Error with OAuth

While experimenting with the WordPress Rest API, I decided to download a plugin called WP OAuth Server by Justin Greer and managed to set up my own OAuth connection.

However, I encountered an issue where I received Error 400 indicating that

The grant type was not specified in the request
.

Below is the code I have been working on:

HTML:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="assets/css/app.css" type="text/css">
</head>
<body>
<div id="result"></div>
<!--<script src="https://maps.googleapis.com/maps/api/js?v=3&sensor=false&libraries=places"></script>-->
<script type="text/javascript" src="assets/js/app.js"></script>
</body>
</html>

JavaScript:

/**
 * OAUTH 2 LOGIN
 */

+function () {
    var $CLIENT_CODE = 'fc8uhbwlo4niqhngrjsdl3tbp3cndpidfs61w77g';
    var $CLIENT_ID = 'llsfdwZzO7qVHBzM4nhfcq1jFW2L8O';
    var $CLIENT_SECRET = 'auaCKn8JWXQmSyYrl3PDi23klIhotp';

    var $AUTHORIZATION_ENDPOINT = 'http://domain.dev/oauth/authorize';
    var $TOKEN_ENDPOINT = 'http://domain.dev/oauth/token';


    $.ajax({
        url: $AUTHORIZATION_ENDPOINT + '?client_id=' + $CLIENT_ID + '&client_secret=' + $CLIENT_SECRET + '&response_type=code',
    }).done(function (url) {
        $('#result').html(url);
        fetchSomething();
    }).fail(function (errorThrown) {
        console.log("Error" > errorThrown.responseText);
    })


    function fetchSomething() {
        $.ajax({
            type: 'POST',
            url: $TOKEN_ENDPOINT + '?grant_type=authorization_code&code=' + $CLIENT_CODE,
        }).done(function (success) {
            console.log(success);
        }).fail(function (error) {
            console.log(error);
        });
    }
}();

CodePen

Answer №1

Here is the approach I took:

$.ajax({
    url: 'https://' + customDomain + '?auth=token',
    type: 'POST',
    data: {
        grant_type: accessType,
        code: userCode,
        client_id: myClientID,
        client_secret: myClientSecret,
        redirect_uri: myRedirectURL
    }
}).done(function (result) {
    console.log(result);
}).fail(function (errorResult) {
    console.log(errorResult.responseJSON.error);
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Dealing with the "error" parameter in JSON in Angular and Ionic

I am currently working with TypeScript and Angular 5.0. I have defined the structure of my backend response in the following interface: export interface JSONResponse { error?: { code: number, message: string }; data?: {}; } The method in my ...

Error in uploading file using Ajax

This document file <form name="classupload" method="post" enctype="multipart/form-data" action=""> <h3>Choose images:&nbsp;</h3><br /> <input type="file" name="Filedata[]" style="margin-top:2px;"/><br /&g ...

"Implementing automated default values for Select/dropdown lists in ReactJs, with the added capability to manually revert back to the default selection

After browsing multiple websites, I couldn't find a clear solution on how to both set and select a default value in a select element. Most resources only explain how to set the value, without addressing how to reselect the default value. My Requireme ...

Managing the orientation of the print grid panel in ext.net

I have a grid panel on my webpage with a toolbar that includes a print button to print the content of the grid view. The layout direction of the grid is set from Right to Left (RTL) to accommodate Arabic text. However, when I click on the print button, th ...

Customizing product prices with unique icons

I have successfully integrated a 'Latest Products' row on the Homepage of my WooCommerce enabled WordPress Website. Now, I am looking to enhance it by adding an icon on each side of the price entry. Although I know how to manually insert <i c ...

Guide to creating a contenteditable div that automatically generates smart quotes instead of traditional dumb quotes

I've observed that when I write in Google Docs, I get smart quotes. However, when I create contenteditable divs on my own, I only see dumb quotes. Is there a way to make my contenteditable divs display smart quotes instead of dumb quotes? ...

Typeahead autocomplete feature in Angular Material

Could you please guide me on how to incorporate autocomplete suggestions in material autocomplete for AngularJS using typeahead? Here's an example format I would like to achieve: Animals: Lion, Tiger Birds: Eagle, Dove Similar to the functionality s ...

When a div is covered by an if statement

One of the challenges I am facing is managing a view with multiple projects, some of which are active while others are not. In my function, if a project's deadline has passed, it displays '0' days left on the view, indicating that these are ...

Is there a problem with evalJSON() causing Simple AJAX JS to work on Safari locally but fail on the server and in Firefox?

I created a script that utilized Prototype's AJAX methods to fetch Twitter data in JSON format, process it, and display it within a div. The script was functioning perfectly during testing on Safari 4.0.3 on an OS 10.6.1 machine. However, when I uploa ...

No activity was detected on the UmbracoApiController controller

I am attempting to retrieve data from an Umbraco Api class and send it to a javascript function. Any assistance in resolving the following errors would be greatly appreciated. {,...} Message : "No HTTP resource was found that matches the request URI &apos ...

Unexpected Outcome Arises When Applying Lodash's Debounce Function to Axios API Call

I keep encountering an issue with my app where it updates every time text is typed. I've implemented debounce on an axios request to queue them up until the timer runs out, then they all go at once. But I want to limit the requests to one per 10-secon ...

Leveraging PHP variables to determine the image location, implement an onclick function to cycle through the image gallery

I am facing an issue with a database query that is supposed to display multiple images. These images are stored in a PHP variable and shown one at a time using a JavaScript gallery with an onclick function to navigate through them. Unfortunately, the funct ...

What's the scoop on NodeJS, Express, Nginx, and Jade?

Currently exploring options for technologies and libraries to use in a new, large-scale project. With knowledge of NodeJS, JavaScript, Express, and Jade (now Pug), my team and I are leaning towards adopting these for the project. The main issue we're ...

Transforming PHP Data into the Desired Format

According to the Eventbrite API v3 documentation, it is recommended to submit data in JSON format. I am currently trying to update simple organizer data through an ExtJS grid, but the changes are not being processed. The solution involves MODX and the upd ...

Analyzing and sorting two sets of data in JavaScript

I am currently working with two arrays that are used to configure buttons. The first array dictates the number of buttons and their order: buttonGroups: [ 0, 2 ] The second array consists of objects that provide information about each button: buttons = ...

Inject JavaScript Object Information into Bootstrap Modal

After iterating through each object and assigning its data to an individual div along with a button, I encountered an issue. When testing the buttons, I noticed that only the last object's data was displayed in all of the modal windows. Any suggestion ...

Tips for successfully transferring values or parameters within the Bootstrap modal

How can I create a cancel button that triggers a modal asking "Are you sure you want to cancel the task?" and calls a function to make an API call when the user clicks "Ok"? The challenge is each user has a unique ID that needs to be passed to the API for ...

What is the best way to align grid elements vertically instead of horizontally on mobile devices using JQuery Mobile?

I've created a basic JQuery Mobile website with a grid containing four horizontally aligned blocks, each with text and a button. While it displays well on desktop browsers, the blocks appear cramped when viewed on my Android Galaxy phone in landscape ...

What are some alternative methods for implementing WordPress Ajax calls for authenticated users that do not rely on cookies?

As the owner of a WordPress "mothership site," my users are registered there. I am interested in making AJAX API calls from one of my client sites to the WordPress site in order to retrieve results based on the user's login information. For example, ...

Retrieve the IDs of all currently logged in users using express.js

Currently, I am working on a project and need to extract a list of user ids of all currently logged in users. I came across a relevant question on StackOverflow at this link. However, I am facing difficulties in accessing the object properties as intended. ...