一、说明
二、代码
<?php
namespace app\common\libs;
use app\common\exception\BaseException;
/**
* Class DouYinApi
* @package app\common\libs
*/
class DouYinApi
{
private $host; //抖音接口API,API调用指南:https://op.jinritemai.com/docs/guide-docs/148/814
private $appKey; //appKey
private $appSecret; //appSecret
private $accessToken; //访问令牌
private $refreshToken; //刷新令牌
private $versionNumber; //API协议版本,当前版本为 2
private $versionNumberStr; //API协议版本,当前版本为 v2
public function __construct()
{
$this->host = 'https://openapi-fxg.jinritemai.com'; //接口访问地址
$this->appKey = '你的抖音后台的appKey';
$this->appSecret = '你的抖音后台的appSecret';
$this->versionNumber = '2';
$this->versionNumberStr = 'v' . $this->versionNumber;
//获取access_token,refresh_token放到最后,如果其他的如versionNumber在后面设置则报错:"v不可为空",因为handleToken中调用了versionNumber,但versionNumber此时的值为NULL
$result = self::handleToken(); //创建Token
// $result = self::handleToken(false); //刷新Token:提示-"缺少code",需要建一张第三方表存抖音该店铺的access_token,refresh_token,expire_time信息
$this->accessToken = $result['access_token']; //用于出创建token接口之外的其他接口
$this->refreshToken = $result['refresh_token']; //用于刷新token接口
}
/**
* 处理(创建/刷新)Token的方法
* 开发指南 > 产品功能 > 授权介绍 -> 自用型应用店铺授权流程:https://op.jinritemai.com/docs/guide-docs/9/21
* @param bool $createToken 是否调用创建Token的方法
* @return array
* @throws BaseException
*/
public function handleToken($createToken = true)
{
if ($createToken) {
//调用创建token接口
$param