ByteDanceAccessToken.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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'=>ByteDance::appId(),
  14. 'secret'=>ByteDance::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. public function getWangKa()
  23. {
  24. $token=Cache::get($this->cacheName);
  25. if(!$token){
  26. $request=ByteDance::httpPost('https://developer.toutiao.com/api/apps/v2/token',[
  27. 'appid'=>ByteDance::appIdTwo(),
  28. 'secret'=>ByteDance::appSecretTwo(),
  29. 'grant_type'=>'client_credential',
  30. ]);
  31. $token=$request['access_token'];
  32. Cache::set($this->cacheName,$token,$request['expires_in']-10);
  33. }
  34. return $token;
  35. }
  36. /**
  37. * @param mixed $appId
  38. */
  39. public function setAppId($appId)
  40. {
  41. $this->appId = $appId;
  42. return $this;
  43. }
  44. /**
  45. * @param mixed $appSecret
  46. */
  47. public function setAppSecret($appSecret)
  48. {
  49. $this->appSecret = $appSecret;
  50. return $this;
  51. }
  52. }