本文介绍如何如果通过SDK对接风险识别产品,这是一种简单便捷的API对接方式。
SDK调用方式介绍
通过使用阿里云风险识别SDK,调用方无需关注签名验证以及Body格式构建等繁琐的事情,推荐您使用风险识别SDK。目前支持JAVA、Python、PHP、C#、Golang共5种SDK。
JAVA SDK
JAVA Maven依赖:
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-saf</artifactId>
<version>3.0.1</version>
</dependency>
JAVA Maven推荐依赖仲裁:
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<optional>true</optional>
<version>4.5.25</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68.noneautotype</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-util</artifactId>
<version>0.31.0</version>
</dependency>
单击地址访问JAVA Maven仓库。
JAVA SDK源码:
如使用决策引擎:
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static com.aliyun.teaopenapi.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// 必填,您的 AccessKey ID
.setAccessKeyId(accessKeyId)
// 必填,您的 AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// Endpoint 请参考 https://api.aliyun.com/product/saf
config.endpoint = "saf.ap-southeast-1.aliyuncs.com";
//SINGAPORE:ap-southeast-1,MALAYSIA:ap-southeast-3,INDONESIA:ap-southeast-5
return new com.aliyun.teaopenapi.Client(config);
}
/**
* API 相关
* @param path params
* @return OpenApi.Params
*/
public static com.aliyun.teaopenapi.models.Params createApiInfo() throws Exception {
com.aliyun.teaopenapi.models.Params params = new com.aliyun.teaopenapi.models.Params()
// 接口名称
.setAction("RequestDecision")
// 接口版本
.setVersion("2019-05-21")
// 接口协议
.setProtocol("HTTPS")
// 接口 HTTP 方法
.setMethod("POST")
.setAuthType("AK")
.setStyle("RPC")
// 接口 PATH
.setPathname("/")
// 接口请求体内容格式
.setReqBodyType("json")
// 接口响应体内容格式
.setBodyType("json");
return params;
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
// 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式。
com.aliyun.teaopenapi.Client client = Sample.createClient(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
com.aliyun.teaopenapi.models.Params params = Sample.createApiInfo();
// query params
java.util.Map<String, Object> queries = new java.util.HashMap<>();
queries.put("ServiceParameters", "{\"email\":\"example@alibabacloud.com\",\"ip\":\"x.x.x.x\",\"deviceToken\":\"******\"}");
queries.put("EventCode", "de_*****");
// runtime options
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
com.aliyun.teaopenapi.models.OpenApiRequest request = new com.aliyun.teaopenapi.models.OpenApiRequest()
.setQuery(com.aliyun.openapiutil.Client.query(queries));
// 复制代码运行请自行打印 API 的返回值
// 返回值为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。
client.callApi(params, request, runtime);
}
}
如使用场景风控(注册风险识别、营销风险识别):
#!/usr/bin/env xcrun swift
import Cocoa
import Foundation
import Tea
import AlibabacloudOpenApi
import AlibabaCloudOpenApiUtil
import TeaUtils
open class Client {
public static func createClient(_ accessKeyId: String?, _ accessKeySecret: String?) throws -> AlibabacloudOpenApi.Client {
var config: AlibabacloudOpenApi.Config = AlibabacloudOpenApi.Config([
"accessKeyId": accessKeyId as! String,
"accessKeySecret": accessKeySecret as! String
])
config.endpoint = "saf.ap-southeast-1.aliyuncs.com"
return AlibabacloudOpenApi.Client(config)
}
public static func createApiInfo() -> AlibabacloudOpenApi.Params {
var params: AlibabacloudOpenApi.Params = AlibabacloudOpenApi.Params([
"action": "ExecuteRequestSG",
"version": "2019-05-21",
"protocol": "HTTPS",
"method": "POST",
"authType": "AK",
"style": "RPC",
"pathname": "/",
"reqBodyType": "json",
"bodyType": "json"
])
return params as! AlibabacloudOpenApi.Params
}
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
public static func main(_ args: [String]?) async throws -> Void {
var client: AlibabacloudOpenApi.Client = try Client.createClient(ProcessInfo.processInfo.environment["ALIBABA_CLOUD_ACCESS_KEY_ID"], ProcessInfo.processInfo.environment["ALIBABA_CLOUD_ACCESS_KEY_SECRET"])
var params: AlibabacloudOpenApi.Params = Client.createApiInfo()
var queries: [String: Any] = [:]
queries["ServiceParameters"] = "{"email":"example@alibabacloud.com","ip":"x.x.x.x","deviceToken":"******"}";
queries["Service"] = "account_abuse_intl_pro";
var runtime: TeaUtils.RuntimeOptions = TeaUtils.RuntimeOptions([:])
var request: AlibabacloudOpenApi.OpenApiRequest = AlibabacloudOpenApi.OpenApiRequest([
"query": AlibabaCloudOpenApiUtil.Client.query(queries)
])
try await client.callApi(params as! AlibabacloudOpenApi.Params, request as! AlibabacloudOpenApi.OpenApiRequest, runtime as! TeaUtils.RuntimeOptions)
}
}
Client.main(CommandLine.arguments)
Python SDK
单击此处下载Python SDK源码。
安装SDK核心库。
如果您使用Python 2.x,执行以下命令,安装阿里云SDK核心库:
pip install aliyun-python-sdk-core
如果您使用Python 3.x,执行以下命令,安装阿里云SDK核心库:
pip install aliyun-python-sdk-core-v3
安装云产品SAF SDK。
pip install aliyun-python-sdk-saf
Python用例:
如使用决策引擎:
# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
import os
import sys
from typing import List
from alibabacloud_tea_openapi.client import Client as OpenApiClient
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_openapi_util.client import Client as OpenApiUtilClient
class Sample:
def __init__(self):
pass
@staticmethod
def create_client(
access_key_id: str,
access_key_secret: str,
) -> OpenApiClient:
"""
使用AK&SK初始化账号Client
@param access_key_id:
@param access_key_secret:
@return: Client
@throws Exception
"""
config = open_api_models.Config(
# 必填,您的 AccessKey ID,
access_key_id=access_key_id,
# 必填,您的 AccessKey Secret,
access_key_secret=access_key_secret
)
# Endpoint 请参考 https://api.aliyun.com/product/saf
config.endpoint = f'saf.ap-southeast-1.aliyuncs.com'
return OpenApiClient(config)
@staticmethod
def create_api_info() -> open_api_models.Params:
"""
API 相关
@param path: params
@return: OpenApi.Params
"""
params = open_api_models.Params(
# 接口名称,
action='RequestDecision',
# 接口版本,
version='2019-05-21',
# 接口协议,
protocol='HTTPS',
# 接口 HTTP 方法,
method='POST',
auth_type='AK',
style='RPC',
# 接口 PATH,
pathname=f'/',
# 接口请求体内容格式,
req_body_type='json',
# 接口响应体内容格式,
body_type='json'
)
return params
@staticmethod
def main(
args: List[str],
) -> None:
# 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
# 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式。
client = Sample.create_client(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
params = Sample.create_api_info()
# query params
queries = {}
queries['ServiceParameters'] = '{"email":"example@alibabacloud.com","ip":"x.x.x.x","deviceToken":"******"}'
queries['EventCode'] = 'de_*****'
# runtime options
runtime = util_models.RuntimeOptions()
request = open_api_models.OpenApiRequest(
query=OpenApiUtilClient.query(queries)
)
# 复制代码运行请自行打印 API 的返回值
# 返回值为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。
client.call_api(params, request, runtime)
if __name__ == '__main__':
Sample.main(sys.argv[1:])
如使用场景风控:
# -*- coding: utf-8 -*-
import os
import sys
from typing import List
from alibabacloud_tea_openapi.client import Client as OpenApiClient
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_openapi_util.client import Client as OpenApiUtilClient
class Sample:
def __init__(self):
pass
@staticmethod
def create_client(
access_key_id: str,
access_key_secret: str,
) -> OpenApiClient:
"""
使用AK&SK初始化账号Client
@param access_key_id:
@param access_key_secret:
@return: Client
@throws Exception
"""
config = open_api_models.Config(
# 必填,您的 AccessKey ID,
access_key_id=access_key_id,
# 必填,您的 AccessKey Secret,
access_key_secret=access_key_secret
)
# Endpoint 请参考 https://api.aliyun.com/product/saf
config.endpoint = f'saf.ap-southeast-1.aliyuncs.com'
return OpenApiClient(config)
@staticmethod
def create_api_info() -> open_api_models.Params:
"""
API 相关
@param path: params
@return: OpenApi.Params
"""
params = open_api_models.Params(
# 接口名称,
action='ExecuteRequestSG',
# 接口版本,
version='2019-05-21',
# 接口协议,
protocol='HTTPS',
# 接口 HTTP 方法,
method='POST',
auth_type='AK',
style='RPC',
# 接口 PATH,
pathname=f'/',
# 接口请求体内容格式,
req_body_type='json',
# 接口响应体内容格式,
body_type='json'
)
return params
@staticmethod
def main(
args: List[str],
) -> None:
# 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
# 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式。
client = Sample.create_client(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
params = Sample.create_api_info()
# query params
queries = {}
queries['ServiceParameters'] = '{"email":"example@alibabacloud.com","ip":"x.x.x.x","deviceToken":"******"}'
queries['Service'] = 'account_abuse_intl_pro'
# runtime options
runtime = util_models.RuntimeOptions()
request = open_api_models.OpenApiRequest(
query=OpenApiUtilClient.query(queries)
)
# 复制代码运行请自行打印 API 的返回值
# 返回值为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。
client.call_api(params, request, runtime)
if __name__ == '__main__':
Sample.main(sys.argv[1:])
PHP SDK
单击此处下载PHP SDK源码。
PHP用例:
如使用决策引擎:
<?php
namespace AlibabaCloud\SDK\Sample;
use Darabonba\OpenApi\OpenApiClient;
use AlibabaCloud\OpenApiUtil\OpenApiUtilClient;
use Darabonba\OpenApi\Models\Config;
use Darabonba\OpenApi\Models\Params;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\OpenApiRequest;
class Sample {
/**
* 使用AK&SK初始化账号Client
* @param string $accessKeyId
* @param string $accessKeySecret
* @return OpenApiClient Client
*/
public static function createClient($accessKeyId, $accessKeySecret){
$config = new Config([
// 必填,您的 AccessKey ID
"accessKeyId" => $accessKeyId,
// 必填,您的 AccessKey Secret
"accessKeySecret" => $accessKeySecret
]);
// Endpoint 请参考 https://api.aliyun.com/product/saf
$config->endpoint = "saf.ap-southeast-1.aliyuncs.com";
return new OpenApiClient($config);
}
/**
* API 相关
* @return Params OpenApi.Params
*/
public static function createApiInfo(){
$params = new Params([
// 接口名称
"action" => "RequestDecision",
// 接口版本
"version" => "2019-05-21",
// 接口协议
"protocol" => "HTTPS",
// 接口 HTTP 方法
"method" => "POST",
"authType" => "AK",
"style" => "RPC",
// 接口 PATH
"pathname" => "/",
// 接口请求体内容格式
"reqBodyType" => "json",
// 接口响应体内容格式
"bodyType" => "json"
]);
return $params;
}
/**
* @param string[] $args
* @return void
*/
public static function main($args){
// 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式。
$client = self::createClient(getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'));
$params = self::createApiInfo();
// query params
$queries = [];
$queries["ServiceParameters"] = "{\"email\":\"example@alibabacloud.com\",\"ip\":\"x.x.x.x\",\"deviceToken\":\"******\"}";
$queries["EventCode"] = "de_*****";
// runtime options
$runtime = new RuntimeOptions([]);
$request = new OpenApiRequest([
"query" => OpenApiUtilClient::query($queries)
]);
// 复制代码运行请自行打印 API 的返回值
// 返回值为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。
$client->callApi($params, $request, $runtime);
}
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
require_once $path;
}
Sample::main(array_slice($argv, 1));
如使用场景风控:
<?php
namespace AlibabaCloud\SDK\Sample;
use Darabonba\OpenApi\OpenApiClient;
use AlibabaCloud\OpenApiUtil\OpenApiUtilClient;
use Darabonba\OpenApi\Models\Config;
use Darabonba\OpenApi\Models\Params;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\OpenApiRequest;
class Sample {
/**
* 使用AK&SK初始化账号Client
* @param string $accessKeyId
* @param string $accessKeySecret
* @return OpenApiClient Client
*/
public static function createClient($accessKeyId, $accessKeySecret){
$config = new Config([
// 必填,您的 AccessKey ID
"accessKeyId" => $accessKeyId,
// 必填,您的 AccessKey Secret
"accessKeySecret" => $accessKeySecret
]);
// Endpoint 请参考 https://api.aliyun.com/product/saf
$config->endpoint = "saf.ap-southeast-1.aliyuncs.com";
return new OpenApiClient($config);
}
/**
* API 相关
* @return Params OpenApi.Params
*/
public static function createApiInfo(){
$params = new Params([
// 接口名称
"action" => "ExecuteRequestSG",
// 接口版本
"version" => "2019-05-21",
// 接口协议
"protocol" => "HTTPS",
// 接口 HTTP 方法
"method" => "POST",
"authType" => "AK",
"style" => "RPC",
// 接口 PATH
"pathname" => "/",
// 接口请求体内容格式
"reqBodyType" => "json",
// 接口响应体内容格式
"bodyType" => "json"
]);
return $params;
}
/**
* @param string[] $args
* @return void
*/
public static function main($args){
// 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式。
$client = self::createClient(getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'));
$params = self::createApiInfo();
// query params
$queries = [];
$queries["ServiceParameters"] = "{\"email\":\"example@alibabacloud.com\",\"ip\":\"x.x.x.x\",\"deviceToken\":\"******\"}";
$queries["Service"] = "account_abuse_intl_pro";
// runtime options
$runtime = new RuntimeOptions([]);
$request = new OpenApiRequest([
"query" => OpenApiUtilClient::query($queries)
]);
// 复制代码运行请自行打印 API 的返回值
// 返回值为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。
$client->callApi($params, $request, $runtime);
}
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
require_once $path;
}
Sample::main(array_slice($argv, 1));
C# SDK
单击此处下载C# SDK源码。
C#用例
如使用决策引擎:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Tea;
using Tea.Utils;
namespace AlibabaCloud.SDK.Sample
{
public class Sample
{
/**
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static AlibabaCloud.OpenApiClient.Client CreateClient(string accessKeyId, string accessKeySecret)
{
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
{
// 必填,您的 AccessKey ID
AccessKeyId = accessKeyId,
// 必填,您的 AccessKey Secret
AccessKeySecret = accessKeySecret,
};
// Endpoint 请参考 https://api.aliyun.com/product/saf
config.Endpoint = "saf.ap-southeast-1.aliyuncs.com";
return new AlibabaCloud.OpenApiClient.Client(config);
}
/**
* API 相关
* @param path params
* @return OpenApi.Params
*/
public static AlibabaCloud.OpenApiClient.Models.Params CreateApiInfo()
{
AlibabaCloud.OpenApiClient.Models.Params params_ = new AlibabaCloud.OpenApiClient.Models.Params
{
// 接口名称
Action = "RequestDecision",
// 接口版本
Version = "2019-05-21",
// 接口协议
Protocol = "HTTPS",
// 接口 HTTP 方法
Method = "POST",
AuthType = "AK",
Style = "RPC",
// 接口 PATH
Pathname = "/",
// 接口请求体内容格式
ReqBodyType = "json",
// 接口响应体内容格式
BodyType = "json",
};
return params_;
}
public static void Main(string[] args)
{
// 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式。
AlibabaCloud.OpenApiClient.Client client = CreateClient(Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
AlibabaCloud.OpenApiClient.Models.Params params_ = CreateApiInfo();
// query params
Dictionary<string, object> queries = new Dictionary<string, object>(){};
queries["ServiceParameters"] = "{\"email\":\"example@alibabacloud.com\",\"ip\":\"x.x.x.x\",\"deviceToken\":\"******\"}";
queries["EventCode"] = "de_*****";
// runtime options
AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
AlibabaCloud.OpenApiClient.Models.OpenApiRequest request = new AlibabaCloud.OpenApiClient.Models.OpenApiRequest
{
Query = AlibabaCloud.OpenApiUtil.Client.Query(queries),
};
// 复制代码运行请自行打印 API 的返回值
// 返回值为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。
client.CallApi(params_, request, runtime);
}
}
}
如使用场景风控:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Tea;
using Tea.Utils;
namespace AlibabaCloud.SDK.Sample
{
public class Sample
{
/**
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static AlibabaCloud.OpenApiClient.Client CreateClient(string accessKeyId, string accessKeySecret)
{
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
{
// 必填,您的 AccessKey ID
AccessKeyId = accessKeyId,
// 必填,您的 AccessKey Secret
AccessKeySecret = accessKeySecret,
};
// Endpoint 请参考 https://api.aliyun.com/product/saf
config.Endpoint = "saf.ap-southeast-1.aliyuncs.com";
return new AlibabaCloud.OpenApiClient.Client(config);
}
/**
* API 相关
* @param path params
* @return OpenApi.Params
*/
public static AlibabaCloud.OpenApiClient.Models.Params CreateApiInfo()
{
AlibabaCloud.OpenApiClient.Models.Params params_ = new AlibabaCloud.OpenApiClient.Models.Params
{
// 接口名称
Action = "ExecuteRequestSG",
// 接口版本
Version = "2019-05-21",
// 接口协议
Protocol = "HTTPS",
// 接口 HTTP 方法
Method = "POST",
AuthType = "AK",
Style = "RPC",
// 接口 PATH
Pathname = "/",
// 接口请求体内容格式
ReqBodyType = "json",
// 接口响应体内容格式
BodyType = "json",
};
return params_;
}
public static void Main(string[] args)
{
// 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式。
AlibabaCloud.OpenApiClient.Client client = CreateClient(Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
AlibabaCloud.OpenApiClient.Models.Params params_ = CreateApiInfo();
// query params
Dictionary<string, object> queries = new Dictionary<string, object>(){};
queries["ServiceParameters"] = "{\"email\":\"example@alibabacloud.com\",\"ip\":\"x.x.x.x\",\"deviceToken\":\"******\"}";
queries["Service"] = "account_abuse_intl_pro";
// runtime options
AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
AlibabaCloud.OpenApiClient.Models.OpenApiRequest request = new AlibabaCloud.OpenApiClient.Models.OpenApiRequest
{
Query = AlibabaCloud.OpenApiUtil.Client.Query(queries),
};
// 复制代码运行请自行打印 API 的返回值
// 返回值为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。
client.CallApi(params_, request, runtime);
}
}
}
Golang SDK
单击此处下载Golang SDK源码。
Golang用例:
使用决策引擎调用:
package main
import (
"os"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
openapiutil "github.com/alibabacloud-go/openapi-util/service"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
)
/**
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
func CreateClient (accessKeyId *string, accessKeySecret *string) (_result *openapi.Client, _err error) {
config := &openapi.Config{
// 必填,您的 AccessKey ID
AccessKeyId: accessKeyId,
// 必填,您的 AccessKey Secret
AccessKeySecret: accessKeySecret,
}
// Endpoint 请参考 https://api.aliyun.com/product/saf
config.Endpoint = tea.String("saf.ap-southeast-1.aliyuncs.com")
_result = &openapi.Client{}
_result, _err = openapi.NewClient(config)
return _result, _err
}
/**
* API 相关
* @param path params
* @return OpenApi.Params
*/
func CreateApiInfo () (_result *openapi.Params) {
params := &openapi.Params{
// 接口名称
Action: tea.String("RequestDecision"),
// 接口版本
Version: tea.String("2019-05-21"),
// 接口协议
Protocol: tea.String("HTTPS"),
// 接口 HTTP 方法
Method: tea.String("POST"),
AuthType: tea.String("AK"),
Style: tea.String("RPC"),
// 接口 PATH
Pathname: tea.String("/"),
// 接口请求体内容格式
ReqBodyType: tea.String("json"),
// 接口响应体内容格式
BodyType: tea.String("json"),
}
_result = params
return _result
}
func _main (args []*string) (_err error) {
// 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式。
client, _err := CreateClient(tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")), tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")))
if _err != nil {
return _err
}
params := CreateApiInfo()
// query params
queries := map[string]interface{}{}
queries["ServiceParameters"] = tea.String("{\"email\":\"example@alibabacloud.com\",\"ip\":\"x.x.x.x\",\"deviceToken\":\"******\"}")
queries["EventCode"] = tea.String("de_*****")
// runtime options
runtime := &util.RuntimeOptions{}
request := &openapi.OpenApiRequest{
Query: openapiutil.Query(queries),
}
// 复制代码运行请自行打印 API 的返回值
// 返回值为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。
_, _err = client.CallApi(params, request, runtime)
if _err != nil {
return _err
}
return _err
}
func main() {
err := _main(tea.StringSlice(os.Args[1:]))
if err != nil {
panic(err)
}
}
使用场景风控调用:
package main
import (
"os"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
openapiutil "github.com/alibabacloud-go/openapi-util/service"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
)
/**
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
func CreateClient (accessKeyId *string, accessKeySecret *string) (_result *openapi.Client, _err error) {
config := &openapi.Config{
// 必填,您的 AccessKey ID
AccessKeyId: accessKeyId,
// 必填,您的 AccessKey Secret
AccessKeySecret: accessKeySecret,
}
// Endpoint 请参考 https://api.aliyun.com/product/saf
config.Endpoint = tea.String("saf.ap-southeast-1.aliyuncs.com")
_result = &openapi.Client{}
_result, _err = openapi.NewClient(config)
return _result, _err
}
/**
* API 相关
* @param path params
* @return OpenApi.Params
*/
func CreateApiInfo () (_result *openapi.Params) {
params := &openapi.Params{
// 接口名称
Action: tea.String("ExecuteRequestSG"),
// 接口版本
Version: tea.String("2019-05-21"),
// 接口协议
Protocol: tea.String("HTTPS"),
// 接口 HTTP 方法
Method: tea.String("POST"),
AuthType: tea.String("AK"),
Style: tea.String("RPC"),
// 接口 PATH
Pathname: tea.String("/"),
// 接口请求体内容格式
ReqBodyType: tea.String("json"),
// 接口响应体内容格式
BodyType: tea.String("json"),
}
_result = params
return _result
}
func _main (args []*string) (_err error) {
// 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式。
client, _err := CreateClient(tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")), tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")))
if _err != nil {
return _err
}
params := CreateApiInfo()
// query params
queries := map[string]interface{}{}
queries["ServiceParameters"] = tea.String("{\"email\":\"example@alibabacloud.com\",\"ip\":\"x.x.x.x\",\"deviceToken\":\"******\"}")
queries["Service"] = tea.String("account_abuse_intl_pro")
// runtime options
runtime := &util.RuntimeOptions{}
request := &openapi.OpenApiRequest{
Query: openapiutil.Query(queries),
}
// 复制代码运行请自行打印 API 的返回值
// 返回值为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。
_, _err = client.CallApi(params, request, runtime)
if _err != nil {
return _err
}
return _err
}
func main() {
err := _main(tea.StringSlice(os.Args[1:]))
if err != nil {
panic(err)
}
}