【问题标题】:OAuth and google plus apiOAuth 和 google plus api
【发布时间】:2011-10-17 14:28:44
【问题描述】:

我在我的一个 gaelyk 应用程序中使用了 google-start-project 的代码。这是 OAuth 2.0 授权过程的常规代码。与 twitter 不同,每当应用程序请求授权时,用户必须允许应用程序继续运行,我认为这很奇怪。我犯了一些错误吗?

    // 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("/");
}

【问题讨论】:

  • redirect_uri 的值是多少?我在这里遇到了问题。

标签: java groovy oauth oauth-2.0 google-plus


【解决方案1】:

不,你做得对。我认为 Google+ 不支持身份验证 - 仅支持授权。这就是 OAuth 的理念——授权用户,而不是对他们进行身份验证。对于身份验证,您可以使用OpenID

顺便说一句,starter项目有点复杂,不支持maven,google添加新的API方法时也没有及时更新。因此我创建了this project,您可以检查它是否适合您。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    • 1970-01-01
    • 2011-10-29
    • 1970-01-01
    • 1970-01-01
    • 2014-04-18
    相关资源
    最近更新 更多