Java,Google云端硬盘:为什么GoogleAPI有时会在授权时不提供刷新令牌?

时间:2015-09-15 07:42:26

标签: java file-upload google-api google-drive-api

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

问题是,Google有时会决定给我刷新令牌,有时会突然出现,但事实并非如此。这导致了一个问题,因为在发出请求时没有刷新令牌,我得到了401.

那么,有时我没有获得刷新令牌,这种异想天开的本质是什么? 这是我的authorize,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,否则我可以上传文件。但是使用的代码总是一样的。你能帮忙的话,我会很高兴。非常感谢。

1 个答案:

答案 0 :(得分:0)

如果有人遇到问题,那么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;
    }

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