Java, Google 驱动:为什么 GoogleAPI 有时在授权时不提供刷新令牌?

Java, Google Drive : Why does GoogleAPI sometime does not give refresh token when authorized?

我有一个 Spring-MVC 项目,我在其中集成了 Google 驱动器功能以将附件上传到 google 驱动器。为此,我使用了他们的示例代码并对其进行了修改。现在,每当我授权时,我都会获得一个 Credential 对象,并将访问令牌和刷新令牌保存在数据库中。

问题是有时 Google 决定给我刷新令牌,但有时却出乎意料地没有。这导致了一个问题,因为在发出请求时没有刷新令牌,我收到了 401。

那么,当我有时没有获得刷新令牌时,这种异想天开的性质是什么? 这是我的授权、storeCredentials、gettingStoredCredentials 和保存文件的代码。

     @Override
        public Credential authorize() throws IOException {
            InputStream in =
                    DriveQuickstartImpl.class.getResourceAsStream("/client_secret.json");
            GoogleClientSecrets clientSecrets =
                    GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

            GoogleAuthorizationCodeFlow flow =
                    new GoogleAuthorizationCodeFlow.Builder(
                            HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                            .setDataStoreFactory(DATA_STORE_FACTORY)
                            .setAccessType("offline")
                            .build();
            Credential credential = new AuthorizationCodeInstalledApp(
                    flow, new com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver()).authorize("user");
            if (credential != null) {
                storeCredentials(credential);
                return credential;
            }
            return null;
        }

 @Override
    public void storeCredentials(Credential credential) {
        Person person = this.personService.getCurrentlyAuthenticatedUser();
        if (!(credential == null)) {
            person.setGoogleDrive(true);
            this.personService.updatePerson(person);
            GoogleDrive googleDrive = new GoogleDrive();
            googleDrive.setAccessToken(credential.getAccessToken());
            googleDrive.setRefreshToken(credential.getRefreshToken());
            this.googleDriveService.saveCredentials(googleDrive, person.getId());
        }
    }

  private Credential getStoredCredentials(Long groupAccountid) {
        try {
            GroupAccount groupAccount = this.groupAccountService.getGroupById(groupAccountid);
            Person person = this.personService.findPersonByUsername(groupAccount.getAdminUsername());
            if (person.isGoogleDrive()) {
                GoogleDrive googleDrive = this.googleDriveService.getUsersGoogleDrive(person.getId());
                GoogleCredential credential = new GoogleCredential.Builder().setJsonFactory(JSON_FACTORY)
                        .setTransport(HTTP_TRANSPORT).setClientSecrets(clientid, clientsecret).build();
                credential.setAccessToken(googleDrive.getAccessToken());
                credential.setRefreshToken(googleDrive.getRefreshToken());
                return credential;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;

    }


 @Override
    public File insertFile(MultipartFile multipartFile, int noteid, Long groupAccountId, String folderId) {
        try {
            GroupAccount groupAccount = this.groupAccountService.getGroupById(groupAccountId);
            Person person = this.personService.findPersonByUsername(groupAccount.getAdminUsername());
            if (person.isGoogleDrive()) {
                Credential credential = getStoredCredentials(groupAccountId);
                Drive driveService = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, null).
                        setApplicationName(APPLICATION_NAME).
                        setHttpRequestInitializer(credential).build();

                File body = new File();
                body.setTitle(multipartFile.getOriginalFilename());
                body.setMimeType(multipartFile.getContentType());
                body.setOriginalFilename(multipartFile.getOriginalFilename());

                body.setParents(Arrays.asList(new ParentReference().setId(folderId)));
                InputStreamContent mediaContent = new InputStreamContent(multipartFile.getContentType(),
                        new BufferedInputStream(multipartFile.getInputStream()));
                try {
                    File file = driveService.files().insert(body, mediaContent).execute();
                    this.groupAttachmentsService.addGoogleDriveAttachment(file.getWebContentLink(), multipartFile.getOriginalFilename(),
                            file.getFileSize(), noteid, file.getId(), multipartFile);
                    insertPermission(driveService, file.getId(), "default", "anyone", "reader");
                    return file;
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            }
        } catch (Exception e) {
            return null;
        }

        return null;
    }

错误日志:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
{
  "code" : 401,
  "errors" : [ {
    "domain" : "global",
    "location" : "Authorization",
    "locationType" : "header",
    "message" : "Invalid Credentials",
    "reason" : "authError"
  } ],
  "message" : "Invalid Credentials"
}

我正在检查数据库中的值,我可以看到每当刷新令牌为空时,我都会收到 401,否则我可以上传文件。但是使用的代码总是一样的。你能帮忙的话,我会很高兴。非常感谢。

万一有人遇到问题,则需要将 setApprovalPrompt 设置为 'force' 并将 setAccessType 设置为 'offline'。可以将授权码更改为我在下面给出的内容:

@Override
    public Credential authorize() throws IOException {
        InputStream in =
                DriveQuickstartImpl.class.getResourceAsStream("/client_secret.json");
        GoogleClientSecrets clientSecrets =
                GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

        GoogleAuthorizationCodeFlow flow =
                new GoogleAuthorizationCodeFlow.Builder(
                        HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                        .setDataStoreFactory(DATA_STORE_FACTORY)
                        .setAccessType("offline")
                        .setApprovalPrompt("force")
                        .build();
        Credential credential = new AuthorizationCodeInstalledApp(
                flow, new com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver()).authorize("user");
        if (credential != null) {
            storeCredentials(credential);
            return credential;
        }
        return null;
    }

如果需要任何帮助,请发表评论。