Encrypted identity authentication - SDK call tutorial
This tutorial shows you how to call the encrypted identity authentication API from a Java application, using Apache Maven 3.8.1 and dbus_sdk_1.0.3 as examples.
Prerequisites
Java 1.7 or later. To check your Java version, run the java -version command.
IntelliJ IDE and Maven are installed, and the Maven environment variable is configured.
Procedure
Open IntelliJ IDE. Click New Project . Select Maven and click Next .
Enter a project Name and Location . This tutorial uses `dbus_Demo` as the project name. Click Finish .
After the project is created, modify the Maven and terminal paths.
In the top navigation bar, click File > Settings .
On the Settings page, in the navigation pane on the left, select Build, Execution, Deployment > Build Tools > Maven .
Select the Override checkbox for User settings file . Set the path to the `setting.xml` file from your local Maven installation. Click Apply , then click OK .
In the navigation pane on the left, select Tools > Terminal . Set the Shell path to your local path. Click Apply , then click OK .
Download the dbus SDK to your local machine. In IntelliJ IDE, open the terminal at the bottom of the page. Run the following command to install the dbus SDK to your local repository. A `BUILD SUCCESS` message indicates that the installation is successful.
mvn install:install-file -DgroupId=com.aliyun.dataq.service.dbus -DartifactId=dbus-sdk -Dversion=1.0.3-SNAPSHOT -Dpackaging=jar -Dfile=${path_to_your_dbus_sdk_jar_file}/dbus-sdk-1.0.3.jarIn the Project view, open the pom.xml file and add the following dependency.
<dependencies> <dependency> <groupId>com.aliyun.dataq.service.dbus</groupId> <artifactId>dbus-sdk</artifactId> <version>1.0.3-SNAPSHOT</version> </dependency> </dependencies>
On the right side of the page, click the M icon to open the Maven tool window. Click the refresh icon. The Dependencies folder appears in your `dbus_Demo` project. This indicates that the dependency was added successfully.
After you complete the configuration, create a Java class file named `Demo` in the Java folder of the `dbus_Demo` project.
In the `Demo.java` file, copy the following sample code. Modify the `endpoint`, `path`, `AppCode`, `AppKey`, and `AppSecret` parameters based on the call information for your target API. For more information about how to obtain API call information, see Obtain API call information.
NoteThe API access path consists of the `endpoint` and `path`.
import com.alibaba.cosmo.remote.httpclient.HttpMethod; import com.alibaba.fastjson.JSONObject; import com.aliyun.dataq.service.dbus.signature.DaasClient; import com.aliyun.dataq.service.dbus.signature.DataQService; import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.MultipartEntityBuilder; import java.io.File; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; public class Demo { public static void main(String[] args) { // The endpoint of the target API. String endpoint = "https://dataq.aliyuncs.com"; // The path of the target API. String path = "/yourworkspaceCode/yourAppCode/APIPath"; // The AppCode, AppKey, and AppSecret of the application that is authorized to call the target API. String appCode = "yourAppCode"; String AppKey = "yourAppKey"; String AppSecret = "yourAppSecret"; DataQService.Builder builder = new DataQService.Builder(); builder.setPath(path); builder.setModuleName(appCode); builder.setAccessKeyId(AppKey); builder.setAccessKeySecret(AppSecret); // Add request header parameters. Map<String, Object> headerMap = new HashMap<>(); headerMap.put("Accept", "application/json"); headerMap.put("Content-Type", "application/json"); headerMap.put("h1", ""); headerMap.put("h2", ""); builder.addHeader(headerMap); // Add query parameters. Map<String, Object> queryParam = new HashMap<>(); queryParam.put("p1", ""); queryParam.put("p2", ""); builder.addQueryParam(queryParam); // Add request body parameters. Map<String, Object> map = new HashMap<>(); map.put("b1", ""); List<String> values = Arrays.asList("v1"); map.put("b2", values); builder.setRequestBody(new JSONObject(map)); // File upload. MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create() .addTextBody("k1","v1") .addTextBody("k2","v2") .addBinaryBody("file", new File("/demo/2602780.txt"), ContentType.APPLICATION_OCTET_STREAM, "2602780.txt"); builder.setMultipartEntityBuilder(multipartEntityBuilder); // Set the request method. DataQService dataQService = builder.setHttpMethod(HttpMethod.GET).build(); // Initialize the request client. DaasClient client = new DaasClient(endpoint); // Send the request. Object httpResponse = client.invoke(dataQService, 1000000, 30); System.out.println(httpResponse); } }
After you finish editing, right-click in the editor and select Run 'Demo.main()' . Wait for the code to finish running. The API call result is returned.
