1. 创建 RAM 用户
2. 编辑个人授权策略
添加: AdministratorAccess
3. 下载SDK, 复制aliyun-php-sdk-core,aliyun-php-sdk-cloudauth目录
https://github.com/aliyun/aliyun-openapi-php-sdk
core/config.php里增加
//config sdk auto load path.
Autoloader::addAutoloadPath("aliyun-php-sdk-cloudauth");
4. 调用代码, 需要修改的内容有($biz, $accesskeyid,$accesssecret)
define('DS', '/');
define('APP_PATH', realpath(dirname(__FILE__).DS.'..'.DS.'application').DS);
include_once APP_PATH . 'third/aliyun/aliyun-php-sdk-core/Config.php';
use Cloudauth\Request\V20180807 as cloudauth;
$iClientProfile = DefaultProfile::getProfile("cn-hangzhou", $accessKeyId, $accessSecret);
$iClientProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", "Cloudauth", "cloudauth.aliyuncs.com");
$client = new DefaultAcsClient($iClientProfile);
$biz = "yannihealth"; //您在控制台上创建的、采用RPBasic认证方案的认证场景标识, 创建方法:https://help.aliyun.com/document_detail/59975.html
$ticketId = guid(); //认证ID, 由使用方指定, 发起不同的认证任务需要更换不同的认证ID
$token = null; //认证token, 表达一次认证会话
$statusCode = -1; //-1 未认证, 0 认证中, 1 认证通过, 2 认证不通过
//1. 服务端发起认证请求, 获取到token
//GetVerifyToken接口文档:https://help.aliyun.com/document_detail/57050.html
$getVerifyTokenRequest = new cloudauth\GetVerifyTokenRequest();
$getVerifyTokenRequest->setBiz($biz);
$getVerifyTokenRequest->setTicketId($ticketId);
try {
$response = $client->getAcsResponse($getVerifyTokenRequest);
$token = $response->Data->VerifyToken->Token; //token默认30分钟时效,每次发起认证时都必须实时获取
Help::print_json(0, ['token' => $token]);//输出token
} catch (Exception $e) {
print_r ($e->getTrace());
}