QxtXmlRpcClient *RpcClientFactory::build() const { QUrl httpUrl(url); // url, username and password lost in loss ?!? httpUrl.setUserName(userName); httpUrl.setPassword(password); QxtXmlRpcClient *rpcClient = new QxtXmlRpcClient(parent); rpcClient->setServiceUrl(httpUrl); return rpcClient; }
bool CFitbit::RefreshToken(const bool bForce) { if (m_refreshToken.empty()) { return false; } if (!bForce) { if (!m_isLogged) return false; if ((mytime(NULL) - 15) < m_nextRefreshTs) return true; //no need to refresh the token yet } // Time to refresh the token std::stringstream sstr; sstr << "grant_type=refresh_token&"; sstr << "refresh_token=" << m_refreshToken << "&"; sstr << "client_id=" << m_clientId << "&"; sstr << "client_secret=" << m_clientSecret; std::string httpData = sstr.str(); std::vector<std::string> ExtraHeaders; ExtraHeaders.push_back("Host: api.Fitbit.net"); ExtraHeaders.push_back("Content-Type: application/x-www-form-urlencoded;charset=UTF-8"); std::string httpUrl("https://api.fitbit.net/oauth2/token"); std::string sResult; bool ret = HTTPClient::POST(httpUrl, httpData, ExtraHeaders, sResult); if (!ret) { _log.Log(LOG_ERROR, "Fitbit: Error connecting to Server..."); return false; } Json::Value root; Json::Reader jReader; ret = jReader.parse(sResult, root); if (!ret) { _log.Log(LOG_ERROR, "Fitbit: Invalid/no data received..."); //Force login next time m_isLogged = false; return false; } if (root["access_token"].empty() || root["expires_in"].empty() || root["refresh_token"].empty()) { //Force login next time _log.Log(LOG_ERROR, "Fitbit: No access granted, forcing login again..."); m_isLogged = false; return false; } /* m_accessToken = root["access_token"].asString(); m_refreshToken = root["refresh_token"].asString(); int expires = root["expires_in"].asInt(); m_nextRefreshTs = mytime(NULL) + expires; //StoreRefreshToken(); */ return true; }
bool CFitbit::Login() { if (m_isLogged) return true; if (LoadRefreshToken()) { if (RefreshToken(true)) { m_isLogged = true; return true; } } std::stringstream sstr; sstr << "grant_type=password&"; sstr << "client_id=" << m_clientId << "&"; sstr << "client_secret=" << m_clientSecret << "&"; sstr << "username="******"&"; sstr << "password="******"&"; sstr << "scope=activity heartrate"; std::string httpData = sstr.str(); std::vector<std::string> ExtraHeaders; ExtraHeaders.push_back("Host: api.fitbit.com"); ExtraHeaders.push_back("Content-Type: application/x-www-form-urlencoded;charset=UTF-8"); std::string httpUrl("https://api.fitbit.com/oauth2/token"); std::string sResult; bool ret = HTTPClient::POST(httpUrl, httpData, ExtraHeaders, sResult); if (!ret) { _log.Log(LOG_ERROR, "Fitbit: Error connecting to Server..."); return false; } #ifdef DEBUG_FitbitWeatherStationW SaveString2Disk(sResult, "E:\\fitbit_login.json"); #endif Json::Value root; Json::Reader jReader; ret = jReader.parse(sResult, root); if (!ret) { _log.Log(LOG_ERROR, "Fitbit: Invalid/no data received..."); return false; } if (root["access_token"].empty() || root["expires_in"].empty() || root["refresh_token"].empty()) { _log.Log(LOG_ERROR, "Fitbit: No access granted, check username/password..."); return false; } /* //Initial Access Token m_accessToken = root["access_token"].asString(); m_refreshToken = root["refresh_token"].asString(); //_log.Log(LOG_STATUS, "Access token: %s", m_accessToken.c_str()); //_log.Log(LOG_STATUS, "RefreshToken: %s", m_refreshToken.c_str()); int expires = root["expires_in"].asInt(); m_nextRefreshTs = mytime(NULL) + expires; StoreRefreshToken(); */ return true; }