当前位置: 首页 > ds >正文

thinkphp8.1 调用巨量广告API接口,刷新token

1、在mysql中建立表sys_token;

CREATE TABLE `sys_token` (`id` int UNSIGNED NOT NULL,`access_token` varchar(50) COLLATE utf8mb4_general_ci NOT NULL,`expires_in` timestamp NOT NULL,`refresh_token` varchar(50) COLLATE utf8mb4_general_ci NOT NULL,`refresh_token_expires_in` timestamp NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='API token';

备注:expires_in\refresh_token_expires_in 巨量引擎返回的是纯数字,单位:秒,需要你在tp里转换成年月日时分秒;

2、第一次获取令牌时,官方会给你auth_code,你根据你的app_id,secret,auth_code获取令牌,并存入database中;

3、由于令牌的有效期是1天,刷新令牌有效期是30天;令牌过期了需要刷新令牌;

use think\response\Json;
use think\facade\Db;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
use DateTime;class Index
{protected string $accessToken;public function __construct(){$data = Db::name('sys_token')->find(1);$expired_time = $data['expires_in']; // accessToken的过期时间$result = (new DateTime() > new DateTime($expired_time) ? 1 : 0);if ($result===1) {$this->getRefreshToken($data['refresh_token']); // 过期了刷新令牌} else {$this->accessToken = $data['access_token'];}}public function index(){// 在调用任意接口时,需要有访问令牌;return $this->accessToken;}public function getRefreshToken(string $refreshToken): Json{$client = new Client();$headers = ['Content-Type' => 'application/json',];// 请求体数据$body = ['app_id' => '你的app_id','secret' => '您的secret','refresh_token' => $refreshToken,];try {// 异步请求$response = $client->request('POST', 'https://api.oceanengine.com/open_api/oauth2/refresh_token/', ['headers' => $headers,'json' => $body,  // 使用 'json' 自动编码为 JSON]);// 获取响应体并输出$responseBody = $response->getBody()->getContents();$result = json_decode($responseBody, true);$data = ['id'=>1,'access_token' => $result['data']['access_token'],'expires_in' => $result['data']['expires_in'],'refresh_token' => $result['data']['refresh_token'],'refresh_token_expires_in' => $result['data']['refresh_token_expires_in']];Db::name('sys_token')->update($data);return json(['code'=>0, 'msg'=>'刷新token成功']);} catch (RequestException $e) {// 错误处理return json(['code'=>1, 'msg'=>$e->getMessage()]);}}
}

测试环境:centOS stream 9、php8.3、thinkphp8.1; 欢迎大家指正........

http://www.xdnf.cn/news/12365.html

相关文章:

  • 测试W5500的第11步_使用ARP解析IP地址对应的MAC地址
  • 以光量子为例,详解量子获取方式
  • 使用 async/await 封装 wx.request 请求
  • NLP学习路线图(二十五):注意力机制
  • 会计 - 金融负债和权益工具
  • NC | 基于语言模型的药物设计新方法
  • jenkins脚本查看及备份
  • AppTrace技术全景:开发者视角下的工具链与实践经验
  • 人工智能的社交课:从博弈游戏到健康关怀
  • .Net Framework 4/C# LINQ*
  • Python----目标检测(yolov5-7.0安装及训练细胞)
  • 国芯思辰|SCS5501/5502芯片组打破技术壁垒,重构车载视频传输链路,兼容MAX9295A/MAX96717
  • Python爬虫实战:研究RoboBrowser库相关技术
  • [大语言模型]在个人电脑上部署ollama 并进行管理,最后配置AI程序开发助手.
  • 国内头部的UWB企业介绍之品铂科技
  • 关于根据词库分词的算法逻辑实现(最长词汇匹配原则)
  • 6月5日day45
  • 《Pytorch深度学习实践》ch8-多分类
  • 空间利用率提升90%!小程序侧边导航设计与高级交互实现
  • 【p2p、分布式,区块链笔记 MESH】Bluetooth蓝牙通信拓扑与操作 BR/EDR(经典蓝牙)和 BLE
  • 从0开始学习R语言--Day17--Cox回归
  • 深度学习张量
  • Postman接口测试之postman设置接口关联,实现参数化
  • selinux firewalld
  • 将HTML内容转换为Canvas图像,主流方法有效防止文本复制
  • RunnablePassthrough介绍和透传参数实战
  • Kinova机械臂在Atlas手术导航系统中的核心作用
  • 外网访问内网服务器常用的三种简单操作步骤方法,本地搭建网址轻松让公网连接
  • java32
  • Monorepo架构: Lerna、NX、Turbo等对比与应用分析