使用 AccessKey 鉴权方式执行工作流,可以通过 SDK 调用执行,为您提供更安全的服务。

SDK逻辑编排目前提供了两种客户端调用方式,如果无需鉴权,可以直接调用 endpoint 执行工作流;如果选择的是 AccessKey 鉴权方式,需要配合 SDK 调用执行,可以通过以下步骤执行。

步骤

  1. 获取 Path

2. 通过 RAM 授权(使用子账号调用)

如果是使用子账号进行访问,请提前给子账号赋予权限,具体操作可参见 使用RAM授权

3. 使用 SDK 调用

JAVA

  • 安装依赖

将 Maven 依赖项添加到 pom.xml 中

<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>4.0.3</version>
</dependency>
  • 代码示例
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

public class TestJavaSDK {
    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("default", "<accessKeyId>", "<accessSecret>");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("lc.<regionId>.aliyuncs.com");
        request.setVersion("2019-09-25");
        
        request.setUriPattern("<Path>");
        // 这里根据工作流需求设置参数
        request.putQueryParameter("RegionId", "us-east-1");
        request.putHeadParameter("Content-Type", "application/json");
        // 这里根据工作流需求设置正文,注意是 JSON 字符串
        String requestBody = "";
        request.setHttpContent(requestBody.getBytes(), "utf-8", FormatType.JSON);
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}

GO

  • 代码示例
package main

import (
	"fmt"
	"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
	"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
)

func main() {
	client, err := sdk.NewClientWithAccessKey("default", "<accessKeyId>", "<accessSecret>")
	if err != nil {
		panic(err)
	}

	request := requests.NewCommonRequest()
	request.Method = "POST"
	request.Scheme = "https" // https | http
	request.Domain = "lc.<regionId>.aliyuncs.com"
	request.Version = "2019-09-25"
        // 注意下面的 [WorkflowId] 替换为刚才复制的 ID
	request.PathPattern = "<Path>"
  	request.Headers["Content-Type"] = "application/json"
        // 这里根据工作流需求设置参数
  	request.QueryParams["RegionId"] = "us-east-1"
        // 这里根据工作流需求设置正文,注意是 JSON 字符串
	body := ``
	request.Content = []byte(body)
	
	response, err := client.ProcessCommonRequest(request)
	if err != nil {
		panic(err)
	}
	fmt.Print(response.GetHttpContentString())
}

Python

  • 代码示例
#!/usr/bin/env python
#coding=utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
client = AcsClient('<accessKeyId>', '<accessSecret>', 'default')

request = CommonRequest()
request.set_accept_format('json')
request.set_method('POST')
request.set_protocol_type('https') # https | http
request.set_domain('lc.<regionId>.aliyuncs.com')
request.set_version('2019-09-25')

# 注意下面的 [WorkflowId] 替换为刚才复制的 ID
request.set_uri_pattern('<Path>')
# 这里根据工作流需求设置参数
request.add_query_param('RegionId', "us-east-1")

request.add_header('Content-Type', 'application/json')
# 这里根据工作流需求设置正文,注意是 JSON 字符串
body = ''''''
request.set_content(body.encode('utf-8'))

response = client.do_action_with_exception(request)

# python2:  print(response) 
print(str(response, encoding = 'utf-8'))

Ruby

# gem install aliyunsdkcore

require 'aliyunsdkcore'

client = ROAClient.new(
  access_key_id: '<accessKeyId>',
  access_key_secret: '<accessSecret>',
  endpoint: 'https://lc.<regionId>.aliyuncs.com',
  api_version: '2019-09-25'
)

response = client.request(
  method: 'POST',
  # 注意下面的 [WorkflowId] 替换为刚才复制的 ID
  uri: '<Path>',
  # 这里根据工作流需求设置参数
  queries: {
    "RegionId": "cn-hangzhou",
  },
  headers: {
    "Content-Type": "application/json"
  },
  # 这里根据工作流需求设置正文,注意是 JSON 字符串

  body: ``,
  options: {}
)

print response

Node.js

  • 安装依赖

npm install @alicloud/pop-core

  • 代码示例
'use strict'
var ROAClient = require('@alicloud/pop-core').ROAClient;
var client = new ROAClient({
  accessKeyId: '<your access key id>',
  accessKeySecret: '<your access key secret>',
  endpoint: 'https://lc.<regionId>.aliyuncs.com',
  apiVersion: '2019-09-25',
});

async function test() {
  try {
    var res = await client.request(
      'POST',
      '<Path>',
      {}, // 这里根据工作流需求设置参数
      '<request body>', // 这里根据工作流需求设置正文,注意是 JSON 字符串
      {
        'content-type': 'application/json',
      }
    );
    console.log('invoke workflow: ', res);
  } catch (err) {
    console.error(err);
  }
}

test().then();

接口详细入参可以查看SDK 源码

逻辑编排支持的 Region

Region RegionId Endpoint
上海 cn-shanghai https://lc.cn-shanghai.aliyuncs.com
美东 us-east-1 https://lc.us-east-1.aliyuncs.com