认证码
认证码是接口访问的一个安全验证码
认证码生成规则:SHA256(应用ID + 应用密钥 + Saas ID + 用户ID)
接入概述
与即时通讯集成,开发者需要按照以下步骤完成:- 注册一个应用
- 编写接口调用函数
- 根据接口文档实现业务逻辑
第一步:注册一个应用
第二步:编写接口调用函数(PHP代码示例)
/* * 声明 * 所有的结果返回格式是 status data err_code err_msg * status 0 错误 1 成功 * data 返回结果数据 * err_code 错误码 * err_msg 错误信息 * */ class IMSdk { //开发者需要这里---------------- private $app_id = '1476263646' ; private $app_secret = 'A792D4A284DAB50' ; private $data_type = 'json' ; private $rest_site = 'http://101.69.228.146:8000' ; //当前登录的SSID ,USERID,USERNAME private $ssid = ''; private $uid = ''; private $uname = ''; /** * 初始化 */ public function __construct($ssid,$uid,$uname){ $this->ssid = $ssid ; $this->uid = $uid ; $this->uname = $uname ; } /** * 验证令牌的详细信息 */ public function getTokenInfo($token){ $param = array('token'=>$token); $res = $this->sendRest('oauth', 'get_token_info',$param) ; return $res ; } /** * 请求接口 * @param unknown $module 模块名称 * @param unknown $method 方法 * @param unknown $ssid 企业ID * @param unknown $uid 用户ID * @param unknown $uname 用户姓名 * @param unknown $param 参数 * @param unknown $timeout 超时时间 * @return {status:0/1,data:'',err_code:0,err_msg:''} */ private function sendRest($module,$method,$param = array(),$timeout = 10){ //得到接口地址 $url = $this->rest_site . "/api/$module/$method.html"; //得到必须参数 $param['ssid'] = $this->ssid; $param['uid'] = $this->uid; $param['uname'] = $this->uname; $param['app_id'] = $this->app_id; $param['data_type'] = $this->data_type; $param['authen'] = $this->getAuthen(); $data = "" ; foreach($param as $key=>$value) { $data .= ($data == ""?"":"&") .$key . "=" . $value ; } //CURL提交数据 $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt ($ch, CURLOPT_POST, 1);//POST curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 2);//连接超时 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); //不返回状态 curl_setopt ($ch, CURLOPT_TIMEOUT, $timeout);//执行超时 $res = curl_exec($ch); curl_close($ch); //将字符串转为对象 $res = json_decode($res); return $res; } /** * 得到认证码 * @param unknown $ssid * @param unknown $uid * @return string */ private function getAuthen(){ return hash('sha256',$this->app_id . $this->app_secret . $this->ssid . $this->uid); } }
第三步:根据接口文档实现业务逻辑
//引入Sdk文件 require 'IMSdk.class.php'; //实例化SDK $ssid = 'aipu'; $uid = '1'; $uname = 'admin'; $imsdk = new IMSdk($ssid,$uid,$uname); //验证令牌 $token = '80144BAE-6CE2-264D-CF27-85B2F3B51BF7' ; $res = $imsdk->getTokenInfo($token);
示例代码下载:下载示例代码