use of com.google.api.client.auth.oauth2.TokenResponse in project che by eclipse.
the class OAuthAuthenticator method callback.
/**
* 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());
}
}
use of com.google.api.client.auth.oauth2.TokenResponse in project OsmAnd-tools by osmandapp.
the class UpdateSubscriptionImpl method getPublisherApi.
private static AndroidPublisher getPublisherApi(String file) throws JSONException, IOException {
Properties properties = new Properties();
properties.load(new FileInputStream(file));
GOOGLE_CLIENT_CODE = properties.getProperty("GOOGLE_CLIENT_CODE");
GOOGLE_CLIENT_ID = properties.getProperty("GOOGLE_CLIENT_ID");
GOOGLE_CLIENT_SECRET = properties.getProperty("GOOGLE_CLIENT_SECRET");
GOOGLE_REDIRECT_URI = properties.getProperty("GOOGLE_REDIRECT_URI");
TOKEN = properties.getProperty("TOKEN");
// getRefreshToken();
String token = TOKEN;
String accessToken = getAccessToken(token);
TokenResponse tokenResponse = new TokenResponse();
// System.out.println("refresh token=" + token);
// System.out.println("access token=" + accessToken);
tokenResponse.setAccessToken(accessToken);
tokenResponse.setRefreshToken(token);
tokenResponse.setExpiresInSeconds(3600L);
tokenResponse.setScope("https://www.googleapis.com/auth/androidpublisher");
tokenResponse.setTokenType("Bearer");
HttpRequestInitializer credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT).setJsonFactory(JSON_FACTORY).setClientSecrets(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET).build().setFromTokenResponse(tokenResponse);
AndroidPublisher publisher = new AndroidPublisher.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(GOOGLE_PRODUCT_NAME).build();
return publisher;
}
use of com.google.api.client.auth.oauth2.TokenResponse in project data-transfer-project by google.
the class GoogleAuthDataGenerator method generateAuthData.
@Override
public AuthData generateAuthData(String callbackBaseUrl, String authCode, String id, AuthData initialAuthData, String extra) {
Preconditions.checkState(initialAuthData == null, "Earlier auth data not expected for Google flow");
AuthorizationCodeFlow flow;
TokenResponse response;
try {
flow = createFlow();
response = flow.newTokenRequest(authCode).setRedirectUri(// TODO(chuy): Parameterize
callbackBaseUrl + redirectPath).execute();
} catch (IOException e) {
throw new RuntimeException("Error calling AuthorizationCodeFlow.execute ", e);
}
// Figure out storage
Credential credential = null;
try {
credential = flow.createAndStoreCredential(response, id);
} catch (IOException e) {
throw new RuntimeException("Error calling AuthorizationCodeFlow.createAndStoreCredential ", e);
}
// GoogleIdToken.Payload payload = ((GoogleTokenResponse) response).parseIdToken().getPayload();
return new TokensAndUrlAuthData(credential.getAccessToken(), credential.getRefreshToken(), credential.getTokenServerEncodedUrl());
}
use of com.google.api.client.auth.oauth2.TokenResponse in project data-transfer-project by google.
the class GoogleAuth method generateAuthData.
@Override
public AuthData generateAuthData(String callbackBaseUrl, String authCode, UUID jobId, @Nullable AuthData initialAuthData, @Nullable String extra) throws IOException {
Preconditions.checkState(initialAuthData == null, "Earlier auth data not expected for Google flow");
AuthorizationCodeFlow flow = createFlow();
TokenResponse response = flow.newTokenRequest(authCode).setRedirectUri(// TODO(chuy): Parameterize
callbackBaseUrl + CALLBACK_PATH).execute();
// Figure out storage
Credential credential = flow.createAndStoreCredential(response, jobId.toString());
// GoogleIdToken.Payload payload = ((GoogleTokenResponse) response).parseIdToken().getPayload();
return toAuthData(credential);
}
use of com.google.api.client.auth.oauth2.TokenResponse in project data-transfer-project by google.
the class InstagramAuth method generateAuthData.
@Override
public AuthData generateAuthData(String callbackBaseUrl, String authCode, UUID jobId, AuthData initialAuthData, String extra) throws IOException {
Preconditions.checkArgument(Strings.isNullOrEmpty(extra), "Extra data not expected for Instagram oauth flow");
Preconditions.checkArgument(initialAuthData == null, "Earlier auth data not expected for Instagram oauth flow");
AuthorizationCodeFlow flow = createFlow();
TokenResponse response = flow.newTokenRequest(authCode).setRedirectUri(// TODO(chuy): Parameterize
callbackBaseUrl + CALLBACK_PATH).execute();
// Figure out storage
Credential credential = flow.createAndStoreCredential(response, jobId.toString());
return toAuthData(credential);
}
Aggregations