我在Qt中使用C ++,我正在尝试使用Youtube API查找按特定关键字搜索的Youtube视频。我已经有了一个API密钥。
我的代码是:
void Youtube::searchVideos(QString keyword)
{
int maxResults = 5; //the maximum of search results we want shown
QString youtubeurl = "https://www.googleapis.com/youtube/v3/search?part=snippet&type=video&q=" +
keyword +
"%203%27%203&key=" + QString(YOUTUBE_API_KEY) +
"&videoEmbeddable=true" +
"&maxResults=" + QString::number(maxResults);
QEventLoop loop;
QUrl url(youtubeurl);
if (_netManager == NULL)
{
_netManager = new QNetworkAccessManager(this);
QObject::connect(_netManager, SIGNAL(finished(QNetworkReply *)), this, SLOT(processReply(QNetworkReply *)));
}
QNetworkRequest request(url);
_reply = _netManager->get(request);
}
void Youtube::processReply(QNetworkReply * pReply)
{
QVariant statusCodeV = pReply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
QVariant redirectionTargetUrl = pReply->attribute(QNetworkRequest::RedirectionTargetAttribute);
QJsonObject results;
if (pReply->error() == QNetworkReply::NoError)
{
QByteArray message = pReply->readAll();
QString str = QString::fromUtf8(message.data(), message.size());
int statusCode = pReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
qDebug() << QVariant(statusCode).toString();
QJsonDocument jsonResponse = QJsonDocument::fromJson(message);
results = jsonResponse.object();
qDebug() << "Youtube API works.";
}
else
{
qDebug() << "Error searching on Youtube : " << QString(pReply->error()).toUtf8();
}
}
输出结果为:在Youtube上搜索时出错:“c”
我在Javascript中也使用了相同的链接和GET方法,但它运行正常但是我总是在C ++中遇到这个错误。
答案 0 :(得分:1)
包装&#34;评论调试&#34;的结果这是一个简短而干净的答案:
pReply->errorString()
,不要将错误代码(整数)转换为字符串https
解决方案:安装openssl 1.0。*(Qt 5.9不支持1.1。*)。对于Windows,可以在此处下载:https://slproweb.com/products/Win32OpenSSL.html