本文整理汇总了Java中com.google.api.client.auth.oauth2.AuthorizationCodeFlow.createAndStoreCredential方法的典型用法代码示例。如果您正苦于以下问题:Java AuthorizationCodeFlow.createAndStoreCredential方法的具体用法?Java AuthorizationCodeFlow.createAndStoreCredential怎么用?Java AuthorizationCodeFlow.createAndStoreCredential使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.api.client.auth.oauth2.AuthorizationCodeFlow
的用法示例。
在下文中一共展示了AuthorizationCodeFlow.createAndStoreCredential方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getService
import com.google.api.client.auth.oauth2.AuthorizationCodeFlow; //导入方法依赖的package包/类
/**
* Returns an authorized Bitbucket API service.
* @param authorizationCode authorization code received by the redirection
* endpoint
* @return authorized Bitbucket API service
* @throws IOException if an I/O exception has occurred
* @throws NullPointerException if this object has no client credentials
* @since 5.0
*/
public Service getService(String authorizationCode)
throws IOException {
AuthorizationCodeFlow flow = getAuthorizationCodeFlow(true);
AuthorizationCodeTokenRequest request
= flow.newTokenRequest(authorizationCode);
if (redirectionEndpointUri != null) {
request.setRedirectUri(redirectionEndpointUri);
}
TokenResponse tokenResponse = request.execute();
String tokenType = tokenResponse.getTokenType();
if (!tokenType.equals(BEARER_TOKEN_TYPE)) {
throw new UnknownServiceException("Unsupported token type");
}
return new RestService(
flow.createAndStoreCredential(tokenResponse, getUser()));
}
示例2: doAuth
import com.google.api.client.auth.oauth2.AuthorizationCodeFlow; //导入方法依赖的package包/类
private String doAuth(HttpServletRequest req)
throws IOException
{
String authCode = req.getParameter( "code" );
String callbackUri = AuthUtils.fullUrl( req, AuthUtils.OAUTH2_PATH );
// We need a flow no matter what to either redirect or extract information
AuthorizationCodeFlow flow = AuthUtils.buildCodeFlow();
// Without a response code, redirect to Google's authorization URI
if( authCode == null ) {
return flow.newAuthorizationUrl().setRedirectUri( callbackUri ).build();
}
// With a response code, store the user's credential, and
// set the user's ID into the session
GoogleTokenResponse tokenRes = getTokenRes( flow, authCode, callbackUri );
// Extract the Google user ID from the ID token in the auth response
String userId = getUserId( tokenRes );
// Store the user if for the session
SessionUtils.setUserId( req, userId );
// Store the credential with the user
flow.createAndStoreCredential( tokenRes, userId );
return "/";
}
示例3: doAuth
import com.google.api.client.auth.oauth2.AuthorizationCodeFlow; //导入方法依赖的package包/类
private String doAuth(HttpServletRequest req)
throws IOException
{
String authCode = req.getParameter( "code" );
String callbackUri = AuthUtils.fullUrl( req, AuthUtils.OAUTH2_PATH );
// We need a flow no matter what to either redirect or extract information
AuthorizationCodeFlow flow = AuthUtils.buildCodeFlow();
// Without a response code, redirect to Google's authorization URI
if( authCode == null ) {
return flow.newAuthorizationUrl().setRedirectUri( callbackUri ).build();
}
// With a response code, store the user's credential, and
// set the user's ID into the session
GoogleTokenResponse tokenRes = getTokenRes( flow, authCode, callbackUri );
// Extract the Google user ID from the ID token in the auth response
String userId = getUserId( tokenRes );
// Store the user if for the session
SessionUtils.setUserId( req, userId );
// START:subscribe
// Store the credential with the user
flow.createAndStoreCredential( tokenRes, userId );
// successful authorization, subscribe to notifications
LunchRoulette.subscribe( req, userId );
return "/";
// ENDT:subscribe
}
示例4: doAuth
import com.google.api.client.auth.oauth2.AuthorizationCodeFlow; //导入方法依赖的package包/类
private String doAuth(HttpServletRequest req)
throws IOException
{
String authCode = req.getParameter( "code" );
String callbackUri = AuthUtils.fullUrl( req, AuthUtils.OAUTH2_PATH );
// We need a flow no matter what to either redirect or extract information
AuthorizationCodeFlow flow = AuthUtils.buildCodeFlow();
// Without a response code, redirect to Google's authorization URI
if( authCode == null ) {
return flow.newAuthorizationUrl().setRedirectUri( callbackUri ).build();
}
// With a response code, store the user's credential, and
// set the user's ID into the session
GoogleTokenResponse tokenRes = getTokenRes( flow, authCode, callbackUri );
// Extract the Google user ID from the ID token in the auth response
String userId = getUserId( tokenRes );
// Store the user if for the session
SessionUtils.setUserId( req, userId );
// Store the credential with the user
flow.createAndStoreCredential( tokenRes, userId );
return "/";
}
示例5: doAuth
import com.google.api.client.auth.oauth2.AuthorizationCodeFlow; //导入方法依赖的package包/类
private String doAuth(HttpServletRequest req)
throws IOException
{
String authCode = req.getParameter( "code" );
String callbackUri = AuthUtils.fullUrl( req, AuthUtils.OAUTH2_PATH );
// We need a flow no matter what to either redirect or extract information
AuthorizationCodeFlow flow = AuthUtils.buildCodeFlow();
// Without a response code, redirect to Google's authorization URI
if( authCode == null ) {
return flow.newAuthorizationUrl().setRedirectUri( callbackUri ).build();
}
// With a response code, store the user's credential, and
// set the user's ID into the session
GoogleTokenResponse tokenRes = getTokenRes( flow, authCode, callbackUri );
// Extract the Google user ID from the ID token in the auth response
String userId = getUserId( tokenRes );
// Store the user if for the session
SessionUtils.setUserId( req, userId );
// START:subscribe
// Store the credential with the user
flow.createAndStoreCredential( tokenRes, userId );
// successful authorization, subscribe to notifications
LunchRoulette.subscribe( req, userId );
// successful authorization, add Lunch Roulette as a contact
LunchRoulette.addAppAsContact( req, userId );
return "/";
// ENDT:subscribe
}
示例6: doFilter
import com.google.api.client.auth.oauth2.AuthorizationCodeFlow; //导入方法依赖的package包/类
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if(request.getParameter("code") != null ) {
AuthorizationCodeFlow flow = oAuth2Util.newAuthorizationCodeFlow();
TokenResponse tokenResponse = oAuth2Util.newTokenRequest(flow,request.getParameter("code")).execute();
// Extract the Google User ID from the ID token in the auth
// response
//String userId = ((GoogleTokenResponse) tokenResponse).parseIdToken().getPayload().getUserId();
String subject = ((GoogleTokenResponse) tokenResponse).parseIdToken().getPayload().getSubject();
//String email = (String) ((GoogleTokenResponse) tokenResponse).parseIdToken().getPayload().get("email");
logger.info("Code exchange worked. User " + subject + " logged in.");
flow.createAndStoreCredential(tokenResponse, subject);
Authentication auth = new UsernamePasswordAuthenticationToken(subject, tokenResponse.getAccessToken(), (Collection<? extends GrantedAuthority>) new ArrayList<GrantedAuthority>());
authManager.authenticate(auth);
SecurityContextHolder.getContext().setAuthentication(authManager.authenticate(auth));
((HttpServletRequest)request).getSession().setAttribute(
HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY,
SecurityContextHolder.getContext());
if(auth != null){
onAuthenticationSuccess((HttpServletRequest)request,(HttpServletResponse)response,auth);
}
}
chain.doFilter(request, response);
}