Latest

Thursday, July 6, 2017

How to fix a 401 error in my QtApplication?

Asked by: John Smith


Since yesterday, we have a strange 401 error like that (see the log) :

LOG:E:12:01:26.699: AuthenticationManager QNetworkReply::Error (401) : Error transferring https://mysoftwarecompagny.com/oauth/token - server replied: Internal Server Error LOG:E:12:01:26.699: LoginWindow(step 1 / 5) : auth failed (401)

The code stops to work since yesterday. We use laravel on the website. We don't understand why we have this problem. The client secret code doesn't be changed in the database.

We have two codes : "Personnal Access Client" and "Password Grant Client" stored in oauth_clients db. We use only "Password Grant Client" as AUTHENTICATION_CLIENT_SECRET_PROD

This is the code. It uses QtNetwork.

void AuthenticationManager::Connect(const QString & username, const QString & password, int timeoutMS)
{
    m_username = username;
    m_has_reply_finished = false;

    QUrlQuery query;

    query.addQueryItem("grant_type", "post");
    query.addQueryItem("client_id", "2");
    query.addQueryItem("client_secret", AUTHENTICATION_CLIENT_SECRET_PROD);
    query.addQueryItem("username",username);
    query.addQueryItem("password", password);
    query.addQueryItem("scope", "");

    QUrl url(AUTH_URL);
    QString params = query.query(QUrl::FullyEncoded);

    // request
    QByteArray post_data;

    // convert Unicode to 8-bit char
    post_data.append(params);
    QNetworkRequest request= QNetworkRequest(QUrl(url));

    // request
    request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
    QTimer::singleShot(timeoutMS, this, SLOT(OnTimeOut())); // timeout => error

    m_manager.post(request,post_data);
}

void LoginWindow::OnAuthFailed(QNetworkReply::NetworkError error)
{
    LogManager* logmgr = LogManager::GetInstance();
    TextManager* textmgr = TextManager::GetInstance();

    if (error == QNetworkReply::AuthenticationRequiredError)
    {
        logmgr->Log(LOG_LEVEL_ERROR, "LoginWindow(step 1 / 5) : auth failed (invalid credential)");
        ui->infoLabel->setText(textmgr->GetText(TEXT_ID_LOGIN_WRONG_USERNAME_PASSWORD));
    }
    else
    {
        logmgr->Log(LOG_LEVEL_ERROR, QString("LoginWindow(step 1 / 5) : auth failed (") + QString::number((int)error) + ")");
        ui->infoLabel->setText(textmgr->GetText(TEXT_ID_LOGIN_AUTH_NETWORK_ERROR) + " (" + QString::number(error) + ")");
    }
}


Source

No comments:

Post a Comment

Adbox