use of org.dataportabilityproject.types.transfer.auth.TokensAndUrlAuthData in project data-transfer-project by google.
the class GoogleContactsExporter method exportContacts.
private ExportResult<ContactsModelWrapper> exportContacts(TokensAndUrlAuthData authData, Optional<PaginationData> pageData) {
try {
// Set up connection
Connections.List connectionsListRequest = getOrCreatePeopleService(authData).people().connections().list(SELF_RESOURCE);
// Get next page, if we have a page token
if (pageData.isPresent()) {
StringPaginationToken paginationToken = (StringPaginationToken) pageData.get();
connectionsListRequest.setPageToken(paginationToken.getToken());
}
// Get list of connections (nb: not a list containing full info of each Person)
ListConnectionsResponse response = connectionsListRequest.setPersonFields(PERSON_FIELDS).execute();
List<Person> peopleList = response.getConnections();
// Get list of resource names, then get list of Persons
List<String> resourceNames = peopleList.stream().map(Person::getResourceName).collect(Collectors.toList());
GetPeopleResponse batchResponse = getOrCreatePeopleService(authData).people().getBatchGet().setResourceNames(resourceNames).setPersonFields(PERSON_FIELDS).execute();
List<PersonResponse> personResponseList = batchResponse.getResponses();
// Convert Persons to VCards
List<VCard> vCards = personResponseList.stream().map(a -> convert(a.getPerson())).collect(Collectors.toList());
// Determine if there's a next page
StringPaginationToken nextPageData = null;
if (response.getNextPageToken() != null) {
nextPageData = new StringPaginationToken(response.getNextPageToken());
}
ContinuationData continuationData = new ContinuationData(nextPageData);
ContactsModelWrapper wrapper = new ContactsModelWrapper(makeVCardString(vCards));
// Get result type
ResultType resultType = ResultType.CONTINUE;
if (nextPageData == null) {
resultType = ResultType.END;
}
return new ExportResult<ContactsModelWrapper>(resultType, wrapper, continuationData);
} catch (IOException e) {
return new ExportResult<ContactsModelWrapper>(ResultType.ERROR, e.getMessage());
}
}
use of org.dataportabilityproject.types.transfer.auth.TokensAndUrlAuthData 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());
}
Aggregations