使用 Google Drive Oauth 2.0 管理访问令牌

Die*_*vič 7 java oauth-2.0 google-api-java-client google-drive-api

我正在使用 Google API Java 客户端来管理从 Java 中的 Google App Engine 对 Google Drive API 的访问。

我得到一个用户访问令牌和刷新令牌,并将它们保存在我们的数据库中。虽然,我认为只有刷新令牌需要持久化。

如何管理访问令牌过期?你怎么看这个策略:

  • 登录 Web 应用程序后,我会从刷新令牌中获取访问令牌并将其存储在会话中。我必须如何从存储在数据库中的刷新令牌创建 Google 凭据对象?

  • 当我访问 Drive 操作时,如果过期,我会捕获 401 异常以重新创建 Access Token

我已经阅读了Credential 和 Credential Store,但它似乎已被弃用。现在必须使用:StoredCredential。有人有使用这个新界面的示例吗?

谢谢。

Dav*_*vid 8

如果您使用 Drive API 库,它会为您处理 401 异常,只要您为其提供带有访问和刷新令牌的凭据。

以下是如何Credential使用StoredCredential. 您可以使用不同于以下内容的实现MemoryDataStoreFactory

public class ApiCredentialManager {
    private DataStore<StoredCredential> dataStore;
    
        //Put your scopes here
        public static String[] SCOPES_ARRAY = { "https://www.googleapis.com/auth/admin.directory.user" };
    
        private ApiCredentialManager() {
    
            try {
                dataStore = MemoryDataStoreFactory.getDefaultInstance().getDataStore("credentialDatastore");
            } catch (IOException e) {
                throw new RuntimeException("Unable to create in memory credential datastore", e);
            }
        }
    
        public static ApiCredentialManager getInstance() {
            if (instance == null)
                instance = new ApiCredentialManager();
    
            return instance;
        }
    
        public Credential getCredential(String username) throws Exception {
            try {
                GoogleCredential credential = new GoogleCredential.Builder()
                        .setTransport(new NetHttpTransport())
                        .setJsonFactory(new JacksonFactory())
                        .addRefreshListener(
                                new DataStoreCredentialRefreshListener(
                                        username, dataStore))
                        .build();
                
                if(dataStore.containsKey(username)){
                    StoredCredential storedCredential = dataStore.get(username);
                    credential.setAccessToken(storedCredential.getAccessToken());
                    credential.setRefreshToken(storedCredential.getRefreshToken());
                }else{
                    //Do something of your own here to obtain the access token.
                    //Most usually redirect the user to the OAuth page
                }
                
                return credential;
            } catch (GeneralSecurityException e) {
                throw new Exception("isuue while setting credentials", e);
            } catch (IOException e) {
                e.printStackTrace();
                throw new Exception("isuue while setting credentials", e);
            }
        }
        
        //Call this when you've obtained the access token and refresh token from Google
        public void saveCredential(String username, Credential credential){
            StoredCredential storedCredential = new StoredCredential();
            storedCredential.setAccessToken(credential.getAccessToken());
            storedCredential.setRefreshToken(credential.getRefreshToken());
            dataStore.set(username, storedCredential);
        }
}
Run Code Online (Sandbox Code Playgroud)