Overview
You can call OpenAPI to perform all operations in the miniapp lifecycle, from creation to publication. This connects your server-side to the mPaaS server-side.
If you call OpenAPI using STS, see Call OpenAPI using STS.
Throttling
OpenAPI calls are throttled to prevent excessive requests from impacting application performance. The throttling policy is as follows:
mcube OpenAPI calls are throttled on a per-device basis. The throttling dimension is a combination of `appId` and `workspaceId`.
mcube uses two devices to accept OpenAPI requests. A Server Load Balancer instance forwards these requests.
On a per-device basis, the API for uploading miniapp resource packages is throttled to 10 calls per minute. This means you can call it once every 6 seconds. All other APIs are throttled to 600 calls per minute. This means you can call them once every 0.1 seconds.
Preparations
Before you use OpenAPI, you must obtain an AccessKey, an App ID, a Workspace ID, and a Tenant ID. Then, you need to configure Maven dependencies and prepare to upload configuration files.
Obtain an AccessKey
An AccessKey includes an AccessKey ID and an AccessKey secret. For more information, see Obtain an AccessKey.
AccessKey ID: Used to identify the user.
AccessKey secret: A key used to authenticate the user. You must keep it confidential.
Obtain the App ID, Workspace ID, and Tenant ID
Log on to the mPaaS console and go to your application.
On the Overview page, navigate to Code Configuration (select Android or iOS as needed) > Download Configuration File > Download Now. The App ID, Workspace ID, and Tenant ID are displayed in the Code Configuration window that opens on the right.
Configure Maven dependencies
Before you use OpenAPI, you must configure the following Maven dependency.
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>mpaas20201028</artifactId>
<version>5.0.0</version>
</dependency>Configure environment variables
Configure the MPAAS_AK_ENV and MPAAS_SK_ENV environment variables.
For Linux and macOS, run the following commands:
export MPAAS_AK_ENV=<ACCESS_KEY_ID> export MPAAS_SK_ENV=<ACCESS_KEY_SECRET>NoteReplace
<ACCESS_KEY_ID>with your AccessKey ID and<ACCESS_KEY_SECRET>with your AccessKey secret.Windows configuration method
Create the MPAAS_AK_ENV and MPAAS_SK_ENV environment variables, and set their values to your AccessKey ID and AccessKey secret.
Restart Windows.
Usage examples
import com.aliyun.mpaas20201028.Client;
import com.aliyun.mpaas20201028.models.QueryMcubeVhostRequest;
import com.aliyun.mpaas20201028.models.QueryMcubeVhostResponse;
public class MpaasApiDemo {
/**
* The App ID in the mPaaS console.
*/
private static final String APP_ID = "ALIPUB40DXXXXXXX";
/**
* The workspace ID in the mPaaS console.
*/
private static final String WORKSPACE_ID = "default";
/**
* The tenant ID in the mPaaS console.
*/
private static final String TENANT_ID = "XVXXXXXF";
/**
* The Endpoint to call.
*/
private static final String END_POINT = "mpaas.cn-hangzhou.aliyuncs.com";
public static void main(String[] args) throws Exception {
// An Alibaba Cloud account AccessKey has permissions for all APIs. For security, use a Resource Access Management (RAM) user for API calls and daily O&M.
// Do not hard-code the AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked, which compromises the security of all resources in your account.
// This example shows how to store the AccessKey ID and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
String accessKeyId = System.getenv("MPAAS_AK_ENV");
String accessKeySecret = System.getenv("MPAAS_SK_ENV");
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret)
.setEndpoint(END_POINT);
Client client = new Client(config);
QueryMcubeVhostRequest queryMcubeVhostRequest = new QueryMcubeVhostRequest();
queryMcubeVhostRequest.setAppId(APP_ID);
queryMcubeVhostRequest.setWorkspaceId(WORKSPACE_ID);
queryMcubeVhostRequest.setTenantId(TENANT_ID);
QueryMcubeVhostResponse acsResponse = null;
try {
QueryMcubeVhostResponse queryMcubeVhostResponse = client.queryMcubeVhost(queryMcubeVhostRequest);
System.out.println(queryMcubeVhostResponse.getBody().getResultCode());
System.out.println(queryMcubeVhostResponse.getBody().getQueryVhostResult());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Upload a configuration file
The APIs do not accept file streams. To upload a file, you must first use the upload utility class to upload the file to Object Storage Service (OSS). Then, pass the returned OSS address as a parameter to the target API.
Download the file upload utility class: OssPostObject.java.zip.
Sample code
The following example shows how to upload a file:
GetMcubeFileTokenRequest getMcubeFileTokenRequest = new GetMcubeFileTokenRequest();
getMcubeFileTokenRequest.setAppId(APP_ID);
getMcubeFileTokenRequest.setOnexFlag(true);
getMcubeFileTokenRequest.setTenantId(TENANT_ID);
getMcubeFileTokenRequest.setWorkspaceId(WORKSPACE_ID);
GetMcubeFileTokenResponse acsResponse = client.getMcubeFileToken(getMcubeFileTokenRequest);
System.out.println(JSON.toJSONString(acsResponse));
GetMcubeFileTokenResponseBody.GetMcubeFileTokenResponseBodyGetFileTokenResultFileToken fileToken = acsResponse.getBody().getGetFileTokenResult().getFileToken();
OssPostObject ossPostObject = new OssPostObject();
ossPostObject.setKey(fileToken.getDir());
ossPostObject.setHost(fileToken.getHost());
ossPostObject.setOssAccessId(fileToken.getAccessid());
ossPostObject.setPolicy(fileToken.getPolicy());
ossPostObject.setSignature(fileToken.getSignature());
ossPostObject.setFilePath("your/local/file/path");
String s = ossPostObject.postObject();For more information about GetMcubeFileTokenRequest, see Obtain an upload file token.