本文整理汇总了Java中com.google.api.client.auth.oauth2.AuthorizationCodeResponseUrl类的典型用法代码示例。如果您正苦于以下问题:Java AuthorizationCodeResponseUrl类的具体用法?Java AuthorizationCodeResponseUrl怎么用?Java AuthorizationCodeResponseUrl使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
AuthorizationCodeResponseUrl类属于com.google.api.client.auth.oauth2包,在下文中一共展示了AuthorizationCodeResponseUrl类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onError
import com.google.api.client.auth.oauth2.AuthorizationCodeResponseUrl; //导入依赖的package包/类
@Override
protected void onError(HttpServletRequest req, HttpServletResponse resp, AuthorizationCodeResponseUrl errorResponse) throws IOException {
String callbackUrl = (String) req.getSession().getAttribute("callbackUrl");
if(callbackUrl == null) {
BufferedOutputStream bos = new BufferedOutputStream(resp.getOutputStream());
PrintWriter pw = new PrintWriter(bos);
resp.setContentType("application/json");
resp.setHeader("access-control-allow-origin", "*");
pw.print(String.format(
"{\"error\": {\"code\": \"%s\", \"errorMessage\": \"%s\", \"errorDescription\": \"%s\", \"errorUri\": \"%s\", \"state\": \"%s\"}}",
errorResponse.getCode(),
errorResponse.getError(),
errorResponse.getErrorDescription(),
errorResponse.getErrorUri(),
errorResponse.getState()
));
pw.close();
} else {
resp.sendRedirect(callbackUrl + "?notLoggedIn=true");
req.getSession().removeAttribute("callbackUrl");
}
}
示例2: doGet
import com.google.api.client.auth.oauth2.AuthorizationCodeResponseUrl; //导入依赖的package包/类
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String requestUrl = req.getRequestURL().append('?').append(req.getQueryString()).toString();
AuthorizationCodeResponseUrl authorizationCodeResponseUrl =
new AuthorizationCodeResponseUrl(requestUrl);
if (authorizationCodeResponseUrl.getError() != null) {
throw new IOException("Received error: " + authorizationCodeResponseUrl.getError());
} else {
// Authenticate the user and store their credential with their user ID (derived from
// the request).
HttpSession httpSession = req.getSession(true);
if (httpSession.getAttribute(Server.USER_SESSION_ID) == null) {
httpSession.setAttribute(Server.USER_SESSION_ID, new Random().nextLong());
}
String authorizationCode = authorizationCodeResponseUrl.getCode();
oAuth2Credentials.authenticate(authorizationCode, httpSession.getAttribute(Server.USER_SESSION_ID).toString());
}
resp.sendRedirect("/");
}
示例3: onError
import com.google.api.client.auth.oauth2.AuthorizationCodeResponseUrl; //导入依赖的package包/类
@Override
protected void onError(HttpServletRequest req, HttpServletResponse resp, AuthorizationCodeResponseUrl errorResponse) throws ServletException, IOException {
LOG.severe(errorResponse.getError() + ": " + errorResponse.getErrorDescription());
GenericUrl url = new GenericUrl(req.getRequestURL().toString());
url.setRawPath(SessionManager.SIGN_IN_URL);
resp.sendRedirect(url.build());
}
示例4: getUserFromUrl
import com.google.api.client.auth.oauth2.AuthorizationCodeResponseUrl; //导入依赖的package包/类
private String getUserFromUrl(AuthorizationCodeResponseUrl authorizationCodeResponseUrl)
throws IOException {
String state = authorizationCodeResponseUrl.getState();
if (!(state == null || state.isEmpty())) {
String decoded = URLDecoder.decode(state, "UTF-8");
String[] items = decoded.split("&");
for (String str : items) {
if (str.startsWith("userId=")) {
return str.substring(7, str.length());
}
}
}
return null;
}
示例5: onError
import com.google.api.client.auth.oauth2.AuthorizationCodeResponseUrl; //导入依赖的package包/类
@Override
protected void onError(HttpServletRequest request, HttpServletResponse response,
AuthorizationCodeResponseUrl errorResponse) {
try {
response.sendRedirect("/denied");
} catch (IOException e) {
LOG.warning("Cannot redirect on authorization error.");
}
}
示例6: onError
import com.google.api.client.auth.oauth2.AuthorizationCodeResponseUrl; //导入依赖的package包/类
@Override
protected void onError(
HttpServletRequest req, HttpServletResponse resp, AuthorizationCodeResponseUrl errorResponse)
throws ServletException, IOException {
resp.sendRedirect("/unauthorized.html");
}
示例7: callback
import com.google.api.client.auth.oauth2.AuthorizationCodeResponseUrl; //导入依赖的package包/类
/**
* Process callback request.
*
* @param requestUrl request URI. URI should contain authorization code generated by authorization
* server
* @param scopes specify exactly what type of access needed. This list must be exactly the same as
* list passed to the method {@link #getAuthenticateUrl(URL, java.util.List)}
* @return id of authenticated user
* @throws OAuthAuthenticationException if authentication failed or <code>requestUrl</code> does
* not contain required parameters, e.g. 'code'
*/
public String callback(URL requestUrl, List<String> scopes) throws OAuthAuthenticationException {
if (!isConfigured()) {
throw new OAuthAuthenticationException("Authenticator is not configured");
}
AuthorizationCodeResponseUrl authorizationCodeResponseUrl =
new AuthorizationCodeResponseUrl(requestUrl.toString());
final String error = authorizationCodeResponseUrl.getError();
if (error != null) {
throw new OAuthAuthenticationException("Authentication failed: " + error);
}
final String code = authorizationCodeResponseUrl.getCode();
if (code == null) {
throw new OAuthAuthenticationException("Missing authorization code. ");
}
try {
TokenResponse tokenResponse =
flow.newTokenRequest(code)
.setRequestInitializer(
request -> {
if (request.getParser() == null) {
request.setParser(flow.getJsonFactory().createJsonObjectParser());
}
request.getHeaders().setAccept(MediaType.APPLICATION_JSON);
})
.setRedirectUri(findRedirectUrl(requestUrl))
.setScopes(scopes)
.execute();
String userId = getUserFromUrl(authorizationCodeResponseUrl);
if (userId == null) {
userId =
getUser(newDto(OAuthToken.class).withToken(tokenResponse.getAccessToken())).getId();
}
flow.createAndStoreCredential(tokenResponse, userId);
return userId;
} catch (IOException ioe) {
throw new OAuthAuthenticationException(ioe.getMessage());
}
}
示例8: onError
import com.google.api.client.auth.oauth2.AuthorizationCodeResponseUrl; //导入依赖的package包/类
@Override
protected void onError(HttpServletRequest req, HttpServletResponse resp,
AuthorizationCodeResponseUrl errorResponse)
throws ServletException, IOException {
// handle error
}
示例9: run
import com.google.api.client.auth.oauth2.AuthorizationCodeResponseUrl; //导入依赖的package包/类
@Override
public Navigation run() throws Exception {
StringBuffer requestURL = request.getRequestURL();
if(request.getQueryString() != null) {
requestURL.append("?").append(request.getQueryString());
}
AuthorizationCodeResponseUrl responseUrl = new AuthorizationCodeResponseUrl(requestURL.toString());
String code = responseUrl.getCode();
if(responseUrl.getError() != null) {
logger.severe(responseUrl.getError());
return null;
}
if (code == null) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return null;
}
GoogleAuthorizationCodeFlow flow = FusionService.newFlow();
String redirectUri = FusionService.getRedirectUri(request);
GoogleTokenResponse res = flow.newTokenRequest(code).setRedirectUri(redirectUri).execute();
flow.createAndStoreCredential(res, FusionService.getUserId());
logger.fine("success");
return null;
}