Init.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace DouYinGameOpen;
  3. /**
  4. * 通用管理
  5. */
  6. class Init extends BaseApi{
  7. /**
  8. * 2.1 获取client_token
  9. * - client_token的有效时间为 2 个小时,重复获取 client_token 后会使上次的 client_token 失效(但有 5 分钟的缓冲时间,连续多次获取 client_token 只会保留最新的两个 client_token)。
  10. * - 禁止频繁调用 client_token 接口(频控规则:5 分钟内超过 500 次接口调用,接口报错,错误码 10020)。
  11. *
  12. * @return mixed
  13. */
  14. public function client_token()
  15. {
  16. $api_url = self::Douyin_Url . '/webcast/game/oauth/client_token/';
  17. $params = [
  18. 'app_id' => $this->app_id,
  19. 'app_secret' => $this->app_secret,
  20. ];
  21. return $this->https_post($api_url, $params);
  22. // $result = [
  23. // 'data' => [
  24. // "access_token" => "clt.4874669616136b06ab3d8ca4550adf07OGg6sVIrOKjp2p3w7fJYLQhcC6Z8_hl",
  25. // "description" => "",
  26. // "error_code" => 0,
  27. // "expires_in" => 7200,
  28. // "log_id" => "20250522152945A1F02B01A0C1DC0F8CE2",
  29. // ],
  30. // "message" => "success",
  31. // ];
  32. }
  33. /**
  34. * 2.2 获取access_token
  35. * - access_token 有效期仅有 15 天
  36. *
  37. * @param $code
  38. *
  39. * @return mixed
  40. */
  41. public function access_token($code)
  42. {
  43. $api_url = self::Douyin_Url . '/webcast/game/oauth/access_token/';
  44. $params = [
  45. 'app_id' => $this->app_id,
  46. 'app_secret' => $this->app_secret,
  47. 'code' => $code,
  48. ];
  49. return $this->https_post($api_url, $params);
  50. // $result = [
  51. // 'data' => [
  52. // "access_token" => "act.3.Db88ppxc_2nckhTcjbE9Z2-lZ9PMcp4CrzRQckv7rrzaPOT8a7TkU4Ls9W9dCCmOok13FWfiE1hySTT0PtyxnxaG4Fs_wIbTPaQa729YfZ54kZRow5Ic2W8gZtpL7n-E1ArbGPp-bG0lUZ5AfX_Cf01NVwSfqBVv3eezpQ==_lf",
  53. // "description" => "",
  54. // "error_code" => "0",
  55. // "expires_in" => "1296000",
  56. // "log_id" => "20250521100953C76761CDD18B97348C5A",
  57. // "open_id" => "_0003YHzVQz7jHbDlXzei6geJ0FR3hhRoVbQ",
  58. // "refresh_expires_in" => "2592000",
  59. // "refresh_token" => "rft.62a7620229764c902b1c0f7da87c5815HEPO6yeLoPKtFxwdA0BvxOp1In5F_lf",
  60. // "scope" => "user_info",
  61. // ],
  62. // "message" => "success",
  63. // ];
  64. }
  65. /**
  66. * 2.3 刷新access_token
  67. * - 刷新 access_token,有效期为 30 天。
  68. * - 最多能再获取 5 次新的 refresh_token,最长续期为 15 + 30 + 30 * 5 = 195 天。
  69. *
  70. * @param $refresh_token
  71. *
  72. * @return mixed
  73. */
  74. public function refresh_token($refresh_token)
  75. {
  76. $api_url = self::Douyin_Url . '/webcast/game/oauth/refresh_token/';
  77. $params = [
  78. 'app_id' => $this->app_id,
  79. 'refresh_token' => $refresh_token,
  80. ];
  81. return $this->https_post($api_url, $params);
  82. }
  83. /**
  84. * 2.4 刷新refresh_token
  85. * - 通过旧的 refresh_token 获取新的 refresh_token,调用后旧 refresh_token 会失效,新 refresh_token 有 30 天有效期。最多只能获取 5 次新的 refresh_token,5 次过后需要用户重新授权。
  86. *
  87. * @param $refresh_token
  88. *
  89. * @return mixed
  90. */
  91. public function renew_refresh_token($refresh_token)
  92. {
  93. $api_url = self::Douyin_Url . '/webcast/game/oauth/renew_refresh_token/';
  94. $params = [
  95. 'app_id' => $this->app_id,
  96. 'refresh_token' => $refresh_token,
  97. ];
  98. return $this->https_post($api_url, $params);
  99. }
  100. /**
  101. * 3.4.7 游戏内授权后回调抖音游戏
  102. *
  103. * @param $client_token
  104. * @param $is_agree 是否同意授权: 0=拒绝,1=同意
  105. * @param $uid 游戏账号ID
  106. * @param $scheme 抖音跳转游戏scheme的参数,回调时需要透传带上
  107. *
  108. * @return mixed
  109. */
  110. public function jump_game_auth_callback($client_token, $is_agree, $uid, $scheme){
  111. $api_url = self::Douyin_Url . '/api/webcast/v1/game/doulink/jump_game_auth_callback/';
  112. $headers = [
  113. 'access-token' => $client_token,
  114. ];
  115. $params = [
  116. 'is_agree' => $is_agree,
  117. 'game_user_id' => $uid,
  118. 'extra_json' => $scheme,
  119. ];
  120. return $this->https_post($api_url, $params, $headers);
  121. }
  122. }