fixed api key provider

This commit is contained in:
Boernsman 2020-12-02 21:13:45 +01:00
parent fba67dbd3b
commit 3207d5c8a4

View File

@ -53,11 +53,17 @@ void IntegrationPluginNetatmo::init()
void IntegrationPluginNetatmo::startPairing(ThingPairingInfo *info) void IntegrationPluginNetatmo::startPairing(ThingPairingInfo *info)
{ {
// Checking the client credentials
if (m_clientId.isEmpty() || m_clientSecret.isEmpty()) {
qCWarning(dcNetatmo()) << "Pairing failed, client credentials are not set";
info->finish(Thing::ThingErrorAuthenticationFailure, QT_TR_NOOP("Error, client credentials are not set."));
return;
}
// Checking the internet connection // Checking the internet connection
NetworkAccessManager *network = hardwareManager()->networkManager(); NetworkAccessManager *network = hardwareManager()->networkManager();
QNetworkReply *reply = network->get(QNetworkRequest(QUrl("https://api.netatmo.net"))); QNetworkReply *reply = network->get(QNetworkRequest(QUrl("https://api.netatmo.net")));
connect(reply, &QNetworkReply::finished, this, [reply, info] { connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
reply->deleteLater(); connect(reply, &QNetworkReply::finished, info, [reply, info] {
//The server replies usually 404 not found on this request //The server replies usually 404 not found on this request
//int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); //int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (reply->error() == QNetworkReply::NetworkError::HostNotFoundError) { if (reply->error() == QNetworkReply::NetworkError::HostNotFoundError) {
@ -70,17 +76,12 @@ void IntegrationPluginNetatmo::startPairing(ThingPairingInfo *info)
void IntegrationPluginNetatmo::confirmPairing(ThingPairingInfo *info, const QString &username, const QString &password) void IntegrationPluginNetatmo::confirmPairing(ThingPairingInfo *info, const QString &username, const QString &password)
{ {
if (m_clientId.isEmpty() || m_clientSecret.isEmpty()) {
info->finish(Thing::ThingErrorAuthenticationFailure, QT_TR_NOOP("Error, client credentials are not set."));
return;
}
OAuth2 *authentication = new OAuth2(m_clientId, m_clientSecret, this); OAuth2 *authentication = new OAuth2(m_clientId, m_clientSecret, this);
authentication->setUrl(QUrl("https://api.netatmo.net/oauth2/token")); authentication->setUrl(QUrl("https://api.netatmo.net/oauth2/token"));
authentication->setUsername(username); authentication->setUsername(username);
authentication->setPassword(password); authentication->setPassword(password);
authentication->setScope("read_station read_thermostat write_thermostat"); authentication->setScope("read_station read_thermostat write_thermostat");
connect(authentication, &OAuth2::authenticationChanged, info, [this, info, username, password, authentication](){ connect(authentication, &OAuth2::authenticationChanged, info, [this, info, username, password, authentication](){
if (authentication->authenticated()) { if (authentication->authenticated()) {
pluginStorage()->beginGroup(info->thingId().toString()); pluginStorage()->beginGroup(info->thingId().toString());
@ -138,7 +139,8 @@ void IntegrationPluginNetatmo::setupThing(ThingSetupInfo *info)
authentication->setPassword(password); authentication->setPassword(password);
} else { } else {
if (m_clientId.isEmpty() || m_clientSecret.isEmpty()) { if (m_clientId.isEmpty() || m_clientSecret.isEmpty()) {
info->finish(Thing::ThingErrorAuthenticationFailure, QT_TR_NOOP("Error, client credentials are not set.")); qCWarning(dcNetatmo()) << "Setup failed because of missing client credentials";
info->finish(Thing::ThingErrorAuthenticationFailure, QT_TR_NOOP("Error, client credentials are not set."));
return; return;
} }
authentication = new OAuth2(m_clientId, m_clientSecret, this); authentication = new OAuth2(m_clientId, m_clientSecret, this);
@ -433,19 +435,23 @@ void IntegrationPluginNetatmo::updateClientCredentials()
QString clientId = configValue(netatmoPluginCustomClientIdParamTypeId).toString(); QString clientId = configValue(netatmoPluginCustomClientIdParamTypeId).toString();
QString clientSecret = configValue(netatmoPluginCustomClientSecretParamTypeId).toString(); QString clientSecret = configValue(netatmoPluginCustomClientSecretParamTypeId).toString();
if (!clientId.isEmpty() && !clientSecret.isEmpty()) { if (!clientId.isEmpty() && !clientSecret.isEmpty()) {
qCDebug(dcNetatmo()) << "Using API key from plugin settings.";
m_clientId = clientId; m_clientId = clientId;
m_clientSecret = clientSecret; m_clientSecret = clientSecret;
qCDebug(dcNetatmo()) << "Using API key from plugin settings.";
qCDebug(dcNetatmo()) << " Client ID" << m_clientId.mid(0, 4)+"****";
qCDebug(dcNetatmo()) << " Client secret" << m_clientSecret.mid(0, 4)+"****";
return; return;
} }
clientId = apiKeyStorage()->requestKey("netatmo").data("clientId"); clientId = apiKeyStorage()->requestKey("netatmo").data("clientId");
clientId = apiKeyStorage()->requestKey("netatmo").data("clientSecret"); clientSecret = apiKeyStorage()->requestKey("netatmo").data("clientSecret");
if (!clientId.isEmpty() && !clientSecret.isEmpty()) { if (!clientId.isEmpty() && !clientSecret.isEmpty()) {
qCDebug(dcNetatmo()) << "Using API key from nymea API keys provider";
m_clientId = clientId; m_clientId = clientId;
m_clientSecret = clientSecret; m_clientSecret = clientSecret;
qCDebug(dcNetatmo()) << "Using API key from nymea API keys provider";
qCDebug(dcNetatmo()) << " Client ID" << m_clientId.mid(0, 4)+"****";
qCDebug(dcNetatmo()) << " Client secret" << m_clientSecret.mid(0, 4)+"****";
return; return;
} }
qCWarning(dcNetatmo()) << "No API key set."; qCWarning(dcNetatmo()) << "No API key set.";
qCWarning(dcNetatmo()) << "Either install an API key pacakge (nymea-apikeysprovider-plugin-*) or provide a key in the plugin settings."; qCWarning(dcNetatmo()) << "Either install an API key package (nymea-apikeysprovider-plugin-*) or provide a key in the plugin settings.";
} }