ByteDanceAccessToken.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace app\service\byte_dance;
  3. use think\Cache;
  4. class ByteDanceAccessToken implements ByteDanceInterface {
  5. protected $appId;
  6. protected $appSecret;
  7. protected $cacheName='cache_bytedance_access_token';
  8. public function get()
  9. {
  10. $token=Cache::get($this->cacheName);
  11. if(!$token){
  12. $request=ByteDance::httpPost('https://developer.toutiao.com/api/apps/v2/token',[
  13. 'appid'=>$this->appId,
  14. 'secret'=>$this->appSecret,
  15. 'grant_type'=>'client_credential',
  16. ]);
  17. $token=$request['access_token'];
  18. Cache::set($this->cacheName,$token,$request['expires_in']-10);
  19. }
  20. return $token;
  21. }
  22. /**
  23. * @param mixed $appId
  24. */
  25. public function setAppId($appId)
  26. {
  27. $this->appId = $appId;
  28. return $this;
  29. }
  30. /**
  31. * @param mixed $appSecret
  32. */
  33. public function setAppSecret($appSecret)
  34. {
  35. $this->appSecret = $appSecret;
  36. return $this;
  37. }
  38. }