Desidero ottenere un nuovo "token di accesso" basato sul "token di aggiornamento" salvato nel database.(JAVA) Analisi API Google - impossibile ottenere il nuovo "token di accesso" utilizzando "Aggiorna token"
Ecco il codice che ho scritto:
GoogleCredential.Builder credentialBuilder = new GoogleCredential.Builder()
.setTransport(HTTP_TRANSPORT).setJsonFactory(JSON_FACTORY)
.setClientSecrets(CLIENT_ID, CLIENT_SECRET);
credentialBuilder.addRefreshListener(new MyCredentialRefreshListener());
credential = credentialBuilder.build();
credential.setRefreshToken("saved_refresh_token_from_database");
try {
credential.refreshToken();
} catch (IOException e) {
e.printStackTrace();
}
class MyCredentialRefreshListener implements CredentialRefreshListener {
public void onTokenResponse(Credential cr, TokenResponse tr) {
System.out.println("Credential was refreshed successfully.");
}
public void onTokenErrorResponse(Credential cr, TokenErrorResponse tr) {
System.out.println(tr);
}
}
ottengo questo messaggio:
com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad
Richiesta {"errore": "invalid_grant"}
Io uso lo stesso CLIENT_ID, CLIE NT_SECRET e "Aggiorna token" in uno script php e lì sono riuscito a ottenere il nuovo "token di accesso" utilizzando "Aggiorna token" quando "token di accesso" è scaduto.
Ho scritto il codice basato su javadoc.google-oauth-java-client.
Qualcuno qui sa come modificare il codice per ottenere il nuovo token di accesso?
Grazie in anticipo.
UPDATE: il problema era che stavo salvando nel database il refresh_token senza fare un json_decode su di esso e conteneva un "\" che è considerato carattere di escape in JSON.
Ho provato il codice e ha funzionato per me. Penso che sia stato un problema di dati/formato con il tuo token di aggiornamento. – user1697575