Я пытаюсь загрузить видео в Твиттер в laravel 11, видео имеет размер mp4, 3 МБ и длительность 5 секунд, но с этим видео и со всеми другими видео, а не только с этим, появляется эта ошибка:
Примечание: мои учетные данные приложения и токены пользователя верны
use Abraham\TwitterOAuth\TwitterOAuth;
class TwitterController extends Controller
{
public function uploadVideo(Request $request)
{
// Increase the maximum execution time to 300 seconds (5 minutes)
set_time_limit(300);
// Set your access token details
$userfirst = User::find(Auth()->user()->id);
if ($userfirst->social_type != "twitter") {
$account_id = $userfirst->account_id;
$user = User::where('account_id', $account_id)
->where('social_type', 'twitter')
->where('role', 0)
->first();
if (!$user) {
return response()->json(["message" => "No Twitter account found"], 404);
}
} else {
$user = User::find(Auth()->user()->id);
}
$accessToken = $user->access_token;
$accessTokenSecret = $user->refresh_token;
$userId = $user->social_id;
// Initialize TwitterOAuth with your app credentials and the user's access token
$twitter = new TwitterOAuth(
config('services.twitter.client_id'),
config('services.twitter.client_secret'),
$accessToken,
$accessTokenSecret
);
// Increase the timeout settings
$twitter->setTimeouts(30, 300); // Set the connect timeout to 30 seconds and the request timeout to 300 seconds
// Tell the library we want to use the v1.1 API for media upload
$twitter->setApiVersion('1.1');
// Check if there's a video to upload
if ($request->hasFile('video')) {
$videoFile = $request->file('video');
$videoPath = $videoFile->getPathname();
$videoMimeType = $videoFile->getMimeType();
$videoExtension = $videoFile->getClientOriginalExtension();
// Get the filename without the .tmp extension
$pathInfo = pathinfo($videoPath);
$newVideoPath = $pathInfo['dirname'] . DIRECTORY_SEPARATOR . $pathInfo['filename'] . '.' . $videoExtension;
// Rename the file with the correct extension
rename($videoPath, $newVideoPath);
// Upload video
$media = $twitter->upload('media/upload', ['media' => $newVideoPath, 'media_category' => 'tweet_video']);
if (isset($media->media_id_string)) {
return response()->json([
"message" => "Video uploaded successfully",
"media_id" => $media->media_id_string
], 200);
} else {
return response()->json([
"message" => "Failed to upload video",
"error" => $media
], 500);
}
} else {
return response()->json(["message" => "No video file provided"], 400);
}
}
}
примечание: загрузка изображений работает нормально
Я пытаюсь загрузить видео в Твиттер в laravel 11, видео имеет размер mp4, 3 МБ и длительность 5 секунд, но с этим видео и со всеми другими видео, а не только с этим, появляется эта ошибка: Примечание: мои учетные данные приложения и токены пользователя верны [code]{ "message": "Failed to upload video", "error": { "request": "/1.1/media/upload.json", "error": "media type unrecognized." } } [/code] и это мой код: [code]use Abraham\TwitterOAuth\TwitterOAuth; class TwitterController extends Controller { public function uploadVideo(Request $request) { // Increase the maximum execution time to 300 seconds (5 minutes) set_time_limit(300);
// Set your access token details $userfirst = User::find(Auth()->user()->id); if ($userfirst->social_type != "twitter") { $account_id = $userfirst->account_id; $user = User::where('account_id', $account_id) ->where('social_type', 'twitter') ->where('role', 0) ->first(); if (!$user) { return response()->json(["message" => "No Twitter account found"], 404); } } else { $user = User::find(Auth()->user()->id); }
// Initialize TwitterOAuth with your app credentials and the user's access token $twitter = new TwitterOAuth( config('services.twitter.client_id'), config('services.twitter.client_secret'), $accessToken, $accessTokenSecret );
// Increase the timeout settings $twitter->setTimeouts(30, 300); // Set the connect timeout to 30 seconds and the request timeout to 300 seconds
// Tell the library we want to use the v1.1 API for media upload $twitter->setApiVersion('1.1');
// Check if there's a video to upload if ($request->hasFile('video')) { $videoFile = $request->file('video'); $videoPath = $videoFile->getPathname(); $videoMimeType = $videoFile->getMimeType(); $videoExtension = $videoFile->getClientOriginalExtension();
// Get the filename without the .tmp extension $pathInfo = pathinfo($videoPath); $newVideoPath = $pathInfo['dirname'] . DIRECTORY_SEPARATOR . $pathInfo['filename'] . '.' . $videoExtension;
// Rename the file with the correct extension rename($videoPath, $newVideoPath);