AI-generated Key Takeaways
-
All requests to Google Pay's Omnichannel API must be authorized using OAuth 2.0 with a service account.
-
The provided Java code demonstrates how to authorize requests using a service account key file and make API calls.
-
To use the code, you need to add specific Maven dependencies for Google API Client, HTTP Client, and Gson libraries.
-
Further information on OAuth 2.0 and Google API client libraries is available through provided links.
All requests to Google Pay APIs must be authorized by an authenticated user. Your application must include the following code to authorize the Omnichannel API.
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.FileContent;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpContent;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.common.io.CharStreams;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.security.GeneralSecurityException;
import java.util.Collections;
public class OauthClient {
private static final String JWT_PATH = "service-account-key.json";
private static final String API_SCOPE = "https://www.googleapis.com/auth/nbupaymentsmerchants";
private static final String POST_JSON = "initiate.json";
private static final String API_URL =
"https://nbupayments.googleapis.com/v1/merchantPayments:initiate";
public static final Charset UTF8 = Charset.forName("UTF-8");
public static final int HTTP_TIMEOUT_MS = 10000;
public static void main(String args[]) {
HttpTransport httpTransport;
HttpRequestFactory httpRequestFactory;
try {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = getGoogleCredential(httpTransport);
httpRequestFactory = httpTransport.createRequestFactory(credential);
doPostApplication(httpRequestFactory);
} catch (IOException | GeneralSecurityException e) {
System.out.println("Error in posting data.");
}
}
/** Creates a {@link HttpContent} from the json file. */
private static HttpContent getContent() {
return new FileContent("application/json", new File(POST_JSON));
}
/** Creates a new credential using the service account json file and Oauth scope. */
private static GoogleCredential getGoogleCredential(HttpTransport httpTransport)
throws FileNotFoundException, IOException {
return GoogleCredential.fromStream(
new FileInputStream(new File(JWT_PATH)),
httpTransport,
JacksonFactory.getDefaultInstance())
.createScoped(Collections.singleton(API_SCOPE));
}
/** Gets the specification of an application from the Google servers. */
private static void doPostApplication(HttpRequestFactory requestFactory) throws IOException {
GenericUrl url = new GenericUrl(API_URL);
HttpRequest httpRequest = requestFactory.buildPostRequest(url, getContent());
doHttpRequest(httpRequest);
}
/** Executes HTTP request against the API and prints response. */
private static void doHttpRequest(HttpRequest httpRequest) throws IOException {
// Set read timeout.
httpRequest.setReadTimeout(HTTP_TIMEOUT_MS).setThrowExceptionOnExecuteError(false);
HttpResponse httpResponse = httpRequest.execute();
System.out.println("Status code: " + httpResponse.getStatusCode());
String responseContent = inputStreamToString(httpResponse.getContent());
// Parse and print formatted response.
JsonObject response = new JsonParser().parse(responseContent).getAsJsonObject();
System.out.println("response: " + response);
}
/** Reads input stream contents using the UTF-8 charset into a string. */
private static String inputStreamToString(InputStream in) throws IOException {
return CharStreams.toString(new InputStreamReader(in, UTF8));
}
}
Maven dependencies
To make your code work, you must add the following Maven dependencies while building the app.
<dependencies>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.22.0</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client</artifactId>
<version>1.22.0</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson</artifactId>
<version>1.22.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>
Oauth 2.0 References
https://developers.google.com/identity/protocols/OAuth2#serviceaccount
https://developers.google.com/identity/protocols/OAuth2ServiceAccount
The following client libraries integrate with popular frameworks, which makes implementing OAuth 2.0 simpler.
- Google-api-client
- Google-http-java-client