gpt4 book ai didi

c++ - Twitter OAuth2 的 QNetworkRequest 返回禁止错误

转载 作者:行者123 更新时间:2023-11-30 05:16:08 24 4
gpt4 key购买 nike

我一直在努力使 Twitter API 的应用程序唯一身份验证在 Qt 上工作。当我运行我的代码时,出现此错误:

Failure "Error transferring https://api.twitter.com/oauth2/token - server replied: Forbidden"

这是我的请求代码:

    QJsonObject params;
params.insert("grant_type","client_credentials");

QUrl url("https://api.twitter.com/oauth2/token");

QNetworkRequest request;
request.setUrl(url);
QString authorizationKey = m_consumerKey + ":" + m_consumerSecret;
QByteArray encryptedAuth = authorizationKey.toUtf8().toBase64();
request.setRawHeader("User-Agent", "My App v1.0.0");
request.setRawHeader("Authorization", "Basic "+ encryptedAuth);
request.setRawHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
request.setSslConfiguration(QSslConfiguration::defaultConfiguration());

QNetworkReply* reply = m_networkManager->post(request, QJsonDocument(params).toJson());

也许我错过了什么?

最佳答案

我能够解决这个问题。事实证明,我发送的请求正文格式错误。我改变了:

QJsonObject params;
params.insert("grant_type","client_credentials");
...
QNetworkReply* reply = m_networkManager->post(request, QJsonDocument(params).toJson());

到:

QUrlQuery payload;
payload.addQueryItem("grant_type","client_credentials");
QByteArray postData = payload.query(QUrl::FullyEncoded).toUtf8();
...
QNetworkReply* reply = m_networkManager->post(request, postData);

关于c++ - Twitter OAuth2 的 QNetworkRequest 返回禁止错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42800618/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com