Get AccessToken From Refrsh Token
Hi, I am using this library. The acquired idToken is getting expired in none of the time. Is there any way to get fresh Token or some alternative via the RefreshToken. I have tried the performActionWithFreshToken also but no luck. Making API calls using the idToken returned by method performActionWithFreshToken should not get expired I guess. Also I have tried stting offline_acess as scope. Can you please guide me on the same. Thanks
@VirRajpurohit The offline_access scope is necessary to obtain a refresh token. When a user logs in the first time, the "offline_access" scope should be present so that a refresh token is returned (along with the id or access token). Then when you want to get a new access token, you need to send the refresh token to the /token endpoint. To learn more about how that is done manually, you can check out the "Refresh the token" section in our documentation. To learn more about how it can be done using AppAuth, I would look at TokenRequest.java
@parakhj Thanks for your quicker response. Regarding ==>Then when you want to get a new access token, you need to send the refresh token to the /token endpoint How this needs to be done like. I am currently making a call of PerformActionWithFreshtoken when I receive the response of authRequest. From my understanding the idToken returned with the above method should be available for long time session. Can you please make me understood on the above.
Also I can share the code snippet if you want as I need to get it done on the urgent basis. Thanks
Id Tokens are by default available for 1 hour. You can change that by editing the token configuration in your policy through the Azure AD B2C portal.
I don't know what "PerformActionWithFreshtoken" does. Is that custom code? Code might be helpful, so it doesn't hurt to send it. Read the "Refresh the token" documentation as well.
Just to clarify what the auth process is:
- You make a GET call to the /authorize endpoint and obtain a code
- You make a POST call to the /token endpoint with the code to obtain an id/access/refresh token
- A little later, you make a POST call to the /token endpoint with the refresh token to obtain a new id/access token
@parakhj Yes I have gone through the Doc and I understood the concept as well. The problem resides in the 3rd point you have mentioned. I dont know any method available in the library that do the above task.
idp.retrieveConfig(LoginActivity.this, mRetrieveCallback);
idp is instance of IdentityProvider.java class.
then in callback
if (idp.getClientId() == null) {
makeRegistrationRequest(serviceConfiguration, idp);
} else {
makeAuthRequest(serviceConfiguration, idp, new AuthState());
}
public void makeAuthRequest(
@NonNull AuthorizationServiceConfiguration serviceConfig,
@NonNull IdentityProvider idp,
@NonNull AuthState authState) {
String loginHint = "";
if (loginHint.isEmpty()) {
loginHint = null;
}
AuthorizationRequest authRequest = new AuthorizationRequest.Builder(
serviceConfig,
idp.getClientId(),
ResponseTypeValues.CODE,
idp.getRedirectUri())
.setScope("openid offline_access")
.setLoginHint(loginHint)
.build();
makeAuthRequest(authRequest, serviceConfig, authState);
}
then the pendingIntent call to respective Activity I do,
then in the targetActivity
public void performTokenRequest(TokenRequest request) {
ClientAuthentication clientAuthentication;
try {
clientAuthentication = mAuthState.getClientAuthentication();
} catch (ClientAuthentication.UnsupportedAuthenticationMethod ex) {
Log.d(TAG, "Token request cannot be made, client authentication for the token "
+ "endpoint could not be constructed (%s)", ex);
return;
}
mAuthService.performTokenRequest(
request,
clientAuthentication,
new AuthorizationService.TokenResponseCallback() {
@Override
public void onTokenRequestCompleted(
@Nullable TokenResponse tokenResponse,
@Nullable AuthorizationException ex) {
onResponseReceived(tokenResponse, ex);
}
});
}
then
public void onResponseReceived(TokenResponse tokenResponse, AuthorizationException authException) {
mPreferences.setStringDetail("REFRESH_TOKEN", tokenResponse.refreshToken);
mPreferences.setStringDetail("ACESS_TOKEN", tokenResponse.idToken);
mAuthState.update(tokenResponse, authException);
performActionWithFreshTokens is not the custom method it is available in the library which I think should be called after the onResponseReceived called and I have done so still the retrived token by this method is not available for longer session.
mAuthState.performActionWithFreshTokens(mAuthService, new AuthState.AuthStateAction() {
@Override
public void execute(String accessToken, String idToken, AuthorizationException ex) {
if (ex != null) {
Log.e(TAG, "Token refresh failed when fetching user info");
return;
}
mPreferences.setStringDetail("ACESS_TOKEN", idToken);
}
})
3.A little later, you make a POST call to the /token endpoint with the refresh token to obtain a new id/access token The little later can be the issue? Please guide me on the same. Thanks in advance
@parakhj Hey, Did you get a chance to look on it?
Leaving a comment for the same help
Need help too