Getting status of 400 - {"error":"invalid_grant"} On Authentication

Viewed 327

I created my own website (www.luig.us). I create a basic IDE for SQL server to teach kids SQL, DML only.

It requires the user to enter an username and password at the beginning to get an token to use the service.

On local, everything works fine, but when I put it on my site, hosted by go-daddy, I will get the same message every time and I don't have a clue why.

{"error":"invalid_grant"}

This is my java script code - look for Authorize. Any help is appreciated. I followed this article closely https://www.c-sharpcorner.com/UploadFile/736ca4/token-based-authentication-in-web-api-2/



function Authorize() {
    let bearer = "Basic Q01JUzMwOERTMzA4OndlYkFQSQ=="
    var email = document.getElementById("email").value;
    var password = document.getElementById("password").value;
    var userNameAndPasswordFilled = (email != "" && password != "");
    if (!userNameAndPasswordFilled) {
        $("#formError").show('fast');
    }
    else {
        var bodyOfRequest = {
            grant_type: "password",
            username: email,
            password: password
        };
        $.ajax({
            type: "POST",
            url: uriManager.access,
            beforeSend: function (request) {
                request.setRequestHeader("Authorization", bearer);
            },
            data: bodyOfRequest,
            success: function (response) {
                sessionStorage.setItem(programKeyWords.BearerToken, response.access_token);
                $('#LogonModal').modal('hide');
            },
            error: function (response) {
                $("#formError").show('fast');
            }
        });
    }
}


2 Answers

I can't say much without more information but first thing I would check is if you actually have users in your database (or whatever oauth you're using). The error you're getting can likely be caused if you don't actually have a user with that username and password in your oauth database.

The endpoint you're using for uriManager.access is where you have to check if that user actually exists.

The problem wasn't with the code. The problem was with:

  1. The connection string. It was not correct.
  2. I was encrypting the username and password and it wasn't matching correctly because of special characters

What I did to fix it.

  1. fixed the connection string
  2. fixed the algorithm to store a hash instead of string.
Related