淘宝oauth的grant_type参数含义及grant type is empty错误

时间:2018-08-10 21149 主题:淘客开发教程
导读《淘宝oauth的grant_type参数含义及grant type is empty错误》要点:大部分人不会忘了设置grant_type,这个报错最可能的原因还是phpcurl设置问题

Oauth支持的5类 grant_type 及说明

  1. authorization_code 授权码模式(即先登录获取code,再获取token)

  2. password 密码模式(将用户名,密码传过去,直接获取token)

  3. client_credentials 客户端模式(无用户,用户向客户端注册,然后客户端以自己的名义向’服务端’获取资源)

  4. implicit 简化模式(在redirect_uri 的Hash传递token; Auth客户端运行在浏览器中,如JS,Flash)

  5. refresh_token 刷新access_token


如果调用时出现oauth grant type is empty错误,一般是忘了设置grant_type,这个报错还有可能的原因还是php curl设置问题。

事实上淘宝SDK有封装curl操作,不需要自己去写这些代码。

封装的函数在TopClient中的curl()方法。

public function curl($url, $postFields = null)
{
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_FAILONERROR, false);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 if ($this->readTimeout) {
  curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout);
 }
 if ($this->connectTimeout) {
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
 }
 //https 请求
 if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
 }
 
 if (is_array($postFields) && 0 < count($postFields))
 {
  $postBodyString = "";
  $postMultipart = false;
  foreach ($postFields as $k => $v)
  {
   if("@" != substr($v, 0, 1))//判断是不是文件上传
   {
    $postBodyString .= "$k=" . urlencode($v) . "&"; 
   }
   else//文件上传用multipart/form-data,否则用www-form-urlencoded
   {
    $postMultipart = true;
   }
  }
  unset($k, $v);
  curl_setopt($ch, CURLOPT_POST, true);
  if ($postMultipart)
  {
   curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  }
  else
  {
   curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
  }
 }
 $reponse = curl_exec($ch);
 
 if (curl_errno($ch))
 {
  throw new Exception(curl_error($ch),0);
 }
 else
 {
  $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  if (200 !== $httpStatusCode)
  {
   throw new Exception($reponse,$httpStatusCode);
  }
 }
 curl_close($ch);
 return $reponse;
}
转载请注明本页网址:https://www.veapi.cn/taokelianmeng/262.html
淘客标签:淘宝oauth

在线
客服

会员请加QQ群交流:

微信
咨询

加微信咨询
顶部