Google drive api google drive客户端库文件上载时出现无效授权错误

Google drive api google drive客户端库文件上载时出现无效授权错误,google-drive-api,spring-boot,Google Drive Api,Spring Boot,当我试图在google drive中的文件夹下添加文件时,总是会出现此错误,该文件夹被标记为“与abc.com中的任何人都可以查看和编辑的模式共享” com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request { "error" : "invalid_grant" } 我遵循《谷歌开发者指南》中的程序 Delegating domain-wide authority to the service acc

当我试图在google drive中的文件夹下添加文件时,总是会出现此错误,该文件夹被标记为“与abc.com中的任何人都可以查看和编辑的模式共享”

com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
  "error" : "invalid_grant"
}
我遵循《谷歌开发者指南》中的程序

Delegating domain-wide authority to the service account

If your application accesses user data, the service account that you created needs to be granted access to the Google Apps domain’s user data that you want to access.

The following steps must be performed by an administrator of the Google Apps domain:

Go to your Google Apps domain’s Admin console.
Select Security from the list of controls. If you don't see Security listed, select More controls from the gray bar at the bottom of the page, then select Security from the list of controls. If you can't see the controls, make sure you're signed in as an administrator for the domain.
Select Show more and then Advanced settings from the list of options.
Select Manage API client access in the Authentication section.
In the Client Name field enter the service account's Client ID.
In the One or More API Scopes field enter the list of scopes that your application should be granted access to. For example, if your application needs domain-wide access to the Google Drive API and the Google Calendar API, enter: https://www.googleapis.com/auth/drive, https://www.googleapis.com/auth/calendar.
Click Authorize.
Your application now has the authority to make API calls as users in your domain (to "impersonate" users). When you prepare to make authorized API calls, you specify the user to impersonate.
在application.properties中添加了键

然后为驱动器创建了一个配置类

@Configuration
public class GoogleDriveAPIConfiguration {

  @Resource
  private Environment environment;

  private final org.springframework.core.io.Resource pkeyFile = new ClassPathResource("abcdefgh-0aafb873fcc9.p12");

  private static final String GOOGLE_RIVE_SERVICE_ACCOUNT = "google.drive.service.account";

  private static final String GOOGLE_DRIVE_APPLICATION_NAME = "google.drive.application.name";

  private static final String GOOGLE_DRIVE_IMPERSONATE_ACCOUNT = "google.drive.impersonate.account";

  @Bean
  @Autowired
  public GoogleCredential googleCredential(JsonFactory jsonFactory, HttpTransport httpTransport)
      throws GeneralSecurityException, IOException {

    GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
        .setJsonFactory(jsonFactory).setServiceAccountId(environment.getProperty(GOOGLE_RIVE_SERVICE_ACCOUNT))
        .setServiceAccountPrivateKeyFromP12File(getPrivateKeyFile())
        .setServiceAccountScopes(Collections.singleton(DriveScopes.DRIVE))
        .setServiceAccountId(environment.getProperty(GOOGLE_DRIVE_IMPERSONATE_ACCOUNT))
        .build();

    return credential;
  }

  @Bean
  public JsonFactory getJsonFactory() {
    JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
    return JSON_FACTORY;
  }

  @Bean
  public HttpTransport getHttpTransport() throws GeneralSecurityException, IOException {
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    return httpTransport;
  }

  @Bean
  @Autowired
  public Drive getDrive(HttpTransport httpTransport, JsonFactory JSON_FACTORY, GoogleCredential credential) {
    Drive drive = new Drive.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(
        GOOGLE_DRIVE_APPLICATION_NAME).build();
    return drive;
  }

  private File getPrivateKeyFile() throws IOException {
    return pkeyFile.getFile();
  }
}
应用程序启动时没有任何错误,下面是文件上载代码完整文件


错误消息也没有什么帮助,你知道我哪里出了问题吗?

这些信息对你很有帮助:我已经涵盖了所有这些,奇怪的是,我仍然不知道这里出了什么问题:-
@Configuration
public class GoogleDriveAPIConfiguration {

  @Resource
  private Environment environment;

  private final org.springframework.core.io.Resource pkeyFile = new ClassPathResource("abcdefgh-0aafb873fcc9.p12");

  private static final String GOOGLE_RIVE_SERVICE_ACCOUNT = "google.drive.service.account";

  private static final String GOOGLE_DRIVE_APPLICATION_NAME = "google.drive.application.name";

  private static final String GOOGLE_DRIVE_IMPERSONATE_ACCOUNT = "google.drive.impersonate.account";

  @Bean
  @Autowired
  public GoogleCredential googleCredential(JsonFactory jsonFactory, HttpTransport httpTransport)
      throws GeneralSecurityException, IOException {

    GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
        .setJsonFactory(jsonFactory).setServiceAccountId(environment.getProperty(GOOGLE_RIVE_SERVICE_ACCOUNT))
        .setServiceAccountPrivateKeyFromP12File(getPrivateKeyFile())
        .setServiceAccountScopes(Collections.singleton(DriveScopes.DRIVE))
        .setServiceAccountId(environment.getProperty(GOOGLE_DRIVE_IMPERSONATE_ACCOUNT))
        .build();

    return credential;
  }

  @Bean
  public JsonFactory getJsonFactory() {
    JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
    return JSON_FACTORY;
  }

  @Bean
  public HttpTransport getHttpTransport() throws GeneralSecurityException, IOException {
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    return httpTransport;
  }

  @Bean
  @Autowired
  public Drive getDrive(HttpTransport httpTransport, JsonFactory JSON_FACTORY, GoogleCredential credential) {
    Drive drive = new Drive.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(
        GOOGLE_DRIVE_APPLICATION_NAME).build();
    return drive;
  }

  private File getPrivateKeyFile() throws IOException {
    return pkeyFile.getFile();
  }
}
  @Autowired
  public void setDrive(Drive drive) {
    this.drive = drive;
  }

  @Async
  public void uploadResume(UploadData data, byte[] uploadFile) throws GeneralSecurityException, IOException {

    LOGGER.info("Uploading file type {} of size {} to Drive",data.getContentType(),uploadFile.length);
    com.google.api.services.drive.model.File file = new com.google.api.services.drive.model.File();

    List<ParentReference> referneces = new ArrayList<ParentReference>();
    referneces.add(buildParentReference(data.getApplication().getRole()));

    file.setTitle(data.getFileName());
    file.setParents(referneces);
    file.setCopyable(true);
    file.setEditable(true);
    file.setWritersCanShare(true);
    file.setTitle(data.getFileName());

    ByteArrayInputStream inputBytes = new ByteArrayInputStream(uploadFile);

    InputStreamContent fileUpload = new InputStreamContent(data.getContentType(), new BufferedInputStream(inputBytes));
    fileUpload.setLength(uploadFile.length);

    Drive.Files.Insert request = drive.files().insert(file, fileUpload);
    request.getMediaHttpUploader().setProgressListener(getProgressListener());
    request.execute();
  }