Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.net.URI;
- import java.net.URISyntaxException;
- import java.util.Collections;
- import javax.inject.Inject;
- import com.google.api.client.auth.oauth2.AuthorizationCodeRequestUrl;
- import com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest;
- import com.google.api.client.auth.oauth2.BearerToken;
- import com.google.api.client.auth.oauth2.ClientParametersAuthentication;
- import com.google.api.client.auth.oauth2.Credential;
- import com.google.api.client.auth.oauth2.CredentialStoreRefreshListener;
- import com.google.api.client.auth.oauth2.TokenResponse;
- import com.google.api.client.extensions.appengine.auth.oauth2.AppEngineCredentialStore;
- import com.google.api.client.extensions.appengine.http.UrlFetchTransport;
- import com.google.api.client.http.GenericUrl;
- import com.google.api.client.json.jackson2.JacksonFactory;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- @SuppressWarnings("deprecation")
- public class GoogleOAuth2Utils {
- private final static Logger log = LoggerFactory.getLogger(GoogleOAuth2Utils.class);
- private final GoogleOAuth2Config googleOAuth2Config;
- @Inject
- public GoogleOAuth2Utils(final GoogleOAuth2Config googleOAuth2Config) {
- this.googleOAuth2Config = googleOAuth2Config;
- }
- public Credential blankCredential(final String userId) {
- Credential credential =
- new Credential.Builder(BearerToken.authorizationHeaderAccessMethod())
- .addRefreshListener(new CredentialStoreRefreshListener(userId,
- new AppEngineCredentialStore()))
- .setJsonFactory(new JacksonFactory())
- .setTokenServerUrl(new GenericUrl("https://accounts.google.com/o/oauth2/token"))
- .setClientAuthentication(
- new ClientParametersAuthentication(
- this.googleOAuth2Config.getGoogleApiKey(),
- this.googleOAuth2Config.getGoogleApiSecret()))
- .setTransport(new UrlFetchTransport())
- .build();
- return (credential);
- }
- public Credential getCredential(String userId, TokenResponse tokenResponse) {
- Credential credential = blankCredential(userId);
- credential.setFromTokenResponse(tokenResponse);
- if (tokenResponse.getRefreshToken() != null) {
- log.debug("setting refresh token from token response");
- credential.setRefreshToken(tokenResponse.getRefreshToken());
- }
- AppEngineCredentialStore appEngineCredentialStore = new AppEngineCredentialStore();
- appEngineCredentialStore.store(userId, credential);
- return (credential);
- }
- public TokenResponse tokenResponse(final String code, final String scheme, final String serverName, final int serverPort) throws IOException, URISyntaxException {
- TokenResponse tokenResponse =
- new AuthorizationCodeTokenRequest(new UrlFetchTransport(),
- new JacksonFactory(),
- new GenericUrl("https://accounts.google.com/o/oauth2/token"),
- code)
- .setRedirectUri(googleOJCCallbackUri(scheme, serverName, serverPort))
- .setClientAuthentication(new ClientParametersAuthentication(
- this.googleOAuth2Config.getGoogleApiKey(),
- this.googleOAuth2Config.getGoogleApiSecret()))
- .execute();
- return (tokenResponse);
- }
- public String callbackUrl(final String scheme, final String serverName, final int serverPort, final String requestUri) throws URISyntaxException {
- String url =
- new AuthorizationCodeRequestUrl("https://accounts.google.com/o/oauth2/auth", this.googleOAuth2Config.getGoogleApiKey())
- .setRedirectUri(googleOJCCallbackUri(scheme, serverName, serverPort))
- .setScopes(Collections.singletonList("https://www.googleapis.com/auth/urlshortener"))
- .setState(requestUri)
- .set("approval_prompt", "force")
- .set("access_type", "offline")
- .build();
- return (url);
- }
- public String googleOJCCallbackUri(String scheme, String serverName, int serverPort) throws URISyntaxException {
- final URI callbackUri = new URI(scheme,
- null,
- serverName,
- serverPort,
- this.googleOAuth2Config.getGoogleOJCCallbackPath(),
- null, null);
- return (callbackUri.normalize().toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment