在对接支付宝授权登录的时候 遇到很多 整理一下 支付宝文档是真坑 文档多乱七八糟 在APP授权登录的时候 要生成一个签名 与 支付宝规定好的字符串 拼接之后返回给前端 但是我在对接的时候 然后去找文档 文档地址 然后我又根据这个文档 文档地址 要组成一个这个形式的字符串 然后我按照他说的去生成 然后又找到了 这个文档 文档链接 根据里面参数构造 来来回回整不好 签名一直有问题 然后联系了客服 给到了这个文档 文档链接
上代码吧
require VENDOR_PATH . 'alipay-sdk-php-all/aop/AopClient.php';
$target_id = time();
$str = 'apiname=com.alipay.account.auth&app_id=2021002172672046&app_name=mc&auth_type=AUTHACCOUNT&biz_type=openservice&method=alipay.open.auth.sdk.code.get&pid=2088241235324422&product_id=APP_FAST_LOGIN&scope=kuaijie&sign_type=RSA2&target_id='.$target_id;
$json = json_encode($str);
$aop = new \AopClient();
$privatekey = $this->rsaPrivateKey;
$signType = "RSA2";
$sign = $aop->alonersaSign($str, $privatekey, $signType, false);
$sign = $str.'&sign='.urlencode($sign);
获取到 code之后 就要用code获取token
require VENDOR_PATH . 'alipay-sdk-php-all/aop/AopClient.php';
require VENDOR_PATH . 'alipay-sdk-php-all/aop/request/AlipayUserInfoShareRequest.php';
require VENDOR_PATH . 'alipay-sdk-php-all/aop/request/AlipaySystemOauthTokenRequest.php';
$this->checkLogin();
$user = $this->userInfo;
$code = input('data');
$json = json_decode(htmlspecialchars_decode($code), true);
$code = $json['data']['authCode'];
$aopToken = new \AopClient();
$aopToken->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aopToken->appId = $this->appId;
$aopToken->rsaPrivateKey = $this->rsaPrivateKey;
$aopToken->alipayrsaPublicKey = $this->alipayrsaPublicKey;
$aopToken->apiVersion = '1.0';
$aopToken->signType = 'RSA2';
$aopToken->postCharset = 'UTF-8';
$aopToken->format = 'json';
$request = new \AlipaySystemOauthTokenRequest();
$request->setGrantType("authorization_code");
$request->setCode($code);
$result = $aopToken->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$accessToken = $result->$responseNode->access_token;
if ($result->$responseNode->access_token) {
$aop = new \AopClient();
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->appId = $this->appId;
$aop->rsaPrivateKey = $this->rsaPrivateKey;
$aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset = 'UTF-8';
$aop->format = 'json';
$requests = new \AlipayUserInfoShareRequest();
$results = $aop->execute($requests, $accessToken);
$resultCode = $results->alipay_user_info_share_response->code;
if (!empty($resultCode) && $resultCode == 10000) {
$add_data = array(
'avatar' => $results->alipay_user_info_share_response->avatar,
'nick_name' => $results->alipay_user_info_share_response->nick_name,
'province' => $results->alipay_user_info_share_response->province,
'city' => $results->alipay_user_info_share_response->city,
'gender' => $results->alipay_user_info_share_response->gender,
'user_id' => $results->alipay_user_info_share_response->user_id,
);
} else {
$this->error('获取失败');
}
}
最后获取到用户信息存储绑定就行
|