[英]Google Oauth2 - Call to Token
我正在閱讀Google API,它說:
“第一步是根據已知的用戶ID調用loadCredential(String)來檢查最終用戶的憑據是否已知。否則,請調用newAuthorizationUrl()並將最終用戶的瀏覽器定向到授權頁面。Web然后,瀏覽器將使用“代碼”查詢參數重定向到重定向URL,然后可以使用newTokenRequest(String)請求該令牌來訪問訪問令牌。最后,使用createAndStoreCredential(TokenResponse,String)存儲和獲取訪問受保護資源的憑據”。
因此,我將代碼行放入我的代碼中,即使使用正確的導入語句,它也會失敗。 “類型不匹配:無法從憑據轉換為AuthorizationCodeRequestUrl”
AuthorizationCodeRequestUrl bob2 = AuthorizationCodeFlow.loadCredential("test"); or
GoogleAuthorizationCodeFlow bob2 = AuthorizationCodeFlow.loadCredential("test");
另外,如果有人對Google的Oauth2實現有很好的例子,請發布它們。 我發現我能理解的唯一一個(參見下面的鏈接)似乎已過時,並且我想使用最新版本。 https://developers.google.com/google-apps/tasks/oauth-authorization-callback-handler
這是我使用的代碼。 這不是一個完整的示例,但是應該可以使某人知道。
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
// response.getWriter().println(" User " + user);
if (user == null) {
response.sendRedirect(userService.createLoginURL(getFullRequestUrl(request)));
return;
}
然后無論如何我都會獲得授權碼。 (錯誤編碼。)
response.sendRedirect(new GoogleAuthorizationRequestUrl(oauthProperties2.getClientId(), OAuthCodeCallbackHandlerServlet.getOAuthCodeCallbackHandlerUrl(request), oauthProperties2.getScopesAsString()).build());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.