private/RequestHandlerPrivate.cpp
/** ************************************************************************************************
    * @brief        Refresh the token to access the API.
    *
    * @details      QOAuth2AuthorizationCodeFlow::refreshAccessToken() doesn't work with Spotify.
    *               The header "Authorization" value is incorrect. Until a way to override it is found,
    *               a custom request is sent.
    *               The query is the step 4 of the authorization flow documentation.
    *               https://developer.spotify.com/documentation/general/guides/authorization-guide/#authorization-code-flow
    ***************************************************************************************************/
@brief        Refresh the token to access the API.
@brief Refresh the token to access the API .
void RequestHandlerPrivate::refreshToken()
    {
        QUrl url(m_authManager->accessTokenUrl());
        QUrlQuery query;
        query.addQueryItem("grant_type", "refresh_token");
        query.addQueryItem("refresh_token", m_authManager->refreshToken());
        url.setQuery(query);
        QNetworkRequest request(url);
        QString authorization("Basic %1");
        QByteArray code;
        code.append(m_authManager->clientIdentifier() + ":" +  m_authManager->clientIdentifierSharedKey());
        request.setRawHeader("Authorization", QByteArray("Basic ") + code.toBase64());
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
        auto reply = m_networkAccessManager->post(request, QByteArray());
        connect(reply, &QNetworkReply::finished, this, &RequestHandlerPrivate::onRefreshTokenReplyReceived);
    }
void RequestHandlerPrivate :: refreshToken ( ) { QUrl url ( m_authManager -> accessTokenUrl ( ) ) ; QUrlQuery query ; query . addQueryItem ( " " , " " ) ; query . addQueryItem ( " " , m_authManager -> refreshToken ( ) ) ; url . setQuery ( query ) ; QNetworkRequest request ( url ) ; QString authorization ( " " ) ; QByteArray code ; code . append ( m_authManager -> clientIdentifier ( ) + " " + m_authManager -> clientIdentifierSharedKey ( ) ) ; request . setRawHeader ( " " , QByteArray ( " " ) + code . toBase64 ( ) ) ; request . setHeader ( QNetworkRequest :: ContentTypeHeader , " " ) ; auto reply = m_networkAccessManager -> post ( request , QByteArray ( ) ) ; connect ( reply , & QNetworkReply :: finished , this , & RequestHandlerPrivate :: onRefreshTokenReplyReceived ) ; }
@brief        Refresh the token to access the API.
@brief Refresh the token to access the API .
// Construct the URL. // Add query parameters // Create the request. // The "Authorization" header is  a base64 encoding of the client ID and secret.