configData = $configData; $this->consumerKey = env('TWITTER_CONSUMER_KEY'); $this->consumerSecret = env('TWITTER_CONSUMER_SECRET'); $this->oauthCallbackUrl = env('DIST_SITE_URL') . '/dist/callback/twitter'; if (!empty($configData['access_token']) && !empty($configData['access_token_secret'])) { $this->twitterOAuth = new TwitterOAuth( $this->consumerKey, $this->consumerSecret, $configData['access_token'], $configData['access_token_secret'] ); } } public function login() { $connection = new TwitterOAuth($this->consumerKey, $this->consumerSecret); $requestToken = $connection->oauth('oauth/request_token', ['oauth_callback' => $this->oauthCallbackUrl]); session(['twitter_oauth_token' => $requestToken['oauth_token']]); session(['twitter_oauth_token_secret' => $requestToken['oauth_token_secret']]); $url = $connection->url('oauth/authorize', ['oauth_token' => $requestToken['oauth_token']]); return [ 'status' => true, 'data' => ['url' => $url] ]; } public function loginCallback(Request $request) { $oauthToken = $request->get('oauth_token'); $oauthVerifier = $request->get('oauth_verifier'); $requestToken = session('twitter_oauth_token'); $requestTokenSecret = session('twitter_oauth_token_secret'); $connection = new TwitterOAuth( $this->consumerKey, $this->consumerSecret, $requestToken, $requestTokenSecret ); $accessToken = $connection->oauth('oauth/access_token', [ 'oauth_verifier' => $oauthVerifier ]); return [ 'status' => true, 'data' => [ 'accessToken' => $accessToken['oauth_token'], 'accessTokenSecret' => $accessToken['oauth_token_secret'], 'userId' => $accessToken['user_id'], 'userName' => $accessToken['screen_name'], ] ]; } public function postImage($message, $imagePaths, $accessToken = null) { try { $mediaIds = []; foreach ($imagePaths as $imagePath) { if (!file_exists($imagePath)) { throw new \Exception("图片不存在: {$imagePath}"); } $uploadedMedia = $this->twitterOAuth->upload('media/upload', [ 'media' => $imagePath ]); if (isset($uploadedMedia->error)) { throw new \Exception('媒体上传失败: ' . json_encode($uploadedMedia)); } $mediaIds[] = $uploadedMedia->media_id_string; } $tweet = $this->twitterOAuth->post('statuses/update', [ 'status' => $message, 'media_ids' => implode(',', $mediaIds) ]); if (isset($tweet->errors)) { throw new \Exception('推文发布失败: ' . json_encode($tweet->errors)); } return [ 'status' => true, 'data' => [ 'tweetId' => $tweet->id_str, 'mediaIds' => $mediaIds ] ]; } catch (\Exception $e) { return ['status' => false, 'data' => $e->getMessage()]; } } public function postVideo($message, $videoPath, $accessToken = null) { try { if (!file_exists($videoPath)) { throw new \Exception("视频文件不存在: {$videoPath}"); } $uploadedVideo = $this->twitterOAuth->upload('media/upload', [ 'media' => $videoPath, 'media_type' => 'video/mp4' ], true); // 注意: chunked upload if (isset($uploadedVideo->error)) { throw new \Exception('视频上传失败: ' . json_encode($uploadedVideo)); } $tweet = $this->twitterOAuth->post('statuses/update', [ 'status' => $message, 'media_ids' => $uploadedVideo->media_id_string ]); if (isset($tweet->errors)) { throw new \Exception('推文发布失败: ' . json_encode($tweet->errors)); } return [ 'status' => true, 'data' => [ 'tweetId' => $tweet->id_str, 'mediaId' => $uploadedVideo->media_id_string ] ]; } catch (\Exception $e) { return ['status' => false, 'data' => $e->getMessage()]; } } // 保持空实现 public function getComments($postId) {} public function replyToComment($commentId) {} public function deleteComment($commentId) {} }