当前位置: 首页 > 知识库问答 >
问题:

用Google Drive Oauth 2.0管理访问令牌

赫连心思
2023-03-14

我正在使用谷歌API Java客户端来管理从谷歌应用程序引擎访问谷歌驱动器API。

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

>

  • 一旦我登录到web应用程序,我就会从刷新令牌中获得一个访问令牌,并将其存储在会话中。我必须如何创建一个谷歌凭据对象从刷新令牌存储在数据库?

    谢了。

  • 共有1个答案

    巫煌
    2023-03-14

    如果您使用的是Drive API库,它将为您处理401异常,只要您给它一个具有访问和刷新令牌的凭据。

    以下是如何使用StoredCredential构建Credential对象。您可以使用与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);
            }
    }
    
     类似资料:
    • 我在CentOS虚拟机中安装了RabbitMQ,该虚拟机的网络适配器被定义为Bridge。我正在尝试配置RabbitMQ管理,以便通过机器的IP地址访问WebApp。配置如下:

    • 我在KeyCloak有两个客户: CP:客户端公共 抄送:已启用服务帐户和若干资源的客户端机密。 null 交换的令牌: 但是错误仍然存在,alghout aud从帐户更改为[account,cc]。

    • 我已经在Azure API管理中创建了一个API,用于从后端API获取数据。后端API使用带有10分钟后过期的访问令牌的oAuth2。使用返回的刷新令牌,您可以获得一个新的访问令牌,该令牌在另外10分钟内仍然有效。等等。 在Azure APIM的开发门户中,可以进行授权,这将使访问时间为10分钟。10分钟后,您必须再次手动进行授权,以便获得另外10分钟的访问权限。 在Azure APIM中是否有一

    • 我知道之前有人对此提出过疑问,但在尝试推送存储库时,我仍然会遇到一个恼人的错误: 在我输入我的个人访问令牌后,它会出错: 远程:2021 8月13日取消了对密码验证的支持。请使用个人访问令牌。遥控器:请看https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/了解更多信息。致命:身份验证

    • 在Firebase Firestore中,我试图只允许(自定义分配的)管理员写/更新/删除资源,为此我有以下安全规则: 我正在使用users集合中标记为管理员的用户登录: NfwIQAjfNdS85yDvd5yPVDyMTUj2是从身份验证窗格中获得的UID:

    • 我已经为PostgreSQL创建了新的Azure数据库-灵活的服务器,管理员用户为“admin\u demo”。 现在,在创建数据库之后,我完成了以下步骤: > 创建角色role\u developer;--创建新角色。 授予使用权限,在模式abc上创建以role_developer;--授予对模式中新角色的访问权限。 将架构abc中所有表的select、insert、update、delete授