# Quickstart

## Enable the API

Find the API library on the Cloud Console under `APIs and Services > Library`. Search for `Payments Reseller Subscription API` and enable it.

Once enabled, you will have access to two environments:

- production: <https://paymentsresellersubscription.googleapis.com>
- sandbox: <https://preprod-paymentsresellersubscription.googleapis.com>

## Using the Java API client

See the [Github Repository](https://github.com/googleapis/google-api-java-client-services/tree/master/clients/google-api-services-paymentsresellersubscription/v1) for the latest version of the java API client library.

### Use the sandbox endpoint

You can override the API endpoint in the java client library as follows:

    PaymentsResellerSubscription client = new PaymentsResellerSubscription.Builder(
        GoogleNetHttpTransport.newTrustedTransport(),
        JacksonFactory.getDefaultInstance(),
        requestInitializer)
        // Override the root_url for sandbox testing.
        .setRootUrl("https://preprod-paymentsresellersubscription.googleapis.com")
        .build();

### Use oauth authentication for service accounts

If your production environment in on GCP, such as Compute Engine or App Engine, you can directly use:

    GoogleCredentials credentials = GoogleCredentials.getApplicationDefault().createScoped(SCOPES);

Otherwise, you can load the service account credential file explicitly.

    GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath))
          .createScoped(SCOPES);

Then, specify a request initializer that automatically sets the auth token as follows:

    HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials);


    PaymentsResellerSubscription client = new PaymentsResellerSubscription.Builder(
          HTTP_TRANSPORT, JacksonFactory.getDefaultInstance(),
          requestInitializer
      ).build();

    // Issue Subscription Provision API
    client.partners()
        .subscriptions()
        .provision("partners/partnerId",
            new Subscription()
                .setLineItems(
                    ImmutableList
                        .of(new LineItem()
                                 .setProduct("partners/partnerId/products/productId")))
                .setPartnerUserToken("user_token")
                .setServiceLocation(
                    new Location().setRegionCode("US")))
        .setSubscriptionId("subscriptionId")
        .execute();

### Use user authorized oauth authentication

Depends on your production environment, [OAuth 2.0 and the Google OAuth Client Library for Java](https://developers.google.com/api-client-library/java/google-oauth-java-client/oauth2#authorization_code_flow) provides code samples for authorization code flow in Servlet and AppEngine. You could also refer to the section [Command-line authorization code flow](https://developers.google.com/api-client-library/java/google-oauth-java-client/oauth2#command-line_authorization_code_flow) in the oauth java client documentation to test the flow locally.

Once you obtained the user authorized credential, you can set the access token as follows:

    // Issue Subscription Entitle API
    client
      .partners()
      .subscriptions()
      .entitle("partners/partnerId/subscriptions/subscriptionId",
          new EntitleSubscriptionRequest())
      .setAccessToken(credential.getAccessToken())
      .execute();

Please note that we don't need a request initializer for auth credentials when creating the `client` object, because the access token is manually specified.

    PaymentsResellerSubscription client = new PaymentsResellerSubscription.Builder(
        GoogleNetHttpTransport.newTrustedTransport(),
        JacksonFactory.getDefaultInstance(),
        /* Don't specify an initializer for auth credentials */ request -> {})
        .build();