Java SDK call example

Updated at:

You can use OpenAPI to automate integration with Cloud Backup and improve your development efficiency. This topic describes how to use OpenAPI to call the DescribeBackupPlans operation of Cloud Backup to query backup plans for Elastic Compute Service (ECS) files.

Step 1: View the OpenAPI documentation

Before you call an OpenAPI operation, read the corresponding API documentation to understand the required parameters and permissions. For more information, see API overview.

Step 2: Create a RAM user and grant permissions

Important

An Alibaba Cloud account has permissions for all API operations. We recommend that you create and use a Resource Access Management (RAM) user for API calls or daily operations and maintenance (O&M).

This step describes how to create a RAM user that has read-only permissions on Cloud Backup resources.

If you have already created and authorized a RAM user, you can skip this step.

  1. Log on to the Resource Access Management (RAM) console using your Alibaba Cloud account.

  2. Create a RAM user.

    1. In the navigation pane on the left, choose Identity Management > Users.

    2. On the Users page, click Create User.

    3. On the Create User page, set the Logon Name and Display Name to config-openapi-operator-hbr. In the Access Mode section, select Console Access and OpenAPI Access. Click OK.

      Important

      The AccessKey secret of a RAM user is displayed only when the user is created and cannot be retrieved later. Be sure to download the CSV file and keep it confidential.

      After you create the RAM user, record the logon name and password. You need this information to log on to the Alibaba Cloud OpenAPI Developer Portal as the RAM user. You also need the user's AccessKey for code debugging.

  3. Grant permissions to the RAM user.

    Note

    By default, a RAM user does not have any permissions to perform operations on Cloud Backup after the user is created. You must grant the required system policy, such as AliyunHBRFullAccess or AliyunHBRReadOnlyAccess, or a custom policy to the RAM user. This example grants the AliyunHBRReadOnlyAccess policy, which provides read-only permissions on Cloud Backup resources.

    1. On the Users page, find the RAM user named config-openapi-operator-hbr and click Add Permissions.

    2. In the Add Permissions panel, search for and select the AliyunHBRReadOnlyAccess system policy, and then click Confirm.

    3. Confirm the authorization result and click Close.

(Optional) Step 3: Debug an OpenAPI operation online

  1. Use the RAM user account to log on to the Alibaba Cloud OpenAPI Developer Portal.

  2. Select Cloud Backup Service as the cloud product.

  3. In the top menu bar, choose Tool Center > Online Debugging.

  4. In the navigation pane on the left, find and click the DescribeBackupPlans operation. Set Endpoint to China (Hangzhou) and the SourceType parameter to ECS_FILE. This action queries backup plans for ECS files.image.png

  5. Click Initiate Call.

    In the Call Result area, you can view the query result, request header, and response header. A Status Code of 200 indicates a successful call.image.png

Step 4: Obtain the SDK sample code

The OpenAPI platform provides software development kits (SDKs) for many programming languages, such as Java, Go, Python, Node.js, TypeScript, PHP, and C++. You can integrate an SDK to call OpenAPI operations directly. SDKs simplify development by handling tasks such as signing, timeouts, and retries, and by returning structured response objects.

This section uses the Java SDK as an example. The method for obtaining sample code is the same for other languages.

  1. On the SDK Sample tab, select an SDK version and language. We recommend using V2.0. For more information about the differences between V2.0 and V1.0, see Differences between V1.0 SDK and V2.0 SDK.

  2. Click Download Complete Project to download the full SDK project. After the download is complete, decompress the package.

    Note
    • In the upper-right corner of the SDK sample area, click the image icon to copy the sample code.

    • Click SDK Information to obtain basic information for development integration, such as the SDK package name, version, management platform, and installation command.

    image

Step 5: Run the SDK sample code

This section uses the Java SDK as an example.

  1. Open IntelliJ IDEA, click File > Open, and select the decompressed project folder. Wait for Maven to automatically install dependencies.

  2. Before you make the call, obtain an AccessKey pair. We recommend that you use the AccessKey pair for the RAM user that you created. For more information, see Create an AccessKey pair for a RAM user.

    Important

    After you obtain the AccessKey pair for the RAM user, set the AccessKey in the environment variables. For more information, see Configure environment variables on Linux, macOS, and Windows.

  3. Run the sample code.

    Double-click the Sample file to open it. After you confirm that the code is error-free, run the code.

    image

  4. View the execution result. In the console at the bottom of the window, press Ctrl+F and search for statusCode. If you see "statusCode":200, the call was successful.

    image

Troubleshooting

  • code: 404, Specified access key is not found.

    The AccessKey that you configured in the environment variables has not taken effect. Restart the IDE or the system and try again.

  • code: 403, User is not authorized to do this operation.

    This error indicates that the RAM user that you are using does not have sufficient permissions to perform this operation. For example, if you grant the AliyunHBRReadOnlyAccess permission to the RAM user, the user can call only query operations. If you use this user to call an operation such as CreateVault, the server returns code: 403, User is not authorized to do this operation..

    To resolve this issue, use your Alibaba Cloud account to log on to the RAM console. In the RAM console, grant the required permissions for the operation to the RAM user. Then, try the operation again. For more information about authorization, see Use RAM for access control.

References