Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve { "error" : "invalid_grant", "error_description" : "Bad Request" } issue in google contact api

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)

like image 705
Bhaskar Avatar asked Oct 29 '25 00:10

Bhaskar


1 Answers

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.

like image 140
Tibor Jándi Avatar answered Oct 31 '25 14:10

Tibor Jándi