我正在尝试通过 Spring Social 使用 google 凭据实现 Google oAuth2 API 以登录到我的 web 应用程序。
对谷歌的查询如下
googleConnectionFactory = new GoogleConnectionFactory(myKey, mySecret);
oauthOperations = googleConnectionFactory.getOAuthOperations();
final String redirectUri = "http://localhost/googleCallback";
final OAuth2Parameters params = new OAuth2Parameters();
params.setRedirectUri(redirectUri);
params.setScope("https://www.googleapis.com/auth/userinfo.profile");
final String authorizeUrl = oauthOperations.buildAuthorizeUrl(
GrantType.AUTHORIZATION_CODE, params);
response.sendRedirect(authorizeUrl);
一旦请求,我将被带到 Google 登录页面。网址显示scope=https://www.googleapis.com/auth/userinfo.profile
登录后,用户将被重定向回我的 webapp,并调用以下方法
final String callbackUrl = "http://localhost/googleCallback";
final AccessGrant accessGrant = oauthOperations.exchangeForAccess(code,
callbackUrl, null);
// THIS CRASHES WITH 401
final Connection<Google> connection = googleConnectionFactory
.createConnection(accessGrant);
// THIS CRASHES TOO WITH 401
new GoogleTemplate(accessGrant.getAccessToken()).userOperations().getUserProfile();
我错过了什么吗?