我正在使用 glcient-java 库来处理 Google oAuth Flow。按照文档中的建议,我有两个分别扩展 AbstractAuthorizationCodeServlet 和 AbstractAuthorizationCodeCallbackServlet 的类。流程进行得很顺利。我现在正在尝试将我在 onSuccess 方法中收到的凭据对象持久化/存储到数据库中。情况是,用户(由唯一 ID 标识)可以选择连接一个或多个谷歌帐户。我想要的是每次通过 oAuth Flow 和 onSuccess 方法连接一个新的 Google 帐户时,我应该能够在数据库中添加一行,其中 uniqueID、GoogleAccountEmailID、AccessToken、RefreshToken 存储在表中。
以下是我的重要实现方法:
@Override
protected AuthorizationCodeFlow initializeFlow() throws ServletException,
IOException {
return
new GoogleAuthorizationCodeFlow.Builder(
new NetHttpTransport(), JacksonFactory.getDefaultInstance(),Constants.GOOGLE_CLIENT_ID, Constants.GOOGLE_CLIENT_SECRET,Constants.SCOPES)
.setAccessType("offline")
.setApprovalPrompt("force")
.build();
}
@Override
protected void onSuccess(HttpServletRequest req, HttpServletResponse resp,
Credential credential) throws ServletException, IOException {
// Creates a new Gmail API client.
System.out.print("Successfully Authenticated");
System.out.println("Access Token"+credential.getAccessToken());
System.out.println("Ref Token"+credential.getRefreshToken());
System.out.println("");
/*Here i want to store the accessToken,RefreshToken,uniqueID,credentialEmailID*/
}
另外,我应该如何实现我的 getUserID() 方法来给我从 oauth 令牌响应或凭据对象中提取的 emailID。
请帮忙。