Java, Google Drive:为什么GoogleAPI有时在授权时不给刷新令牌



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

问题是有时谷歌决定给我刷新令牌,有时它突然不给。这导致了一个问题,因为在发出请求时没有刷新令牌,我得到了401。

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

     @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"
}

我正在检查DB中的值,我可以看到每当刷新令牌为空时,我得到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;
    }

如果需要任何帮助,只需留下评论。

相关内容

  • 没有找到相关文章

最新更新