http://gw.api.alibaba.com/dev/doc/intl/sys_auth.htm?ns=aliexpress.open#concept
关键字段说明
1、appKey和appSecret
appKey是app的唯一标识,appSecret是app的密钥,它们相当于用户名和密码。注册app之后就会生成一个appKey和appSecret,请妥善保存。
2、code
即临时令牌。当用户在开放平台访问app前,需对app进行授权,用户授权后,app方可访问用户的隐私数据。用户在完成授权之后,会跳转到redirect_uri,并带上临时令牌code,app再用code从开放平台后台接口换取access_token。code为一次性令牌,有效期为2分钟。
3、accessToken
即用户授权令牌,为用户一次会话的授权标识,有效期10小时。在获得code后,通过调用开放平台后台接口getToken来获取accessToken。 App在访问用户隐私数据时,需要带上accessToken,也只有accessToken才能作为访问的凭证,其他token如code和refreshToken都不能直接作为访问凭证,需要转换为accessToken之后才能访问用户隐私数据。
4、refreshToken
即长时令牌,有效期半年。当access_token过期后,可以使用refreshToken换取新的accessToken访问用户数据。
5、redirect_uri 即回调地址,指的是App发起请求时,所传的回调地址参数,必填。请参考授权流程中的说明。 API参考说明地址: http://gw.api.alibaba.com/dev/doc/sys_auth.htm?ns=aliexpress.open 速卖通API官网地址
开发逻辑流程
伸手党福利
代码虽然很乱,但感觉很对的起伸手党了,哈哈!!!
/*
* 获取信息模板
*/
public static function getAccountTemplates($config)
{
$config['moduleStatus'] = 'approved';
$config['pageIndex'] = 1;
$apiname = "api.findAeProductDetailModuleListByQurey";
//获取信息模板
$accountTemplats = Apiservice::getinfo($config,$apiname);
return $accountTemplats;
}
/*
* 获取帐号appkey
*/
public static function getacountinfo($account)
{
$account = DB::SELECT('appkey','account','appsecret')
->from('account')
->where('account','=',$account)
->execute('system')
->current();
return $account;
}
//新方法获取通行证
public static function newupdatetoken($account)
{
$row = Apiservice::newgetaccounts($account['account']);
if((strtotime($row['access_create_time'])+10*3600-60*10)<time()){
$ali = new Getapi();
$accessToken =
$ali->getaccesstoken($row['refresh_token'],$account['appkey'],$account['appsecret']);
//通过refreshToken换取accessToken
if(isset($accessToken['error'])){
return $accessToken;
}
$set = array(
'access_token' => $accessToken['access_token'],
'access_create_time' => date('Y-m-d H:i:s',time()),
);
$sql = "UPDATE `erp_aliexpress_accounts`
SET access_token='".$accessToken['access_token']."',access_create_time='".date('Y-m-d H:i:s')."'
WHERE `aliId`=".$accessToken['aliId'];
DB::query(Database::INSERT,$sql)
->execute('system');
$row['access_token'] = $accessToken['access_token'];
}elseif((strtotime($row['refresh_create_time'])+180*24*3600)<time()){
$row['access_token'] = NULL;
}
return $row['access_token'];
}
/**
* 获取速卖通新帐号
* Enter description here ...
* @param unknown_type $account
*/
public static function newgetaccounts($account = '',$fields = '')
{
if(empty($account)){
return '账户为空';
}else{
$result = DB::select('access_token','refresh_token','access_create_time','refresh_create_time')
->from('erp_aliexpress_accounts')
->where('aliexpress_account','=',$account)
->execute('system')
->current();
}
return $result;
}
/*
* 获取帐号appkey
*/
public static function getacountinfo($account)
{
$account = DB::SELECT('appkey','account','appsecret')
->from('account')
->where('account','=',$account)
->execute('system')
->current();
return $account;
}
/*
* 公用方法
*/
public static function getinfo($config,$apiname)
{
$appkey = Apiservice::getacountinfo($config['account']);
$config['appkey'] = $appkey['appkey'];
//获取信息模板
$api = new getapi();
$result = $api::api($config,$apiname);
return $result;
}
const URL = 'http://gw.api.alibaba.com/openapi/';
const SITE = 'aliexpress';
/*
* api公用方法
*/
public function api($set,$apiname)
{
if(empty($set['access_token'])){
return FALSE;
}
$api_url = 'param2/1/aliexpress.open/'.$apiname.'/'.$set['appkey'];
$url = Getapi::URL.$api_url;
$data = http_build_query($set);
$result = Getapi::sendRequestCurl($url,$data);
return $result;
}
/**
* 发送请求
* Enter description here ...
* @param unknown_type $url
* @param unknown_type $data
*/
protected static function sendRequestCurl($url,$data)
{
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch,CURLOPT_POST,TRUE);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_setopt($ch,CURLOPT_TIMEOUT,300);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response,TRUE);
return $response;
}
/*
* 通过refreshToken换取accessToken
* Enter description here ...
* @param unknown_type $refreshToken
*/
public function getaccesstoken($refreshtoken,$appkey,$appsecret)
{
$code_arr = array(
'client_id' => $appkey,
'grant_type' => 'refresh_token',
'client_secret' => $appsecret,
'refresh_token' => $refreshtoken,
);
$sign_str = 'param2/1/system.oauth2/refreshToken/544880';
$code_sign = Getapi::apiSign($appsecret,$code_arr,$sign_str,$appsecret);
$getTokenUrl = 'https://gw.api.alibaba.com/openapi/param2/1/system.oauth2/refreshToken/'.$appkey;
$data =
'grant_type=refresh_token&refresh_token='.$refreshtoken.'&client_id='.$appkey.'&client_secret='.$appsecret.'&_aop_signature='.$code_sign;
$result = $this->sendRequestCurl($getTokenUrl,$data);
return $result;
}
/**
* 签名串组规则
* Enter description here ...
* @param unknown_type $redirectUrl
*/
protected static function apiSign($appsecret,$code_arr,$sign_str = '')
{
ksort($code_arr);
foreach($code_arr as $key => $val){
$sign_str .= $key.$val;
}
$code_sign = strtoupper(bin2hex(hash_hmac("sha1",$sign_str,$appsecret,TRUE)));
return $code_sign;
}