YoutubeService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. namespace App\Services\Smm;
  3. use App\Services\Contracts\SmmPlatformInterface;
  4. use Carbon\Carbon;
  5. use Google_Client;
  6. use Google_Service_YouTube;
  7. use Google_Service_YouTube_Video;
  8. use Google_Service_YouTube_VideoSnippet;
  9. use Google_Service_YouTube_VideoStatus;
  10. use Google_Http_MediaFileUpload;
  11. use Illuminate\Http\Request;
  12. use OSS\OssClient;
  13. use OSS\Core\OssException;
  14. use Illuminate\Support\Facades\Log;
  15. class YoutubeService implements SmmPlatformInterface
  16. {
  17. protected Google_Client $client;
  18. protected string $redirectUri;
  19. protected array $configData;
  20. public function __construct(array $configData)
  21. {
  22. $this->configData = $configData;
  23. $this->client = new Google_Client();
  24. $this->client->setClientId(env('SSM_YOUTUBE_CLIENT_ID'));
  25. $this->client->setClientSecret(env('SSM_YOUTUBE_CLIENT_SECRET'));
  26. $this->client->setRedirectUri(env('DIST_SITE_URL').'/dist/callback/youtube');
  27. $this->client->setScopes([
  28. Google_Service_YouTube::YOUTUBE_UPLOAD,
  29. // Google_Service_YouTube::YOUTUBE,
  30. // Google_Service_YouTube::YOUTUBE_FORCE_SSL,
  31. // Google_Service_YouTube::YOUTUBE_READONLY,
  32. // Google_Service_YouTube::YOUTUBEPARTNER,
  33. // Google_Service_YouTube::YOUTUBEPARTNER_CHANNEL_AUDIT
  34. ]);
  35. $this->client->setAccessType('offline');
  36. $this->client->setPrompt('consent');
  37. $this->client->setDeveloperKey(env('SSM_YOUTUBE_DEV_KEY'));
  38. }
  39. public function login()
  40. {
  41. try {
  42. return ['status' => true, 'data' => ['url' => $this->client->createAuthUrl()]];
  43. } catch (\Exception $e) {
  44. return ['status' => false, 'data' => 'Failed to generate login URL: '.$e->getMessage()];
  45. }
  46. }
  47. public function loginCallback(Request $request)
  48. {
  49. try {
  50. $code = $request->input('code');
  51. if (!$code) {
  52. return ['status' => false, 'data' => 'Authorization code not found'];
  53. }
  54. $token = $this->client->fetchAccessTokenWithAuthCode($code);
  55. if (isset($token['error'])) {
  56. return ['status' => false, 'data' => $token['error_description'] ?? 'Failed to exchange authorization code'];
  57. }
  58. $this->client->setAccessToken($token);
  59. $youtube = new Google_Service_YouTube($this->client);
  60. $channelResponse = $youtube->channels->listChannels('snippet', ['mine' => true]);
  61. return [
  62. 'status' => true,
  63. 'data' => [
  64. 'accessToken' => $token['access_token'],
  65. 'refreshToken' => $token['refresh_token'] ?? '',
  66. 'accessToken_expiresAt' => Carbon::now()->addSeconds($token['expires_in']),
  67. 'userName' => $channelResponse->items[0]->snippet->title ?? '',
  68. 'userId' => $channelResponse->items[0]->id ?? '',
  69. ]
  70. ];
  71. } catch (\Exception $e) {
  72. return ['status' => false, 'data' => $e->getMessage()];
  73. }
  74. }
  75. public function postImage($message, $imagePaths, $accessToken)
  76. {
  77. return [
  78. 'status' => false,
  79. 'data' => 'YouTube API does not support image-only posts'
  80. ];
  81. }
  82. public function postVideo($message, $videoPath, $accessToken)
  83. {
  84. Log::info('YouTube 开始发布视频帖子');
  85. /*
  86. $ossPath = ltrim(parse_url($videoPath, PHP_URL_PATH), '/');
  87. $fileSize = $this->getOssFileSize($ossPath);
  88. if ($fileSize['status'] === false) {
  89. return ['status' => false, 'data' => $fileSize['data']];
  90. }*/
  91. $backup_field1 = $this->configData['postData']['backup_field1'] ?? '';
  92. $backup_field1 = json_decode($backup_field1, true);
  93. $title = $backup_field1['yutube_title'] ?? '';
  94. $category = $backup_field1['yutube_category'] ?? '';
  95. $videoPath = toStoragePath($videoPath);
  96. // 处理 OAuth 回调
  97. $this->client->setAccessToken($accessToken);
  98. // 初始化 YouTube 服务
  99. $youtube = new Google_Service_YouTube($this->client);
  100. try {
  101. // 定义视频元数据
  102. $snippetData = [
  103. 'title' => $title,
  104. 'description' => $message,
  105. 'tags' => [],
  106. 'categoryId' => $category // 人物与博客
  107. ];
  108. Log::info('YouTube 定义视频元数据'.json_encode($snippetData));
  109. $snippet = new Google_Service_YouTube_VideoSnippet();
  110. $snippet->setTitle($snippetData['title']);
  111. $snippet->setDescription($snippetData['description']);
  112. $snippet->setTags($snippetData['tags']);//'test', 'api'
  113. $snippet->setCategoryId($snippetData['categoryId']); // 人物与博客
  114. //$snippet->setChannelId('目标频道ID'); // 不加代码默认发到默认频道(主频道)
  115. // 设置视频状态
  116. $status = new Google_Service_YouTube_VideoStatus();
  117. $status->setPrivacyStatus('public'); // public, private, unlisted
  118. $status->setMadeForKids(true);// 是否为儿童视频
  119. // 创建视频对象
  120. $video = new Google_Service_YouTube_Video();
  121. $video->setSnippet($snippet);
  122. $video->setStatus($status);
  123. // 上传视频
  124. $this->client->setDefer(true);
  125. $insertRequest = $youtube->videos->insert('snippet,status', $video);
  126. Log::info('YouTube 分块上传视频');
  127. $media = new Google_Http_MediaFileUpload(
  128. $this->client,
  129. $insertRequest,
  130. 'video/*',
  131. null,
  132. true,
  133. 2 * 1048576 // 分块大小 2MB
  134. );
  135. $media->setFileSize(filesize($videoPath)); // 替换为视频文件路径
  136. $status = false;
  137. $handle = fopen($videoPath, 'rb');
  138. while (!$status && !feof($handle)) {
  139. $chunk = fread($handle, 2 * 1048576);
  140. $status = $media->nextChunk($chunk);
  141. }
  142. fclose($handle);
  143. $this->client->setDefer(false);
  144. //echo "Video uploaded: " . $status['id'];
  145. Log::info('视频上传成功: '.$status['id']);
  146. return ['status' => true,
  147. 'data' => [
  148. 'responseIds' => [$status['id']],
  149. ]];
  150. } catch (Google_Service_Exception $e) {
  151. return ['status' => false, 'data' => $e->getMessage()];
  152. } catch (Google_Exception $e) {
  153. return ['status' => false, 'data' => $e->getMessage()];
  154. }
  155. }
  156. public function getVideoCategories($regionCode = 'US')
  157. {
  158. try {
  159. $youtube = new Google_Service_YouTube($this->client);
  160. $response = $youtube->videoCategories->listVideoCategories('snippet', [
  161. 'regionCode' => $regionCode
  162. ]);
  163. $categories = [];
  164. foreach ($response->items as $category) {
  165. if ($category->snippet->assignable) {
  166. $categories[$category->id] = $category->snippet->title;
  167. }
  168. }
  169. return [
  170. 'status' => true,
  171. 'data' => $categories
  172. ];
  173. } catch (\Google_Service_Exception $e) {
  174. $error = json_decode($e->getMessage(), true)['error']['message'] ?? $e->getMessage();
  175. return ['status' => false, 'data' => $error];
  176. }
  177. }
  178. public function getComments($postId)
  179. {
  180. try {
  181. $youtube = new Google_Service_YouTube($this->client);
  182. $response = $youtube->commentThreads->listCommentThreads('snippet', [
  183. 'videoId' => $postId,
  184. 'maxResults' => 100
  185. ]);
  186. $comments = [];
  187. foreach ($response->items as $item) {
  188. $comment = $item->snippet->topLevelComment->snippet;
  189. $comments[] = [
  190. 'id' => $item->id,
  191. 'text' => $comment->textDisplay,
  192. 'author' => $comment->authorDisplayName,
  193. 'createdAt' => $comment->publishedAt
  194. ];
  195. }
  196. return ['status' => true, 'data' => $comments];
  197. } catch (\Exception $e) {
  198. Log::error('YouTube get comments error: '.$e->getMessage());
  199. return ['status' => false, 'error' => $e->getMessage()];
  200. }
  201. }
  202. public function replyToComment($commentId)
  203. {
  204. $text = '';
  205. try {
  206. $youtube = new Google_Service_YouTube($this->client);
  207. $commentSnippet = new \Google_Service_YouTube_CommentSnippet();
  208. $commentSnippet->setTextOriginal($text);
  209. $commentSnippet->setParentId($commentId);
  210. $comment = new \Google_Service_YouTube_Comment();
  211. $comment->setSnippet($commentSnippet);
  212. $response = $youtube->comments->insert('snippet', $comment);
  213. return [
  214. 'status' => true,
  215. 'data' => [
  216. 'commentId' => $response->getId(),
  217. 'text' => $text
  218. ]
  219. ];
  220. } catch (\Exception $e) {
  221. Log::error('YouTube reply error: '.$e->getMessage());
  222. return ['status' => false, 'error' => $e->getMessage()];
  223. }
  224. }
  225. public function deleteComment($commentId)
  226. {
  227. try {
  228. $youtube = new Google_Service_YouTube($this->client);
  229. $youtube->comments->delete($commentId);
  230. return ['status' => true];
  231. } catch (\Exception $e) {
  232. Log::error('YouTube delete comment error: '.$e->getMessage());
  233. return ['status' => false, 'error' => $e->getMessage()];
  234. }
  235. }
  236. public function refreshToken($refreshToken)
  237. {
  238. try {
  239. $this->client->refreshToken($refreshToken);
  240. return $this->client->getAccessToken();
  241. } catch (\Exception $e) {
  242. Log::error('YouTube refresh token error: '.$e->getMessage());
  243. return ['error' => $e->getMessage()];
  244. }
  245. }
  246. public function refreshAccessToken($refreshToken)
  247. {
  248. try {
  249. $newToken = $this->client->fetchAccessTokenWithRefreshToken($refreshToken);
  250. return [
  251. 'status' => true,
  252. 'data' => [
  253. 'access_token' => $newToken['access_token'],
  254. 'expires_at' => Carbon::now()->addSeconds($newToken['expires_in']),
  255. 'refresh_token' => $newToken['refresh_token'],
  256. ]
  257. ];
  258. } catch (\Exception $e) {
  259. return ['status' => false, 'error' => 'Failed to refresh access token: '.$e->getMessage()];
  260. }
  261. }
  262. /*
  263. * 返回oss文件大小 (字节)
  264. * $pathName : 'path/to/your/file.txt'
  265. */
  266. /*
  267. public function getOssFileSize($pathName)
  268. {
  269. // 配置信息
  270. $accessKeyId = env('OSS_ACCESS_KEY_ID');
  271. $accessKeySecret = env('OSS_ACCESS_KEY_SECRET');
  272. $endpoint = env('OSS_ENDPOINT'); // 替换为你的 Region Endpoint
  273. $bucketName = env('OSS_BUCKET');
  274. $objectName = $pathName; // OSS 文件路径
  275. try {
  276. // 创建 OssClient 实例
  277. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  278. // 获取文件元数据
  279. $metadata = $ossClient->headObject($bucketName, $objectName);
  280. // 从元数据中获取文件大小(字节)
  281. $fileSize = $metadata['content-length'];
  282. return ['status' => true, 'data' => $fileSize];
  283. } catch (OssException $e) {
  284. // 错误处理
  285. return ['status' => false, 'data' => $e->getMessage()];
  286. }
  287. }
  288. */
  289. }