Search in sources :

Example 46 with AccessToken

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());
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) HttpResponse(org.apache.http.HttpResponse) FileInputStream(java.io.FileInputStream) HttpPatch(org.apache.http.client.methods.HttpPatch) OrderV3(com.google.api.services.actions_fulfillment.v2.model.OrderV3) StringEntity(org.apache.http.entity.StringEntity) AccessToken(com.google.auth.oauth2.AccessToken) OrderUpdateV3(com.google.api.services.actions_fulfillment.v2.model.OrderUpdateV3) HttpClient(org.apache.http.client.HttpClient) ServiceAccountCredentials(com.google.auth.oauth2.ServiceAccountCredentials) FieldMask(com.google.protobuf.FieldMask) PurchaseOrderExtension(com.google.api.services.actions_fulfillment.v2.model.PurchaseOrderExtension) JsonParser(com.google.gson.JsonParser)

Example 47 with AccessToken

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);
}
Also used : AccessToken(com.google.auth.oauth2.AccessToken) IOException(java.io.IOException) GetAccessTokenResult(com.google.appengine.api.appidentity.AppIdentityService.GetAccessTokenResult) Date(java.util.Date)

Example 48 with AccessToken

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());
}
Also used : AccessToken(com.google.auth.oauth2.AccessToken) Date(java.util.Date) BaseSerializationTest(com.google.auth.oauth2.BaseSerializationTest) Test(org.junit.jupiter.api.Test)

Example 49 with AccessToken

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();
}
Also used : AccessToken(com.google.auth.oauth2.AccessToken) Instant(java.time.Instant) OAuth2CredentialsWithRefresh(com.google.auth.oauth2.OAuth2CredentialsWithRefresh)

Example 50 with AccessToken

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));
}
Also used : Path(java.nio.file.Path) AccessToken(com.google.auth.oauth2.AccessToken) Before(org.junit.Before)

Aggregations

AccessToken (com.google.auth.oauth2.AccessToken)71 Test (org.junit.Test)41 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)29 Date (java.util.Date)22 IOException (java.io.IOException)19 AccessToken (io.vertx.ext.auth.oauth2.AccessToken)16 Client (javax.ws.rs.client.Client)10 AccessToken (org.glassfish.jersey.client.oauth1.AccessToken)10 ConsumerCredentials (org.glassfish.jersey.client.oauth1.ConsumerCredentials)10 JsonObject (io.vertx.core.json.JsonObject)9 URI (java.net.URI)9 Feature (javax.ws.rs.core.Feature)8 JerseyTest (org.glassfish.jersey.test.JerseyTest)8 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)6 InputStreamReader (java.io.InputStreamReader)6 Instant (java.time.Instant)6 WebTarget (javax.ws.rs.client.WebTarget)6 LoggingFeature (org.glassfish.jersey.logging.LoggingFeature)6 OAuth2Credentials (com.google.auth.oauth2.OAuth2Credentials)5 OAuth2TokenImpl (io.vertx.ext.auth.oauth2.impl.OAuth2TokenImpl)5