How to connect to Alexa with Qt? - c++

I'm writing an Qt application and try to use Alexa API. I received access token, but I can't use API because of "Host not found" and "Connection closed".
My QNetworkAccessManager defined as
amazonHelper.data()->setNetworkAccessManager(view.data()->engine()->networkAccessManager());
...
void AmazonHelper::setNetworkAccessManager(QNetworkAccessManager *qnam) {
qDebug() << "setNetworkAccessManager()";
_manager = qnam;
connect(_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));
}
After executing
_manager->connectToHostEncrypted("https://avs-alexa-na.amazon.com");
I receive an error "Host not found".
After executing
QNetworkRequest request(QUrl("https://avs-alexa-na.amazon.com/v20160207/directives"));
request.setRawHeader("Authorization", "Bearer %1" + _accessToken.toUtf8());
_manager->get(request);
I receive an error "Connection closed".
What is a right way to use Amazon Alexa API?
Thanks in advance!
UPD1:
I found QNetworkRequest::SpdyAllowedAttribute in Qt documentation but when I tried to set this attribute I got the follow error: 'SpdyAllowedAttribute' is not a member of 'QNetworkRequest'
UPD2:
I tried to use libcurlcpp but after setting CURLOPT_HTTP_VERSION to CURL_HTTP_VERSION_2_0 got exception (https://github.com/JosephP91/curlcpp/issues/84)

Related

QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection'

i'm trying to connect my qt application to a Mysql database and don't know why it's showing the following error message : QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
can someone help me please, this is my code :
void MainWindow::on_pushButton_clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("127.0.0.1");
db.setUserName("root");
db.setPassword("");
db.setDatabaseName("pmkfinal");
if(db.open()){
QMessageBox::information(this,"Connection","Database Connected Successfully");
}else{
QMessageBox::information(this,"Connection","Database not Connected Successfully");
}
}
Thank you All !!
Do not connect to the database repeatedly. Or you can provide an alternate connection name to avoid the warning message.

AWS SNSClient publish call could not reach endpoint

I am trying to publish a message to a topic using the AWS SNSClient from the c++ SDK.
Can someone help me to find a way to figure out what is wrong with my approach? The error message I am getting only says that the "endpoint could not be reached".
I am trying to figure out where my request hangs - in my point of view it could be one of the following:
the docker container the c++ app is running in is blocking the requests somehow (new to docker)
the client configuration is wrong (region, arn, creditials wrong?)
the request is malformed (some parameters not set? Message type maybe?)
Does someone know how I can debug my request and see what the issue is?
Thanks! My code looks something like this (api init and shutdown is omitted):
Aws::SNS::SNSClient client(credentials , config);
Aws::SNS::Model::PublishRequest pubReq;
pubReq.SetTopicArn("...");
pubReq.SetMessage("Test message");
pubOutcome = client.Publish(pubReq);
if(! pubOutcome.IsSuccess() ){
std::cout << "outcome: " << pubOutcome.GetError().GetMessage() << std::endl;
}
My guess without being able to see your code is that you have not specified the correct region. If your code hangs for a few seconds then this is most likely the problem.
Add a line of code like this before your create the SNS Client:
config.region = "us-west-2";
To enable debugging add this line before Aws::InitAPI(options)
options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Debug;
The headers for logging:
#include <aws/core/utils/logging/DefaultLogSystem.h>
#include <aws/core/utils/logging/AWSLogging.h>
Then you can review the logfile that is generated. It will start with "aws_sdk"
I use Visual Studio, so I prefer to step into the code to figure out what is wrong. Sometimes it is simpler to review the logfile.

"Connection closed" When using QNetworkAccessManager and QTcpServer

