All Products
Search
Document Center

Mobile Platform as a Service:Overview and Preparation

Last Updated:Jun 20, 2023

Mini Program developing operations from creating a Mini Program to releasing one can be implemented by calling openAPI, which enables the interaction between the user server side and the mPaaS server side.

Overview

Mini Program developing operations from creating a Mini Program to releasing one can be implemented by calling openAPI, which enables the interaction between the user server side and the mPaaS server side.

Rate limit description

To prevent OpenAPI from being called too frequently and thus affecting the operation of the App, a rate limiting mechanism is adopted for calling OpenAPI. The details are as follows:

  • mcube rate limiting runs on a single server, and the limiting dimensions are appId+workspaceId.

  • Currently mcube provides two servers for accepting OpenAPI requests, which are then forwarded by load balancer.

  • On a single server, the limit for Mini Program uploading interface is 10 times every minute, that is, uploading interface can be called once every 6 seconds; the limit for other interfaces is 600 times every minute, that is, the interfaces can be called once every 0.1 second.

Preparation

Before using OpenAPI, you need to obtain AccessKey, App ID and Workspace ID, configure Maven dependencies and configure file uploading.

Obtain AccessKey

AccessKey includes AccessKey ID and AccessKey Secret. Click here for obtaining method.

  • AccessKey ID: used to identify users.

  • AccessKey Secret: used for user authentication. MUST be kept safe.

Obtain App ID and Workspace ID

  1. Log in to mPaaS console, and enter the App.

  2. In Overview page, click Code configurations (choose Android or iOS based on your needs)> Download configuration file > Download now. You can view App ID and Workspace ID in the Code configurations panel.

Configure Maven dependencies

Before using OpenAPI, you need to complete the following Maven dependency configurations.

<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-mpaas</artifactId>
  <version>1.1.1</version>
</dependency>

<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <optional>true</optional>
  <version>[4.3.2,5.0.0)</version>
</dependency>

Environment Variable Configuration

Configuer environment variable MPAAS_AK_ENV and MPAAS_SK_ENV

  • Linux and macOS system configuration methods execute the following commands:

    export MPAAS_AK_ENV=<access_key_id>
    export MPAAS_SK_ENV=<access_key_secret>
    Note

    access_key_id is replaced with the prepared AccessKey ID, and access_key_secret is replaced with the AccessKey Secret.

  • Windows system configuration method

    1. Create a new environment variable, add environment variables MPAAS_AK_ENV and MPAAS_SK_ENV, and write the prepared AccessKey ID and AccessKey Secret.

    2. Restart Windows system.

Code sample

It is strongly recommended not to save the AccessKey ID and AccessKey Secret in the project code, otherwise the AccessKey may be leaked, threatening the security of all resources under your account. // This example uses saving the AccessKey ID and AccessKey Secret in environment variables as an example. You can also save it to the configuration file according to business needs.import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.mpaas.model.v20201028.QueryMcubeVhostRequest;
import com.aliyuncs.mpaas.model.v20201028.QueryMcubeVhostResponse;
import com.aliyuncs.profile.DefaultProfile;

public class MpaasApiDemo {

    /**
     * The corresponding APP ID on the mPaaS console
     */
    private static final String APP_ID = "ALIPUB40DXXXXXXX";

    /**
     * The corresponding workspace id on the mPaaS console
     */
    private static final String WORKSPACE_ID = "default";

    /**
     * The corresponding tenant id on the mPaaS console
     */
    private static final String TENANT_ID = "XVXXXXXF";

    /**
     * Region ID, the default is cn-hangzhou
     */
    private static final String REGION_ID = "cn-hangzhou";

    /**
     * Product name
     */
    private static final String PRODUCT = "mpaas";

    /**
     * The endpoint to call
     */
    private static final String END_POINT = "mpaas.cn-hangzhou.aliyuncs.com";

    public static void main(String[] args) {
        // Alibaba Cloud account AccessKey has access rights to all APIs. It is recommended that you use RAM users for API access or daily operation and maintenance.
        // It is strongly recommended not to save the AccessKey ID and AccessKey Secret in the project code, otherwise the AccessKey may be leaked, threatening the security of all resources under your account.
        // This example uses saving the AccessKey ID and AccessKey Secret in environment variables as an example. You can also save it to the configuration file according to business needs.
        String accessKeyId = System.getenv("MPAAS_AK_ENV");
        String accessKeySecret = System.getenv("MPAAS_SK_ENV");

        DefaultProfile.addEndpoint(REGION_ID, PRODUCT, END_POINT);
        DefaultProfile profile = DefaultProfile.getProfile(REGION_ID, accessKeyId, accessKeySecret);
        IAcsClient iAcsClient = new DefaultAcsClient(profile);
        QueryMcubeVhostRequest queryMcubeVhostRequest = new QueryMcubeVhostRequest();
        queryMcubeVhostRequest.setAppId(APP_ID);
        queryMcubeVhostRequest.setWorkspaceId(WORKSPACE_ID);
        queryMcubeVhostRequest.setTenantId(TENANT_ID);
        QueryMcubeVhostResponse acsResponse = null;
        try {
            acsResponse = iAcsClient.getAcsResponse(queryMcubeVhostRequest);
            System.out.println(acsResponse.getResultCode());
            System.out.println(acsResponse.getQueryVhostResult());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}

Configure file uploading

Since file streaming is not allowed in all APIs, to upload a file, you need to upload it to OSS first by calling the upload tool class, and then send the returned OSS address as a parameter to the specified API.

You can download the file upload tool class OssPostObject.java.zip.

Code sample

The following shows the code sample of file uploading:

        GetMcubeFileTokenRequest getMcubeFileTokenRequest = new GetMcubeFileTokenRequest();
        getMcubeFileTokenRequest.setAppId(APP_ID);
        getMcubeFileTokenRequest.setOnexFlag(true);
        getMcubeFileTokenRequest.setTenantId(TENANT_ID);
        getMcubeFileTokenRequest.setWorkspaceId(WORKSPACE_ID);
        GetMcubeFileTokenResponse acsResponse = iAcsClient.getAcsResponse(getMcubeFileTokenRequest);
        System.out.println(JSON.toJSONString(acsResponse));

        GetMcubeFileTokenResponse.GetFileTokenResult.FileToken fileToken = acsResponse.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();

Refer to Obtain upload file token for descriptions about GetMcubeFileTokenRequest.