Skip to content

Commit

Permalink
Withings - Use new OAuth2 API to get access and refresh tokens
Browse files Browse the repository at this point in the history
Fixes #4152
  • Loading branch information
amtriathlon committed Feb 2, 2022
1 parent 2f1194b commit 5237b46
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/Cloud/OAuthDialog.cpp
Expand Up @@ -328,10 +328,11 @@ OAuthDialog::urlChanged(const QUrl &url)

} else if (site == WITHINGS) {

urlstr = QString("https://account.withings.com/oauth2/token?");
urlstr = QString("https://wbsapi.withings.net/v2/oauth2");
params.addQueryItem("client_id", GC_NOKIA_CLIENT_ID);
params.addQueryItem("client_secret", GC_NOKIA_CLIENT_SECRET);
params.addQueryItem("redirect_uri","https://www.goldencheetah.org");
params.addQueryItem("action", "requesttoken");
params.addQueryItem("grant_type", "authorization_code");

}
Expand Down Expand Up @@ -453,6 +454,10 @@ OAuthDialog::networkRequestFinished(QNetworkReply *reply)
access_token = document.object()["access_token"].toString();
if (site == POLAR) polar_userid = document.object()["x_user_id"].toDouble();
if (site == RIDEWITHGPS) access_token = document.object()["user"].toObject()["auth_token"].toString();
if (site == WITHINGS) {
refresh_token = document.object()["body"].toObject()["refresh_token"].toString();
access_token = document.object()["body"].toObject()["access_token"].toString();
}
}

// if we failed to extract then we have a big problem
Expand Down
9 changes: 5 additions & 4 deletions src/Cloud/WithingsDownload.cpp
Expand Up @@ -85,12 +85,13 @@ WithingsDownload::getBodyMeasures(QString &error, QDateTime from, QDateTime to,

QString refresh_token = appsettings->cvalue(context->athlete->cyclist, GC_NOKIA_REFRESH_TOKEN).toString();

postData.addQueryItem("action", "requesttoken");
postData.addQueryItem("grant_type", "refresh_token");
postData.addQueryItem("client_id", GC_NOKIA_CLIENT_ID );
postData.addQueryItem("client_secret", GC_NOKIA_CLIENT_SECRET );
postData.addQueryItem("refresh_token", refresh_token );

QUrl url = QUrl( "https://account.withings.com/oauth2/token" );
QUrl url = QUrl( "https://wbsapi.withings.net/v2/oauth2" );

emit downloadStarted(100);

Expand All @@ -110,9 +111,9 @@ WithingsDownload::getBodyMeasures(QString &error, QDateTime from, QDateTime to,
QJsonParseError parseResult;
QJsonDocument migrateJson = QJsonDocument::fromJson(response.toUtf8(), &parseResult);

access_token = migrateJson.object()["access_token"].toString();
QString refresh_token = migrateJson.object()["refresh_token"].toString();
QString userid = QString("%1").arg(migrateJson.object()["userid"].toInt());
access_token = migrateJson.object()["body"].toObject()["access_token"].toString();
QString refresh_token = migrateJson.object()["body"].toObject()["refresh_token"].toString();
QString userid = QString("%1").arg(migrateJson.object()["body"].toObject()["userid"].toInt());


if (access_token != "") appsettings->setCValue(context->athlete->cyclist, GC_NOKIA_TOKEN, access_token);
Expand Down

0 comments on commit 5237b46

Please sign in to comment.