无法使用 Reddit API 通过仅应用程序 OAuth 检索访问令牌
Unable to retrieve access token with Application Only OAuth using Reddit API
所以我阅读了linkhttps://github.com/reddit-archive/reddit/wiki/OAuth2下面的文档。我正在尝试为我的应用程序检索一个访问令牌,它只需要一个仅应用程序 OAuth,因为它不需要用户插入他们的凭据。我已按照上述页面上的说明进行操作,但我无法检索访问令牌,而且我总是得到:
"{\"message\": \"Unauthorized\", \"error\": 401}"
这是我的代码:
#include "reddit.h"
#include <QtNetwork>
#include <QUuid>
const QString GRANT_URL = "https://oauth.reddit.com/grants/installed_client";
const QString ACCESS_TOKEN_URL = "https://www.reddit.com/api/v1/access_token";
const QByteArray CLIENT_IDENTIFIER = "MYID";
Reddit::Reddit(QObject *parent) : QObject(parent)
{
mDeviceID = "DO_NOT_TRACK_THIS_DEVICE";
mAuthHeader = "Basic " + CLIENT_IDENTIFIER.toBase64();
}
void Reddit::getAccessToken()
{
auto netManager = new QNetworkAccessManager(this);
QUrl requestUrl = buildAccessTokenUrl();
QNetworkRequest netRequest(requestUrl);
netRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
netRequest.setRawHeader("Authorization", mAuthHeader);
auto reply = netManager->post(netRequest, requestUrl.query(QUrl::FullyEncoded).toUtf8());
connect(reply, &QNetworkReply::finished, this, &Reddit::accessTokenRequestFinished);
}
void Reddit::accessTokenRequestFinished()
{
auto reply = qobject_cast<QNetworkReply*>(sender());
qDebug() << reply->readAll();
reply->deleteLater();
}
QUrl Reddit::buildAccessTokenUrl()
{
QUrl url(ACCESS_TOKEN_URL);
QUrlQuery urlQuery;
urlQuery.addQueryItem("grant_type", GRANT_URL);
urlQuery.addQueryItem("device_id", mDeviceID);
url.setQuery(urlQuery);
return url;
}
我已使用 "installed" 类型选项在 https://ssl.reddit.com/prefs/apps/ 注册了我的应用程序。
好的,所以我找到了问题。我没有阅读 'Basic' HTTP Authentication Scheme 并且忘记了我的授权 header 中的 :
我修改为:
mAuthHeader = "Basic " + (CLIENT_IDENTIFIER + ":").toBase64();
所以我阅读了linkhttps://github.com/reddit-archive/reddit/wiki/OAuth2下面的文档。我正在尝试为我的应用程序检索一个访问令牌,它只需要一个仅应用程序 OAuth,因为它不需要用户插入他们的凭据。我已按照上述页面上的说明进行操作,但我无法检索访问令牌,而且我总是得到:
"{\"message\": \"Unauthorized\", \"error\": 401}"
这是我的代码:
#include "reddit.h"
#include <QtNetwork>
#include <QUuid>
const QString GRANT_URL = "https://oauth.reddit.com/grants/installed_client";
const QString ACCESS_TOKEN_URL = "https://www.reddit.com/api/v1/access_token";
const QByteArray CLIENT_IDENTIFIER = "MYID";
Reddit::Reddit(QObject *parent) : QObject(parent)
{
mDeviceID = "DO_NOT_TRACK_THIS_DEVICE";
mAuthHeader = "Basic " + CLIENT_IDENTIFIER.toBase64();
}
void Reddit::getAccessToken()
{
auto netManager = new QNetworkAccessManager(this);
QUrl requestUrl = buildAccessTokenUrl();
QNetworkRequest netRequest(requestUrl);
netRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
netRequest.setRawHeader("Authorization", mAuthHeader);
auto reply = netManager->post(netRequest, requestUrl.query(QUrl::FullyEncoded).toUtf8());
connect(reply, &QNetworkReply::finished, this, &Reddit::accessTokenRequestFinished);
}
void Reddit::accessTokenRequestFinished()
{
auto reply = qobject_cast<QNetworkReply*>(sender());
qDebug() << reply->readAll();
reply->deleteLater();
}
QUrl Reddit::buildAccessTokenUrl()
{
QUrl url(ACCESS_TOKEN_URL);
QUrlQuery urlQuery;
urlQuery.addQueryItem("grant_type", GRANT_URL);
urlQuery.addQueryItem("device_id", mDeviceID);
url.setQuery(urlQuery);
return url;
}
我已使用 "installed" 类型选项在 https://ssl.reddit.com/prefs/apps/ 注册了我的应用程序。
好的,所以我找到了问题。我没有阅读 'Basic' HTTP Authentication Scheme 并且忘记了我的授权 header 中的 :
我修改为:
mAuthHeader = "Basic " + (CLIENT_IDENTIFIER + ":").toBase64();