2012-07-01 65 views
0

我讀儘管谷歌API和它說:谷歌的oauth2 - 召喚令牌

「的第一步是基於已知的用戶ID調用loadCredential(字符串),以檢查最終用戶的憑據如果沒有,請調用newAuthorizationUrl()並指導最終用戶的瀏覽器訪問授權頁面,然後Web瀏覽器將使用「code」查詢參數重定向到重定向URL,然後可用於請求訪問令牌使用newTokenRequest(String)。最後,使用createAndStoreCredential(TokenResponse,String)來存儲和獲取用於訪問受保護資源的憑證。「

因此,我將代碼行放入我的代碼中,即使使用正確的導入語句也會失敗。 「類型不匹配:不能轉換從證書到AuthorizationCodeRequestUrl」

AuthorizationCodeRequestUrl bob2 = AuthorizationCodeFlow.loadCredential("test"); or 
GoogleAuthorizationCodeFlow bob2 = AuthorizationCodeFlow.loadCredential("test"); 

http://javadoc.google-oauth-java-client.googlecode.com/hg/1.7.0-beta/com/google/api/client/auth/oauth2/AuthorizationCodeFlow.html?is-external=true#loadCredential%28java.lang.String%29

此外,如果任何人有OAuth2用戶實現對谷歌的任何很好的例子,請張貼。我能找到的唯一一個我能理解的(請參閱下面的鏈接)似乎已經摺舊了,我想使用最新版本。 https://developers.google.com/google-apps/tasks/oauth-authorization-callback-handler

+0

您不建議使用與developers.google.com的鏈接。 – Amber

回答

0

sHere是我使用的代碼。這不是一個完整的例子,但它應該給某個人這個想法。

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());