简体   繁体   English

适用于iOS和Android的QT C ++中的HTTPS请求

[英]HTTPS Request in QT C++ for iOS & Android

I've been trying to find a way to integrate twitter to my cross platform qt project. 我一直在尝试找到一种将Twitter集成到我的跨平台qt项目中的方法。 What I want to achieve is nothing fancy. 我想实现的目标不算什么。 I just want to be able to use the Twitter API to fetch some tweets with a certain hashtag using app only credentials. 我只希望能够使用Twitter API使用仅应用程序凭据来获取带有特定主题标签的某些推文。 In order to do that, I had to get a bearer token using their oauth2 authentication process. 为此,我必须使用其oauth2身份验证过程来获取承载令牌。 And according to their development page, SSL is required. 根据他们的开发页面,需要SSL。 https://dev.twitter.com/oauth/application-only https://dev.twitter.com/oauth/application-only

I've read that in QT, I can use QNetworkAccessManager to send QNetworkRequests but I can't seem to make it work. 我已经读过在QT中,我可以使用QNetworkAccessManager发送QNetworkRequests,但是我似乎无法使其工作。 I get an error: "Protocol \\"\\" is unknown" 我收到错误消息:“协议\\” \\“未知”

I've tried ready a lot of posts but I couldn't get what I want. 我已经尝试了很多帖子,但是我找不到想要的东西。 I would appreciate all the help I could get to make this possible. 我将不胜感激,尽我所能提供帮助。 If anyone can point me to the right direction/tutorial please do so: 如果有人可以指出正确的方向/指南,请这样做:

Attached is my sample code: 随附的是我的示例代码:

.pro .pro

QT += qml quick network

setup code: 设置代码:

m_networkManager = new QNetworkAccessManager(this);
connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));

request code: 请求代码:

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

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

QNetworkRequest request;
QString authorizationKey =  m_consumerKey + ":" + m_consumerSecret;
request.setRawHeader("Authorization", "Basic "+ authorizationKey.toUtf8().toBase64());
request.setRawHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
m_networkManager->post(request, QJsonDocument(params).toJson());

and finally my callback: 最后是我的回调:

void NetworkClass::requestFinished(QNetworkReply *reply)
{
    cout << "header size: " + reply->readBufferSize();
    if (reply->error() == QNetworkReply::NoError) {
        //success
        QByteArray response_data = reply->readAll();
        QJsonDocument json = QJsonDocument::fromJson(response_data);
        cout <<"result: " << response_data.toStdString();
    }
    else {
        //failure
        qDebug() << "Failure" <<reply->errorString();
    }
    reply->deleteLater();
}

Note: I'm not very good with SSL and such. 注意:我对SSL等不是很好。 I'm still a new to this. 我还是这个新手。 Maybe that's why I feel so lost right now. 也许那就是为什么我现在感到如​​此迷茫。 Please please help me. 请帮帮我。 Thanks. 谢谢。

UPDATE 更新
@dutt pointed out that I forgot to set the url to the request. @dutt指出我忘了为请求设置URL。 Yey! 是的 Finally I received a reply. 最后我收到了回复。 However, I got this reply: 但是,我得到了以下答复:
"Error transferring https://api.twitter.com/oauth2/token - server replied: Forbidden" “传输https://api.twitter.com/oauth2/token时出错-服务器回复:禁止”

如果您可以使用Qt 5.8,则下一步可能会有所帮助: http : //blog.qt.io/blog/2017/01/25/connecting-qt-application-google-services-using-oauth-2-0/和示例https ://github.com/qt/qtnetworkauth/tree/5.8/examples/oauth/twittertimeline (未经我测试,但为官方示例)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM