2011-10-17 106 views
2

我正在使用谷歌啓動項目的代碼到我的一個gaelyk應用程序。這是OAuth 2.0授權流程的Groovy編碼。與twitter不同,只要應用程序要求授權,用戶必須允許該應用程序繼續,我認爲這很奇怪。我犯了一些錯誤?OAuth和谷歌加API a

// Check for an error returned by OAuth 
if (params.error) { 
    response.setContentType("text/plain"); 
    out.println("There was a problem during authentication: " + error); 
    log.severe("There was a problem during authentication: " + error); 
    return; 
} 

// When we're redirected back from the OAuth 2.0 grant page, a code will be supplied in a GET parameter named 'code' 

if (!params.code) { 
    // Now that we have the OAuth 2.0 code, we must exchange it for a token to make API requests. 

    // Build the authorization URL 
    AuthorizationRequestUrl authorizeUrl = new GoogleAuthorizationRequestUrl(
      CLIENT_ID, 
      REDIRECT_URI, 
      SCOPES 
     ); 
    authorizeUrl.redirectUri = REDIRECT_URI; 
    authorizeUrl.scope = SCOPES; 
    String authorizationUrl = authorizeUrl.build(); 

    log.info("Redirecting browser for OAuth 2.0 authorization to " + authorizationUrl); 
    response.sendRedirect(authorizationUrl); 
    return; 
} else { 
    log.info("Exchanging OAuth code for access token using server side call"); 

    AccessTokenResponse accessTokenResponse = new GoogleAccessTokenRequest.GoogleAuthorizationCodeGrant(
      new NetHttpTransport(), 
      new GsonFactory(), 
      CLIENT_ID, 
      CLIENT_SECRET, 
      params.code, 
      REDIRECT_URI 
     ).execute(); 

    log.info("Storing authentication token into the session"); 
    request.session.accessToken = accessTokenResponse.accessToken 
    request.session.refreshToken = accessTokenResponse.refreshToken 

    //The authentication is all done! Redirect back to the samples index so you can play with them. 
    response.sendRedirect("/"); 
} 
+0

redirect_uri的值是多少?我在這裏遇到問題。 –

回答

0

不,你做得對。我認爲Google+不支持僅限身份驗證授權。 OAuth的想法 - 授權用戶,而不是驗證它們。要進行身份驗證,您可以使用OpenID

順便說一句,初學者項目有點複雜,不支持maven,並且在google添加新的API方法時不及時更新。因此我創建了this project,你可以檢查它是否適合你。