我不从事Blackberry App开发,但是出于某些原因,我需要Blackberry10 Native应用程序。在此应用程序中,我将使用OAuth API从网站获取数据并在应用程序中对其进行解析。我知道如何使用QNetworkAccessManager
发送常规网络请求并在QNetworkReply
中接收答复。我也了解OAuth1和OAuth2的基本流程。但是,Blackberry中的OAuth调用对我来说是新事物。
我正在使用Blackberry的oauth library。我已经在我的应用中成功实现了OAuth1调用,但是现在我需要使用OAuth2。但是在打电话时,我没有任何回应。 谁能帮我解决这个问题?
以下是我的 OAuth1 代码:
void ApplicationUI::test()
{
KQOAuthRequest* request = new KQOAuthRequest(this);
KQOAuthManager* manager = new KQOAuthManager(this);
QUrl url = QUrl("https://your-api-endpoint-here");
request->initRequest(KQOAuthRequest::TemporaryCredentials, url);
request->setConsumerKey(CONSUMER_KEY);
request->setConsumerSecretKey(CONSUMER_SECRET);
request->setHttpMethod(KQOAuthRequest::GET);
if (request->isValid()) {
qDebug() << "executing request...";
bool ok = connect(manager, SIGNAL(requestReady(QByteArray)), this, SLOT(onRequestFinished(QByteArray)));
Q_ASSERT(ok);
Q_UNUSED(ok);
manager->executeRequest(request);
} else {
qDebug() << "Invalid Request.";
}
}
void ApplicationUI::onRequestFinished(const QByteArray &data)
{
QString response = QString::fromUtf8(data);
qDebug() << "Reply: " << response;
}
输出:
正在执行请求...
回复:“从服务器回复”
对于 OAuth2 :
void ApplicationUI::test()
{
KQOAuthRequest* request = new KQOAuthRequest(this);
KQOAuthManager* manager = new KQOAuthManager(this);
QUrl url = QUrl("https://your-api-endpoint-here");
request->initRequest(KQOAuthRequest::AuthorizedRequest, url);
request->setRequestOAuthMethod(KQOAuthRequest::OAUTH2);
request->setToken("oauth_token");
request->setHttpMethod(KQOAuthRequest::GET);
if (request->isValid()) {
qDebug() << "executing request...";
bool ok = connect(manager, SIGNAL(requestReady(QByteArray)), this, SLOT(onRequestFinished(QByteArray)));
Q_ASSERT(ok);
Q_UNUSED(ok);
manager->executeRequest(request);
} else {
qDebug() << "Invalid Request.";
}
}
void ApplicationUI::onRequestFinished(const QByteArray &data)
{
QString response = QString::fromUtf8(data);
qDebug() << "Reply: " << response;
}
输出:
正在执行请求...
回复:“”