c++ - {"error":"invalid_client"} 尝试刷新 token 时来自 Spotify Web Api

标签 c++ qt spotify

我在 Qt 和 C++ 方面都是新手,但我试图在 uni 项目中使用 spotify web api。不幸的是,我一直坚持将我的刷新 token 发布到 api 以获取新的访问 token 。每次我这样做时,我都会收到响应 {"error":"invalid_client"}。我已经确保我的客户 ID 和客户密码都是正确的。我认为我的标题某处有错误,但我尝试了几件事,但效果不大。

这是我的代码:

void MainWindow::on_pushButton_2_clicked(){

    QNetworkAccessManager * manager = new QNetworkAccessManager(this);

    QUrl url("https://accounts.spotify.com/api/token");
    QNetworkRequest request(url);
    QString header = "my_client_id:my_client_secret";
    QByteArray ba;
    ba.append(header);
    ui->teOutput->appendPlainText(ba.toBase64());
    QString full_header= "Authorization: Basic " + ba.toBase64();
    //ui->teOutput->appendPlainText(full_header);
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    request.setHeader(QNetworkRequest::ContentDispositionHeader, full_header);

    QUrlQuery params;
    params.addQueryItem("grant_type", "refresh_token");
    params.addQueryItem("refresh_token", "here_is_my_refresh_token");

    connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(onFinish(QNetworkReply *)));
    manager->post(request, params.query().toUtf8());
    //ui->teOutput->appendPlainText(params.query().toUtf8());
}

void MainWindow::onFinish(QNetworkReply *rep)
{
    QString data = rep->readAll();
    ui->teOutput->appendPlainText(data);
}

这就是我的请求的样子(正如我所建议的那样,我已经使用 cUrl 进行了检查)
curl -X "POST" -H "Authorization: Basic xxxxxxxxxxx" -d grant_type=refresh_token -d refresh_token=xxxxxxxxxxxxx https://accounts.spotify.com/api/token

最佳答案

命令-H "Authorization: Basic xxxxxxxxxxx"相当于:

request.setRawHeader("Authorization", "Basic xxxxxxxxxxx"); 

所以你应该使用而不是 request.setHeader(QNetworkRequest::ContentDispositionHeader, full_header); .

虽然没有必要实现所有逻辑,因为 Spotify API 使用 Qt 在 Qt Network Authorization 中实现的 OAuth2模块。

在请求或刷新 token 时,只应修改 QNetworkRequest header ,因为这不是 Oauth2 标准的一部分。

常量.h

#ifndef CONSTANTS_H
#define CONSTANTS_H

#include <QByteArray>
#include <QUrl>

namespace Constants {
    const QByteArray SPOTIFY_CLIENT_ID = "xxxxxxxx";
    const QByteArray SPOTIFY_CLIENT_SECRET = "yyyyyyyy";

    const QUrl SPOTIFY_AUTHORIZATION_URL = QUrl("https://accounts.spotify.com/authorize");
    const QUrl SPOTIFY_ACCESSTOKEN_URL = QUrl("https://accounts.spotify.com/api/token");
}

#endif // CONSTANTS_H

网络访问管理器.h

#ifndef NETWORKACCESSMANAGER_H
#define NETWORKACCESSMANAGER_H

#include <QNetworkAccessManager>

class NetworkAccessManager : public QNetworkAccessManager
{
public:
    using QNetworkAccessManager::QNetworkAccessManager;
protected:
    QNetworkReply *createRequest(Operation op, const QNetworkRequest &request, QIODevice *outgoingData);
};

#endif // NETWORKACCESSMANAGER_H

网络访问管理器.cpp

#include "networkaccessmanager.h"
#include "constants.h"
#include <QtGlobal>

QNetworkReply *NetworkAccessManager::createRequest(QNetworkAccessManager::Operation op,
                                                   const QNetworkRequest &request,
                                                   QIODevice *outgoingData)
{
    QNetworkRequest r(request);
    if(r.url() == Constants::SPOTIFY_ACCESSTOKEN_URL)
        r.setRawHeader("Authorization",
                       "Basic " +
                       QByteArray(Constants::SPOTIFY_CLIENT_ID + ":" + Constants::SPOTIFY_CLIENT_SECRET).toBase64());
    return QNetworkAccessManager::createRequest(op, r, outgoingData);
}

