Table of Contents

Search

  1. Preface
  2. Introduction to API Manager
  3. API management
  4. Organizational access policies
  5. API-specific policies
  6. API groups
  7. Authentication and authorization
  8. API Microgateway Service
  9. Analytics

API Manager Guide

API Manager Guide

Java example: Invoke a managed API with OAuth 2.0 authentication

Java example: Invoke a managed API with OAuth 2.0 authentication

You can invoke a managed API where OAuth 2.0 authentication is enabled in Java.
In order to invoke a managed API with the OAuth 2.0 authentication method, API consumers must request an OAuth 2.0 token from the
Informatica Intelligent Cloud Services
OAuth 2.0 server.
You can use any OAuth 2.0 library, tool, or programming language to run the OAuth 2.0 authentication sequence. Before you run the OAuth 2.0 authentication, verify that you have the following information:
  • URL of the
    Informatica Intelligent Cloud Services
    OAuth 2.0 server.
  • OAuth 2.0 client ID and secret with permissions to run the managed API.
The following example shows the codes used for invoking a managed API with OAuth 2.0 authentication in Java:
import com.google.gson.Gson; import com.squareup.okhttp."; import java.io.IOException; import java.util.Map; import java.util.concurrent.Timeunit; public class OAuthClientSample ( public static String TEST_API_URL = "https://apigw-pod1.dm-us.informaticacloud.com/t/apim.usw1.com/get_employee_details"; public static String OAUTH_SERVER_URL = "https://dm-us.informaticacloud.com/authz-service/oauth/token"; public static String CLIENT_CREDENTIALS = "YwliT1ZlMWJGRUpsOOhftenFFM8dZdjpRUjQzQXcwbes-"; OkHttpClient client = new OkHttpClient(); public static void main(String [] args) throws Exception { new OAuthClientSample().runApi(); } // // run an OAuth 2.0 in a loop // public void runApi() throws Exception { // // obtain an OAuth 2.0 token for running the API // String token = getNewToken(); while (true) // // run the API using the OAuth 2.0 token // Request request = new Request.Builder() -url(TEST_API_URL) -method("GET", null) -addHeader("Authorization", Bearer + token) -build(); Response response = client.newCall(request).execute(); // // If the token expired, obtain a new token // if (response.code() --401) token = getNewToken (); else System.out.printIn(response.body().string()); Thread.sleep(TimeUnit.SECONDS.toMillis(30)); } } /** * @return a new OAuth 2.0 token from the authentication server * @throws IOException */ String getNewToken() throws IOException { String authHeader = "Basic " + CLIENT_CREDENTIALS; Request request = new request.Builder() -url(AUTH_SERVER_URL + "?grant_type =client_credentials") -method("POST", RequestBody.create(MediaType.parse("text/plain"), "")) -addHeader("Authorization", authHeader) -build(); Response response = client.newCall(request).execute(); if (response.code() != 200) { System.err.printIn(response.code()); System.exit(1); return null; } Map <String, Object> jsonResponse = new Gson().fromJson(response.body().string(), Map.class); return (String)jsonResponse.get("access_token"); } }

0 COMMENTS

We’d like to hear from you!