I am trying to get an access token for FCM v1 using the below code.
static String SCOPES = "https://www.googleapis.com/auth/firebase.messaging";
private static String getAccessToken() throws IOException {
GoogleCredentials googleCredentials = GoogleCredentials
.getApplicationDefault()
.createScoped(Arrays.asList(SCOPES));
// googleCredentials.refresh();
googleCredentials.refreshIfExpired(); // Doesn't work either
return googleCredentials.getAccessToken().getTokenValue();
}
I am able to get the token and publish messages to FCM. The problem is that everytime I call refreshIfExpired()
, it gives me a new token even if the previous one is not expired. Is this expected? Shall I check for expiry at my end? I am guessing there would be some rate limiter to fetch new tokens. I also tried to have a common GoogleCredentials
object but still the same issue.
Where is the refresh token stored? Where does it check for access token expiry (client or server)?
I tried to read the documentation but it’s not clear enough for me. I need some clear understanding on this and a way to refresh the token only when it’s expired.