如何解决Google联系人api中的{“ error”:“ invalid_grant”,“ error_description”:“ Bad Request”}问题

时间:2019-04-23 14:51:25

标签: java google-api

公共类GoogleContact {     私有静态最终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();
    } 

}

} 响应是: com.google.api.client.auth.oauth2.TokenResponseException:400错误的请求 {   “ error”:“ invalid_grant”,   “ error_description”:“错误请求” }     在com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)     在com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)     在com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)     在com.google.api.client.auth.oauth2.Credential.executeRefreshToken(Credential.java:570)     在com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:247)     在com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)

1 个答案:

答案 0 :(得分:0)

我也有同样的例外。然后,我生成了一个新的certificate.json文件,并从令牌文件夹中删除了旧令牌。然后,我运行该程序,它开始工作并生成了一个新令牌。