gpt4 book ai didi

c++ - 无法使用 Reddit API 通过仅应用程序 OAuth 检索访问 token

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:37:36 24 4
gpt4 key购买 nike

所以我已经阅读了以下链接中的文档 https://github.com/reddit-archive/reddit/wiki/OAuth2 .我正在尝试为我的应用程序检索一个访问 token ,它只需要一个仅应用程序 OAuth,因为它不需要用户插入他们的凭据。我已按照所提到页面上的说明进行操作,但我无法检索访问 token ,而且我总是得到:

"{\"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;
}

我已经在 https://ssl.reddit.com/prefs/apps/ 注册了我的申请使用“已安装”类型选项。

最佳答案

好的,所以我找到了问题。我没读 'Basic' HTTP Authentication Scheme并在我修改为的授权 header 中忘记了 ::

mAuthHeader = "Basic " + (CLIENT_IDENTIFIER + ":").toBase64();

关于c++ - 无法使用 Reddit API 通过仅应用程序 OAuth 检索访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51769986/

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