微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Google 凭据已弃用

如何解决Google 凭据已弃用

尝试获取 Gmail 服务,它应该会显示消息“com.google.api.client.googleapis.auth.oauth2.GoogleCredential”已弃用。

这是我的代码片段。

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.gmail.Gmail;
import org.json.JSONException;
import org.json.JSONObject;

    public Gmail getGmailService() throws Exception {
        InputStream inputStream = getClass().getClassLoader().getResourceAsstream(SecurityConstant.CREDENTIALS.concat(environment).concat(SecurityConstant.JSON_EXTENSION));
        try {
          if (null != inputStream) {
            GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,new InputStreamReader(inputStream));
            String clientId = clientSecrets.getDetails().getClientId();
            String clientSecret = clientSecrets.getDetails().getClientSecret();
            Credential authorize = new **GoogleCredential**.Builder().setTransport(GoogleNetHttpTransport.newTrustedTransport())
                    .setJsonFactory(JSON_FACTORY)
                    .setClientSecrets(clientId,clientSecret)
                    .build().setAccesstoken(getAccesstoken(clientId,clientSecret,refreshToken));
            final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
            inputStream.close();
            return new Gmail.Builder(HTTP_TRANSPORT,JSON_FACTORY,authorize)
                    .setApplicationName(SecurityConstant.MODULE_NAME).build();
    
          }
        } catch (IOException | GeneralSecurityException exception) {
          exception.printstacktrace();
        }
        return null;
      }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。