spotifywrapper.h

#ifndef SPOTIFYWRAPPER_H
#define SPOTIFYWRAPPER_H

#include <QOAuth2AuthorizationCodeFlow>
#include <QObject>

class SpotifyWrapper : public QObject
{
    Q_OBJECT
public:
    explicit SpotifyWrapper(QObject *parent = nullptr);
    QNetworkReply *me();
public Q_SLOTS:
    void grant();
Q_SIGNALS:
    void authenticated();
private:
    QOAuth2AuthorizationCodeFlow oauth2;
};

#endif // SPOTIFYWRAPPER_H

spotifywrapper.cpp

#include "spotifywrapper.h"
#include "networkaccessmanager.h"
#include "constants.h"

#include <QDesktopServices>
#include <QOAuthHttpServerReplyHandler>
#include <QUrlQuery>

SpotifyWrapper::SpotifyWrapper(QObject *parent) : QObject(parent)
{
    QOAuthHttpServerReplyHandler * replyHandler = new QOAuthHttpServerReplyHandler(1337, this);
    replyHandler->setCallbackPath("callback");
    oauth2.setNetworkAccessManager(new NetworkAccessManager(this));
    oauth2.setReplyHandler(replyHandler);
    oauth2.setAuthorizationUrl(Constants::SPOTIFY_AUTHORIZATION_URL);
    oauth2.setAccessTokenUrl(Constants::SPOTIFY_ACCESSTOKEN_URL);
    oauth2.setClientIdentifier(Constants::SPOTIFY_CLIENT_ID);
    oauth2.setScope("user-read-private user-read-email");
    oauth2.setState("34fFs29kd09");
    connect(&oauth2, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser,
            &QDesktopServices::openUrl);
    connect(&oauth2, &QOAuth2AuthorizationCodeFlow::statusChanged, [=](
            QAbstractOAuth::Status status) {
        if (status == QAbstractOAuth::Status::Granted){
            emit authenticated();
        }
    });
}

void SpotifyWrapper::grant()
{
    oauth2.grant();
}

QNetworkReply *SpotifyWrapper::me()
{
    return oauth2.get(QUrl("https://api.spotify.com/v1/me"));
}

主.cpp

#include "spotifywrapper.h"

#include <QNetworkReply>
#include <QGuiApplication>
#include <QTimer>

int main(int argc, char *argv[])
{
    QGuiApplication a(argc, argv);
    SpotifyWrapper wrapper;
    wrapper.grant();
    QObject::connect(&wrapper, &SpotifyWrapper::authenticated, [&wrapper](){
        qDebug() << "authenticated";
        QNetworkReply *reply = wrapper.me();
        QObject::connect(reply, &QNetworkReply::finished, [=]() {
                reply->deleteLater();
                if (reply->error() != QNetworkReply::NoError) {
                    qDebug() << reply->errorString();
                    return;
                }
                qDebug() << reply->readAll();
                QTimer::singleShot(1000, &QCoreApplication::quit);
        });
    });
    return a.exec();
}

您还必须在应用程序的 SETTINGS 中将重定向 URI 设置为“http://127.0.0.1:1337/callback”:

enter image description here

完整示例可在 here 中找到

关于c++ - {"error":"invalid_client"} 尝试刷新 token 时来自 Spotify Web Api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59708421/

相关文章:

qt - Doxygen - 继承覆盖信号/槽的文档

c++ - 如何将数组包含到 QT Creator 项目中

ios - 从 Spotify iOS SDK 获取 userId

spotify - libspotify 资源尚未加载

javascript - 无法再访问基本 Spotify API 数据

c++ - C++ 对象(标准定义)是否保留在内存映射文件中?

c++ - 给定一个类对象的三个“稍微”不同的拷贝,我如何有效地存储它们之间的差异?

c++ - QStyledItemDelegate 如何让两个小部件排成一行?

设置对象属性时出现 C++ 内存访问冲突

c++ - OpenGL 帧缓冲区 - 渲染到纹理