use of org.glassfish.jersey.client.oauth1.AccessToken in project dialogflow-transactions-java by actions-on-google.
the class TransactionsApp method sendOrderUpdate.
private static void sendOrderUpdate(String orderId) throws IOException {
// Setup service account credentials
String serviceAccountKeyFileName = "service-account.json";
// Setup service account credentials
String serviceAccountFile = TransactionsApp.class.getClassLoader().getResource(serviceAccountKeyFileName).getFile();
InputStream actionsApiServiceAccount = new FileInputStream(serviceAccountFile);
ServiceAccountCredentials serviceAccountCredentials = (ServiceAccountCredentials) ServiceAccountCredentials.fromStream(actionsApiServiceAccount).createScoped(Collections.singleton("https://www.googleapis.com/auth/actions.order.developer"));
AccessToken token = serviceAccountCredentials.refreshAccessToken();
// Setup request with headers
HttpPatch request = new HttpPatch("https://actions.googleapis.com/v3/orders/" + orderId);
request.setHeader("Content-type", "application/json");
request.setHeader("Authorization", "Bearer " + token.getTokenValue());
// Create order update
FieldMask fieldMask = FieldMask.newBuilder().addAllPaths(Arrays.asList("lastUpdateTime", "purchase.status", "purchase.userVisibleStatusLabel")).build();
OrderUpdateV3 orderUpdate = new OrderUpdateV3().setOrder(new OrderV3().setMerchantOrderId(orderId).setLastUpdateTime(Instant.now().toString()).setPurchase(new PurchaseOrderExtension().setStatus("DELIVERED").setUserVisibleStatusLabel("Order delivered."))).setUpdateMask(FieldMaskUtil.toString(fieldMask)).setReason("Order status was updated to delivered.");
// Setup JSON body containing order update
JsonParser parser = new JsonParser();
JsonObject orderUpdateJson = parser.parse(new Gson().toJson(orderUpdate)).getAsJsonObject();
JsonObject body = new JsonObject();
body.add("orderUpdate", orderUpdateJson);
JsonObject header = new JsonObject();
header.addProperty("isInSandbox", true);
body.add("header", header);
StringEntity entity = new StringEntity(body.toString());
entity.setContentType(ContentType.APPLICATION_JSON.getMimeType());
request.setEntity(entity);
// Make request
HttpClient httpClient = HttpClientBuilder.create().build();
HttpResponse response = httpClient.execute(request);
LOGGER.info(response.getStatusLine().getStatusCode() + " " + response.getStatusLine().getReasonPhrase());
}
use of org.glassfish.jersey.client.oauth1.AccessToken in project google-auth-library-java by googleapis.
the class AppEngineCredentials method refreshAccessToken.
/**
* Refresh the access token by getting it from the App Identity service
*/
@Override
public AccessToken refreshAccessToken() throws IOException {
if (createScopedRequired()) {
throw new IOException("AppEngineCredentials requires createScoped call before use.");
}
GetAccessTokenResult accessTokenResponse = appIdentityService.getAccessToken(scopes);
String accessToken = accessTokenResponse.getAccessToken();
Date expirationTime = accessTokenResponse.getExpirationTime();
return new AccessToken(accessToken, expirationTime);
}
use of org.glassfish.jersey.client.oauth1.AccessToken in project google-auth-library-java by googleapis.
the class AppEngineCredentialsTest method refreshAccessToken_sameAs.
@Test
void refreshAccessToken_sameAs() throws IOException {
String expectedAccessToken = "ExpectedAccessToken";
MockAppIdentityService appIdentity = new MockAppIdentityService();
appIdentity.setAccessTokenText(expectedAccessToken);
appIdentity.setExpiration(new Date(System.currentTimeMillis() + 60L * 60L * 100L));
AppEngineCredentials credentials = AppEngineCredentials.newBuilder().setScopes(SCOPES).setAppIdentityService(appIdentity).build();
AccessToken accessToken = credentials.refreshAccessToken();
assertEquals(appIdentity.getAccessTokenText(), accessToken.getTokenValue());
assertEquals(appIdentity.getExpiration(), accessToken.getExpirationTime());
}
use of org.glassfish.jersey.client.oauth1.AccessToken in project psoxy by Worklytics.
the class OAuthAccessTokenSourceAuthStrategy method getCredentials.
@Override
public Credentials getCredentials(Optional<String> userToImpersonateIgnored) {
String token = config.getConfigPropertyOrError(ConfigProperty.ACCESS_TOKEN);
// Some date into far future. Expiration is required
Instant expire = clock.instant().plus(365L, ChronoUnit.DAYS);
AccessToken accessToken = new AccessToken(token, Date.from(expire));
// OAuth2Credentials tried to refresh and fail
OAuth2CredentialsWithRefresh.Builder builder = OAuth2CredentialsWithRefresh.newBuilder();
builder.setAccessToken(accessToken);
// refresh does nothing, just return the same token
builder.setRefreshHandler(() -> accessToken);
return builder.build();
}
use of org.glassfish.jersey.client.oauth1.AccessToken in project jib by GoogleContainerTools.
the class CredentialRetrieverFactoryTest method setUp.
@Before
public void setUp() throws CredentialHelperUnhandledServerUrlException, CredentialHelperNotFoundException, IOException {
Mockito.when(mockDockerCredentialHelperFactory.create(Mockito.anyString(), Mockito.any(Path.class), Mockito.anyMap())).thenReturn(mockDockerCredentialHelper);
Mockito.when(mockDockerCredentialHelper.retrieve()).thenReturn(FAKE_CREDENTIALS);
Mockito.when(mockGoogleCredentials.getAccessToken()).thenReturn(new AccessToken("my-token", null));
}
Aggregations