I have simple server with QTcpServer and simple client with QNetworkAccessManager.
When I request data from the server via curl or browser everything is ok
When I request data from any site via QNetworkAccessManager everything is ok
But I can not read data from QTcpServer via QNetworkAccessManager. All requests are reseted. QNetworkAccessManager (client) had send RST (reset connection) right after it received a data from server. And in client code we get the error: "Connection closed" (RemoteHostClosedError)
Aslo, I tried use QNetworkAccessManager from DownloadManager example and QTcpServer from FortuneServer example in various combinations, but the results were the same.
Tested Qt Versions:
Mac Qt 5.7
Linux Qt 5.7
Linux Qt 5.6.2
Linux Qt 5.5.1
Wireshark screenshot: qt-wireshark.png
The upper parts (with red lines) are results of QNetworkAccessManager, and the latest packets with success result are curl attempt to get data from QTcpServer
Also there is a simple example to reproduce the error: testNetwork.zip
And here is sample code for client:
void test(quint16 port)
{
QNetworkAccessManager *manager = new QNetworkAccessManager();
QNetworkRequest request;
request.setUrl(QUrl(QString("http://127.0.0.1:%1/").arg(port)));
manager->connect(manager, &QNetworkAccessManager::finished,
[](QNetworkReply *reply) {
qDebug() << QString("Finished. %1. %2").arg(reply->errorString()).arg(reply->error());
qDebug() << "readed: " << reply->readAll();
});
QNetworkReply *reply = manager->get(request);
reply->connect(reply, &QNetworkReply::readyRead, [reply]() {
qDebug() << QString("readyRead: '%1'").arg(QString(reply->readAll()));
});
}
and for server:
QTcpSocket socket;
...
if(socket.waitForReadyRead(5000))
{
QByteArray request;
request += socket.readAll();
QByteArray responce("HELLO, WORLD! HELLO, WORLD! HELLO, WORLD! HELLO, WORLD!");
socket.write(responce);
if(!socket.waitForBytesWritten())
{
qWarning() << QString("Error occurred in waitForBytesWritten() method of the tcp socket. %1 (%2)")
.arg(socket.errorString())
.arg(socket.error());
}
}
else
{
qWarning() << QString("Error occurred in read method of the tcp socket. %1 (%2)")
.arg(socket.errorString())
.arg(socket.error());
}
Also I created a Bug report on qt.io (QTBUG-56631)
Your client is making an HTTP request, but your server isn't an http server - it's not sending back a valid HTTP request.
Your client works when you point it at a web server, because that IS an http server.
QByteArray responce("HELLO, WORLD! HELLO, WORLD! HELLO, WORLD! HELLO, WORLD!");
isn't a valid HTTP response.

QNetworkReply: Network access is disabled in QWebView

I cannot load website into my QWebView, QNetworkReply is returning me the error: Network Access is disabled. Loading files from local works.
I am using Qt5. Does anyone know why is connection disabled and how this line affects this situation:
QNetworkProxyFactory::setUseSystemConfiguration(false);
My eth0 connection works properly, and I am able to ping any website.
From the Qt doc : calling setUseSystemConfiguration() overrides any application proxy or proxy factory that was previously set. So be careful to not have set any other proxy before.
Moreover, if you want to check the Network access, you might do it that way :
QNetworkAccessManager m_pManager;
QNetworkConfigurationManager configManager;
m_pManager.setConfiguration(configManager.defaultConfiguration());
connect(&m_pManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
connect(&m_pManager, SIGNAL(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)), this, SLOT(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)));
and in your slot :
if(accessible != QNetworkAccessManager::Accessible)
{
// case where the network is not available
}
And for the reply, you can check in the slot replyFinished() if there was an error during the process.

Qt, C++: GConf-WARNING **: Client failed to connect to the D-BUS daemon

I'm trying to make a form application under Arch Linux with Qt and C++, but when I try to run my application which is below:
firstWindow.h
#include <QMainWindow>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QLayout>
class firstWindow:public QMainWindow
{
Q_OBJECT
public:
QLabel *lbl;
QPushButton *btn;
QLineEdit *row;
firstWindow():QMainWindow()
{
setWindowTitle("First Window");
QWidget *win = new QWidget(this);
setCentralWidget(win);
lbl = new QLabel("Hello Universe", win);
btn = new QPushButton("Click Click", win);
row = new QLineEdit(win);
QVBoxLayout *main = new QVBoxLayout(win);
main->addWidget(win);
QHBoxLayout *nextTo = new QHBoxLayout();
nextTo->addWidget(row);
nextTo->addWidget(btn);
main->addLayout(nextTo);
resize(200,50);
win->show();
}
};
After compiling it, I get this error:
#qmake -project
#qmake
#make
# ./QtFirstWindow
(process:21806): GConf-WARNING **: Client failed to connect to the D-BUS daemon:
Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.
(process:21806): GConf-WARNING **: Client failed to connect to the D-BUS daemon:
Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.
(process:21806): GConf-WARNING **: Client failed to connect to the D-BUS daemon:
Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.
Qt: Session management error: Authentication Rejected, reason : None of the authentication protocols specified are supported
It doesn't stop or kill the application, but it also doesn't run it. I think that error isn't about the code that I wrote, because it is a little sample. So how can I deal with that? Any idea?