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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.