From f28d63e8d4d4f93b785d3444b3691184790fdfb9 Mon Sep 17 00:00:00 2001 From: stom79 Date: Sun, 6 Jan 2019 14:44:33 +0100 Subject: [PATCH] refresh fixes --- .../UpdateAccountInfoAsyncTask.java | 7 ++++--- .../UpdateAccountInfoByIDAsyncTask.java | 20 +++++++++++++++++++ .../etalab/mastodon/client/PeertubeAPI.java | 1 + 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/UpdateAccountInfoAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/UpdateAccountInfoAsyncTask.java index d01337b94..c6da177a5 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/UpdateAccountInfoAsyncTask.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/UpdateAccountInfoAsyncTask.java @@ -64,7 +64,6 @@ public class UpdateAccountInfoAsyncTask extends AsyncTask { @Override protected Void doInBackground(Void... params) { Account account = null; - if( social == SOCIAL.MASTODON) { account = new API(this.contextReference.get(), instance, null).verifyCredentials(); account.setSocial("MASTODON"); @@ -75,8 +74,10 @@ public class UpdateAccountInfoAsyncTask extends AsyncTask { }catch (HttpsConnection.HttpsConnectionException exception){ if(exception.getStatusCode() == 401){ HashMap values = new PeertubeAPI(this.contextReference.get(), instance, null).refreshToken(client_id, client_secret, refresh_token); - this.token = values.get("access_token"); - this.refresh_token = values.get("refresh_token"); + if( values.get("access_token") != null) + this.token = values.get("access_token"); + if( values.get("refresh_token") != null) + this.refresh_token = values.get("refresh_token"); } } } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/UpdateAccountInfoByIDAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/UpdateAccountInfoByIDAsyncTask.java index a402fdd57..87a3dc4f0 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/UpdateAccountInfoByIDAsyncTask.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/UpdateAccountInfoByIDAsyncTask.java @@ -20,6 +20,7 @@ import android.database.sqlite.SQLiteDatabase; import android.os.AsyncTask; import java.lang.ref.WeakReference; +import java.util.HashMap; import fr.gouv.etalab.mastodon.client.API; import fr.gouv.etalab.mastodon.client.APIResponse; @@ -65,6 +66,25 @@ public class UpdateAccountInfoByIDAsyncTask extends AsyncTask } catch (HttpsConnection.HttpsConnectionException e) { e.printStackTrace(); } + try { + account = new PeertubeAPI(this.contextReference.get()).verifyCredentials(); + account.setSocial("PEERTUBE"); + }catch (HttpsConnection.HttpsConnectionException exception){ + if(exception.getStatusCode() == 401){ + SQLiteDatabase db = Sqlite.getInstance(this.contextReference.get(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); + account = new AccountDAO(this.contextReference.get(), db).getAccountByToken(token); + HashMap values = new PeertubeAPI(this.contextReference.get()).refreshToken(account.getClient_id(), account.getClient_secret(), account.getRefresh_token().trim()); + String newtoken = values.get("access_token"); + String refresh_token = values.get("refresh_token"); + if( newtoken != null) + account.setToken(newtoken); + if( refresh_token != null) + account.setRefresh_token(refresh_token); + new AccountDAO(this.contextReference.get(), db).updateAccount(account); + } + } + } if( account == null) return null; diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/PeertubeAPI.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/PeertubeAPI.java index 9e0ecf134..bfd238aa0 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/PeertubeAPI.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/PeertubeAPI.java @@ -236,6 +236,7 @@ public class PeertubeAPI { params.put("client_id", client_id); params.put("client_secret", client_secret); params.put("refresh_token", refresh_token); + try { String response = new HttpsConnection(context).post(getAbsoluteUrl("/users/token"), 60, params, null); JSONObject resobj = new JSONObject(response);