public class GoogleContact { private static final JsonFactory JSON_FACTORY = new JacksonFactory();
static String clientId="";
static String clientSecret="";
static String token="";
public static void main(String[] arg) {
ContactsService service = new ContactsService("contacts.googleapis.com");
NetHttpTransport httpTransport=null;
try {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
} catch (GeneralSecurityException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setClientSecrets(clientId, clientSecret)
.build().setRefreshToken(token);
try {
credential.refreshToken();
service.setOAuth2Credentials(credential);
service.useSsl();
} catch (Exception e) {
e.printStackTrace();
}
}
} response is : com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request { "error" : "invalid_grant", "error_description" : "Bad Request" } at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105) at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287) at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307) at com.google.api.client.auth.oauth2.Credential.executeRefreshToken(Credential.java:570) at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:247) at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
I had the same exception. Then I generated a new credentials.json file and deleted the old token from tokens folder. Then I run the program, it worked and a new token was generated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With