【发布时间】: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