Refresh token
This commit is contained in:
parent
9dca5fcd3b
commit
f21732f753
|
@ -172,6 +172,7 @@ public class MainActivity extends AppCompatActivity {
|
|||
oauthParams.setClient_id(account.getClient_id());
|
||||
oauthParams.setClient_secret(account.getClient_secret());
|
||||
oauthParams.setRefresh_token(account.getRefresh_token());
|
||||
oauthParams.setAccess_token(account.getToken());
|
||||
new Thread(() -> {
|
||||
try {
|
||||
new RetrofitPeertubeAPI(MainActivity.this).manageToken(oauthParams);
|
||||
|
|
|
@ -96,6 +96,16 @@ public interface PeertubeService {
|
|||
@Field("username") String username,
|
||||
@Field("password") String password);
|
||||
|
||||
//TOKEN
|
||||
//Refresh
|
||||
@FormUrlEncoded
|
||||
@POST("users/token")
|
||||
Call<Token> refreshToken(
|
||||
@Field("client_id") String client_id,
|
||||
@Field("client_secret") String client_secret,
|
||||
@Field("refresh_token") String refresh_token,
|
||||
@Field("grant_type") String grant_type);
|
||||
|
||||
@GET("users/me")
|
||||
Call<AccountData.Account> verifyCredentials(@Header("Authorization") String credentials);
|
||||
|
||||
|
@ -188,7 +198,11 @@ public interface PeertubeService {
|
|||
|
||||
//Get/Post/Update/Delete playlist
|
||||
@GET("video-playlists")
|
||||
Call<List<PlaylistData.Playlist>> getPlaylists();
|
||||
Call<PlaylistData> getPlaylists();
|
||||
|
||||
//Get/Post/Update/Delete playlist
|
||||
@GET("accounts/{accountHandle}/video-playlists")
|
||||
Call<PlaylistData> getPlaylistsForAccount(@Header("Authorization") String credentials, @Path("accountHandle") String accountHandle);
|
||||
|
||||
@GET("video-playlists/{id}")
|
||||
Call<PlaylistData.Playlist> getPlaylist(@Path("id") String id);
|
||||
|
|
|
@ -176,12 +176,23 @@ public class RetrofitPeertubeAPI {
|
|||
Call<Token> refreshTokenCall = null;
|
||||
if (oauthParams.getGrant_type().compareTo("password") == 0) {
|
||||
refreshTokenCall = peertubeService.createToken(oauthParams.getClient_id(), oauthParams.getClient_secret(), oauthParams.getGrant_type(), oauthParams.getUsername(), oauthParams.getPassword());
|
||||
}else if (oauthParams.getGrant_type().compareTo("refresh_token") == 0) {
|
||||
refreshTokenCall = peertubeService.refreshToken(oauthParams.getClient_id(), oauthParams.getClient_secret(), oauthParams.getRefresh_token(), oauthParams.getGrant_type());
|
||||
}
|
||||
if (refreshTokenCall != null) {
|
||||
try {
|
||||
Response<Token> response = refreshTokenCall.execute();
|
||||
if (response.isSuccessful()) {
|
||||
return response.body();
|
||||
Token tokenReply = response.body();
|
||||
if (oauthParams.getGrant_type().compareTo("refresh_token") == 0 && tokenReply != null) {
|
||||
SharedPreferences sharedpreferences = _context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
||||
SharedPreferences.Editor editor = sharedpreferences.edit();
|
||||
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, tokenReply.getAccess_token());
|
||||
editor.apply();
|
||||
SQLiteDatabase db = Sqlite.getInstance(_context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
||||
new AccountDAO(_context, db).updateAccountToken(oauthParams.getExternalAuthToken(), tokenReply);
|
||||
}
|
||||
return tokenReply;
|
||||
} else {
|
||||
Error error = new Error();
|
||||
error.setStatusCode(response.code());
|
||||
|
@ -1082,7 +1093,7 @@ public class RetrofitPeertubeAPI {
|
|||
* @param videoId String id of the video
|
||||
* @return APIResponse
|
||||
*/
|
||||
public APIResponse playlistAction(PlaylistsVM.action type, String playlistId, String videoId) {
|
||||
public APIResponse playlistAction(PlaylistsVM.action type, String playlistId, String videoId, String acct) {
|
||||
PeertubeService peertubeService = init();
|
||||
|
||||
APIResponse apiResponse = new APIResponse();
|
||||
|
@ -1101,12 +1112,13 @@ public class RetrofitPeertubeAPI {
|
|||
}
|
||||
|
||||
} else if (type == PlaylistsVM.action.GET_PLAYLISTS) {
|
||||
Call<List<PlaylistData.Playlist>> playlistsCall = peertubeService.getPlaylists();
|
||||
Response<List<PlaylistData.Playlist>> response = playlistsCall.execute();
|
||||
if (response.isSuccessful()) {
|
||||
apiResponse.setPlaylists(response.body());
|
||||
Call<PlaylistData> playlistsCall = peertubeService.getPlaylistsForAccount(token, acct);
|
||||
Response<PlaylistData> response = playlistsCall.execute();
|
||||
if (response.isSuccessful() && response.body() != null) {
|
||||
apiResponse.setPlaylists(response.body().data);
|
||||
} else {
|
||||
Error error = generateError(response.code(), response.message());
|
||||
assert response.errorBody() != null;
|
||||
Error error = generateError(response.code(), response.errorBody().string());
|
||||
apiResponse.setError(error);
|
||||
}
|
||||
} else if (type == PlaylistsVM.action.GET_LIST_VIDEOS) {
|
||||
|
|
|
@ -35,6 +35,8 @@ public class OauthParams {
|
|||
private String externalAuthToken;
|
||||
@SerializedName("refresh_token")
|
||||
private String refresh_token;
|
||||
@SerializedName("access_token")
|
||||
private String access_token;
|
||||
|
||||
|
||||
public String getClient_secret() {
|
||||
|
@ -100,4 +102,12 @@ public class OauthParams {
|
|||
public void setRefresh_token(String refresh_token) {
|
||||
this.refresh_token = refresh_token;
|
||||
}
|
||||
|
||||
public String getAccess_token() {
|
||||
return access_token;
|
||||
}
|
||||
|
||||
public void setAccess_token(String access_token) {
|
||||
this.access_token = access_token;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ import java.util.List;
|
|||
|
||||
import app.fedilab.fedilabtube.client.data.AccountData.Account;
|
||||
import app.fedilab.fedilabtube.client.entities.Avatar;
|
||||
import app.fedilab.fedilabtube.client.entities.Token;
|
||||
import app.fedilab.fedilabtube.helper.Helper;
|
||||
|
||||
|
||||
|
@ -128,6 +129,28 @@ public class AccountDAO {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Update an Account token in database
|
||||
*
|
||||
* @param oldToken String
|
||||
* @param token Token
|
||||
* @return boolean
|
||||
*/
|
||||
public int updateAccountToken(String oldToken, Token token) {
|
||||
ContentValues values = new ContentValues();
|
||||
|
||||
|
||||
if (token.getRefresh_token() != null) {
|
||||
values.put(Sqlite.COL_REFRESH_TOKEN, token.getRefresh_token());
|
||||
}
|
||||
if (token.getAccess_token() != null)
|
||||
values.put(Sqlite.COL_OAUTHTOKEN, token.getAccess_token());
|
||||
return db.update(Sqlite.TABLE_USER_ACCOUNT,
|
||||
values, Sqlite.COL_OAUTHTOKEN + " = ? ",
|
||||
new String[]{oldToken});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Update an Account in database
|
||||
*
|
||||
|
|
|
@ -87,7 +87,7 @@ public class PlaylistsVM extends AndroidViewModel {
|
|||
apiResponse = new APIResponse();
|
||||
apiResponse.setPlaylists(new ArrayList<>());
|
||||
} else {
|
||||
apiResponse = new RetrofitPeertubeAPI(_mContext).playlistAction(apiAction, playlist.getId(), videoId);
|
||||
apiResponse = new RetrofitPeertubeAPI(_mContext).playlistAction(apiAction, playlist!=null?playlist.getId():null, videoId, account.getAcct());
|
||||
}
|
||||
Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||
if (apiResponse != null) {
|
||||
|
|
Loading…
Reference in New Issue