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 (将#修改为@)