Java Examples for com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeRequestUrl
The following java examples will help you to understand the usage of com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeRequestUrl. These source code samples are taken from different open source projects.
Example 1
| Project: BlinkCoder-master File: UserController.java View source code |
public void login() {
String state = new BigInteger(130, new SecureRandom()).toString(32);
getSession().setAttribute("state", state);
GoogleAuthorizationCodeRequestUrl url = (GoogleAuthorizationCodeRequestUrl) flow.newAuthorizationUrl();
url.setRedirectUri(CALLBACK_URI).setState(state).build();
redirect(url.toString());
}Example 2
| Project: Pdf-Reviewer-master File: ImageUtils.java View source code |
//Can be used to regenerate a refresh token
@SuppressWarnings("unused")
private static String getRefreshToken() throws IOException {
String client_id = System.getenv("PICASSA_CLIENT_ID");
String client_secret = System.getenv("PICASSA_CLIENT_SECRET");
// Adapted from http://stackoverflow.com/a/14499390/1447621
String redirect_uri = "http://localhost";
String scope = "http://picasaweb.google.com/data/";
List<String> scopes;
HttpTransport transport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
scopes = new LinkedList<String>();
scopes.add(scope);
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(transport, jsonFactory, client_id, client_secret, scopes).build();
GoogleAuthorizationCodeRequestUrl url = flow.newAuthorizationUrl();
url.setRedirectUri(redirect_uri);
url.setApprovalPrompt("force");
url.setAccessType("offline");
String authorize_url = url.build();
// paste into browser to get code
System.out.println("Put this url into your browser and paste in the access token:");
System.out.println(authorize_url);
Scanner scanner = new Scanner(System.in);
String code = scanner.nextLine();
scanner.close();
flow = new GoogleAuthorizationCodeFlow.Builder(transport, jsonFactory, client_id, client_secret, scopes).build();
GoogleTokenResponse res = flow.newTokenRequest(code).setRedirectUri(redirect_uri).execute();
String refreshToken = res.getRefreshToken();
String accessToken = res.getAccessToken();
System.out.println("refresh:");
System.out.println(refreshToken);
System.out.println("access:");
System.out.println(accessToken);
return refreshToken;
}Example 3
| Project: OpenRefine-master File: GDataExtension.java View source code |
public static String getAuthorizationUrl(ButterflyModule module, HttpServletRequest request) throws MalformedURLException {
String authorizedUrl = makeRedirectUrl(module, request);
// New Oauth2
GoogleAuthorizationCodeRequestUrl url = new GoogleAuthorizationCodeRequestUrl(CLIENT_ID, // execution continues at authorized on redirect
authorizedUrl, Arrays.asList("https://www.googleapis.com/auth/fusiontables", // create new spreadsheets
"https://www.googleapis.com/auth/drive", "https://spreadsheets.google.com/feeds"));
return url.toString();
}Example 4
| Project: MongoDBClusterTool-master File: GoogleComputeEngineAuthImpl.java View source code |
public String getRefreshTokenURL(final String redirectUri) throws GoogleComputeEngineException {
GoogleAuthorizationCodeRequestUrl url = authorizationCodeFlow.newAuthorizationUrl();
//GoogleAuthorizationCodeRequestUrl url = new GoogleAuthorizationCodeRequestUrl(clientId, redirectUri, SCOPES).setAccessType("offline");
url.setRedirectUri(redirectUri);
url.setApprovalPrompt("force");
url.setAccessType("offline");
url.setClientId(clientId);
url.setScopes(SCOPES);
return url.build();
}Example 5
| Project: SecureShareLib-master File: YoutubeLoginActivity.java View source code |
@SuppressLint("SetJavaScriptEnabled")
public void login() {
// check for tor settings and set proxy
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean useTor = settings.getBoolean("pusetor", false);
mWebview = new WebView(this);
if (useTor) {
Timber.d("user selected \"use tor\"");
if ((!OrbotHelper.isOrbotInstalled(this)) || (!OrbotHelper.isOrbotRunning(this))) {
Timber.e("user selected \"use tor\" but orbot is not installed or not running");
return;
} else {
try {
WebkitProxy.setProxy("android.app.Application", getApplicationContext(), mWebview, Util.ORBOT_HOST, Util.ORBOT_HTTP_PORT);
} catch (Exception e) {
Timber.e("user selected \"use tor\" but an exception was thrown while setting the proxy: " + e.getLocalizedMessage());
return;
}
}
} else {
Timber.d("user selected \"don't use tor\"");
}
mWebview.getSettings().setJavaScriptEnabled(true);
mWebview.setVisibility(View.VISIBLE);
setContentView(mWebview);
List<String> scopes = new ArrayList<String>();
scopes.add(YouTubeScopes.YOUTUBE_UPLOAD);
scopes.add(YOUTUBE_EMAIL_SCOPE);
String authUrl = new GoogleAuthorizationCodeRequestUrl(CLIENT_ID, REDIRECT_URI, scopes).build();
// WebViewClient must be set BEFORE calling loadUrl!
mWebview.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap bitmap) {
}
@Override
public void onPageFinished(WebView view, String url) {
if (url.startsWith(REDIRECT_URI)) {
if (url.indexOf("code=") != -1) {
if (mReturnedWebCode == null) {
mReturnedWebCode = extractCodeFromUrl(url);
view.setVisibility(View.INVISIBLE);
new Thread(YoutubeLoginActivity.this).start();
//must be done on the main thread
Util.clearWebviewAndCookies(mWebview, YoutubeLoginActivity.this);
}
} else if (url.indexOf("error=") != -1) {
view.setVisibility(View.INVISIBLE);
mAccessResult = RESULT_CANCELED;
}
}
}
private String extractCodeFromUrl(String url) {
return url.substring(REDIRECT_URI.length() + 7, url.length());
}
});
mWebview.loadUrl(authUrl);
}Example 6
| Project: google-sites-liberation-master File: GuiMain.java View source code |
private void openBrowserAndGetToken() {
// Step 1: Authorize -->
String authorizationUrl = new GoogleAuthorizationCodeRequestUrl(CLIENT_ID, REDIRECT_URI, SCOPES).build();
try {
// Point or redirect your user to the authorizationUrl.
java.awt.Desktop.getDesktop().browse(new URI(authorizationUrl));
} catch (Exception e) {
LOGGER.warning("Unable to open browser: " + e.toString());
LOGGER.info("Go to: " + authorizationUrl);
JOptionPane.showMessageDialog(optionsFrame, "Cannot open browser. Check the console for the necessary URL.", "Error", JOptionPane.ERROR_MESSAGE);
}
// End of Step 1 <--
}Example 7
| Project: liferay-portal-master File: GoogleAuthorizationImpl.java View source code |
@Override
public String getLoginRedirect(long companyId, String returnRequestUri, List<String> scopes) throws Exception {
GoogleAuthorizationCodeFlow googleAuthorizationCodeFlow = getGoogleAuthorizationCodeFlow(companyId, scopes);
GoogleAuthorizationCodeRequestUrl googleAuthorizationCodeRequestUrl = googleAuthorizationCodeFlow.newAuthorizationUrl();
googleAuthorizationCodeRequestUrl = googleAuthorizationCodeRequestUrl.setRedirectUri(returnRequestUri);
return googleAuthorizationCodeRequestUrl.build();
}Example 8
| Project: google-login-hook-master File: GoogleOAuth.java View source code |
public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
String cmd = ParamUtil.getString(request, Constants.CMD);
String redirectUri = PortalUtil.getPortalURL(request) + _REDIRECT_URI;
if (cmd.equals("login")) {
GoogleAuthorizationCodeFlow flow = getFlow();
GoogleAuthorizationCodeRequestUrl googleAuthorizationCodeRequestUrl = flow.newAuthorizationUrl();
googleAuthorizationCodeRequestUrl.setRedirectUri(redirectUri);
String url = googleAuthorizationCodeRequestUrl.build();
response.sendRedirect(url);
} else if (cmd.equals("token")) {
HttpSession session = request.getSession();
String code = ParamUtil.getString(request, "code");
if (Validator.isNotNull(code)) {
Credential credential = exchangeCode(code, redirectUri);
Userinfo userinfo = getUserInfo(credential);
User user = setGoogleCredentials(session, themeDisplay.getCompanyId(), userinfo);
if ((user != null) && (user.getStatus() == WorkflowConstants.STATUS_INCOMPLETE)) {
redirectUpdateAccount(request, response, user);
return null;
}
PortletURL portletURL = PortletURLFactoryUtil.create(request, PortletKeys.FAST_LOGIN, themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
portletURL.setWindowState(LiferayWindowState.POP_UP);
portletURL.setParameter("struts_action", "/login/login_redirect");
response.sendRedirect(portletURL.toString());
}
}
return null;
}Example 9
| Project: picketlink-master File: GoogleProcessor.java View source code |
protected InteractionState initialInteraction(HttpServletRequest request, HttpServletResponse response, Set<String> scopes) throws IOException {
String verificationState = generateSecureString();
String authorizeUrl = new GoogleAuthorizationCodeRequestUrl(clientID, returnURL, scopes).setState(verificationState).setAccessType(accessType).build();
if (log.isTraceEnabled()) {
log.trace("Starting OAuth2 interaction with Google+");
log.trace("URL to send to Google+: " + authorizeUrl);
}
HttpSession session = request.getSession();
session.setAttribute(GoogleConstants.ATTRIBUTE_VERIFICATION_STATE, verificationState);
session.setAttribute(GoogleConstants.ATTRIBUTE_AUTH_STATE, InteractionState.State.AUTH.name());
response.sendRedirect(authorizeUrl);
return new InteractionState(InteractionState.State.AUTH, null);
}Example 10
| Project: gatein-portal-master File: GoogleProcessorImpl.java View source code |
protected InteractionState<GoogleAccessTokenContext> initialInteraction(HttpServletRequest request, HttpServletResponse response, Set<String> scopes) throws IOException {
String verificationState = String.valueOf(secureRandomService.getSecureRandom().nextLong());
String authorizeUrl = new GoogleAuthorizationCodeRequestUrl(clientID, redirectURL, scopes).setState(verificationState).setAccessType(accessType).build();
if (log.isTraceEnabled()) {
log.trace("Starting OAuth2 interaction with Google+");
log.trace("URL to send to Google+: " + authorizeUrl);
}
HttpSession session = request.getSession();
session.setAttribute(OAuthConstants.ATTRIBUTE_VERIFICATION_STATE, verificationState);
session.setAttribute(OAuthConstants.ATTRIBUTE_AUTH_STATE, InteractionState.State.AUTH.name());
response.sendRedirect(authorizeUrl);
return new InteractionState<GoogleAccessTokenContext>(InteractionState.State.AUTH, null);
}Example 11
| Project: google-docs-master File: GoogleDocsServiceImpl.java View source code |
/**
* The oauth authentication url
*
* @param state the value of the oauth state parameter to be passed in the authentication url
* @return The complete oauth authentication url
*/
public String getAuthenticateUrl(String state) throws IOException {
String authenticateUrl = null;
if (state != null) {
GoogleAuthorizationCodeRequestUrl urlBuilder = null;
if (StringUtils.isBlank(getGoogleUserName())) {
urlBuilder = getFlow().newAuthorizationUrl().setRedirectUri(GoogleDocsConstants.REDIRECT_URI).setState(state);
} else {
urlBuilder = getFlow().newAuthorizationUrl().setRedirectUri(GoogleDocsConstants.REDIRECT_URI).setState(state).set("login_hint", getGoogleUserName());
}
authenticateUrl = urlBuilder.build();
}
log.debug("Authentication URL: " + authenticateUrl);
return authenticateUrl;
}Example 12
| Project: XCoLab-master File: GoogleAuthHelper.java View source code |
/**
* Builds a login URL based on client ID, secret, callback URI, and scope
*/
public String buildLoginUrl() {
final GoogleAuthorizationCodeRequestUrl url = flow.newAuthorizationUrl();
return url.setRedirectUri(redirectUri).setState(stateToken).build();
}Example 13
| Project: drivemarks-master File: CredentialManager.java View source code |
/**
* Generates a consent page url.
* @return A consent page url string for user redirection.
*/
public String getAuthorizationUrl() {
GoogleAuthorizationCodeRequestUrl urlBuilder = new GoogleAuthorizationCodeRequestUrl(clientSecrets.getWeb().getClientId(), clientSecrets.getWeb().getRedirectUris().get(0), SCOPES);
;
return urlBuilder.build();
}Example 14
| Project: loli.io-master File: GDriveAPI.java View source code |
/**
* Retrieve the authorization URL.
*
* @param emailAddress User's e-mail address.
* @param state State for the authorization URL.
* @return Authorization URL to redirect the user to.
* @throws IOException Unable to load client_secrets.json.
*/
public static String getAuthorizationUrl(String emailAddress, String state) throws IOException {
GoogleAuthorizationCodeRequestUrl urlBuilder = getFlow().newAuthorizationUrl().setRedirectUri(REDIRECT_URI).setState(state);
urlBuilder.set("user_id", emailAddress);
return urlBuilder.build();
}