Coder Social home page Coder Social logo

Comments (8)

themattharris avatar themattharris commented on July 29, 2024

no updates should be necessary. have you followed this example?https://github.com/themattharris/tmhOAuthExamples/blob/master/cli/photo_tweet.php

if that doesn't work let me know the error you are getting back.

from tmhoauth.

sonalmahajan01 avatar sonalmahajan01 commented on July 29, 2024

Thank you so much for forwarding me the link. Do you have for the chunked video upload also?

Thanks and Regards,
Sonal

from tmhoauth.

sonalmahajan01 avatar sonalmahajan01 commented on July 29, 2024

I tried the given example but it gave an error that " media parameter is empty". After debugging, realized the post fields parametr was going empty. So had to modify the tmhOauth.php for the following code:

// setup params for GET/POST method handling if (!empty($prepared_pairs) || !empty($prepared)) { switch ($this->request_settings['method']) { case 'POST': $this->request_settings['postfields'] = $this->request_settings['multipart'] ? $prepared : implode('&', $prepared_pairs); break; default: $this->request_settings['querystring'] = implode('&', $prepared_pairs); break; } }

The uploading the file part works after this modification. But now while creating the tweet i am getting this error: :"The Twitter REST API v1 is no longer active. Please migrate to API v1.1. https://dev.twitter.com/docs/api/1.1/overview.\n".... though i am using 1.1 in my call:

$code = $tmhOAuth->request('POST', 'https://upload.twitter.com/1.1/statuses/update', $twitterAPICallParams, false, true);

from tmhoauth.

themattharris avatar themattharris commented on July 29, 2024

please share your actual code (without the oauth keys)

from tmhoauth.

sonalmahajan01 avatar sonalmahajan01 commented on July 29, 2024

Hi,
I managed to debug it. I was doing 2 things wrongly:

  1. Was using wrong url: "https://upload.twitter.com/1.1/statuses/update". Have to use "https://api.twitter.com/1.1/statuses/update.json".
  2. After every request have to initialize the $tmhOAuth variable with diff variable names.
    And it worked like a charm.

` $tmhOAuth = new \tmhOAuth(array(
'consumer_key' => $twitter_consumer_key,
'consumer_secret' => $twitter_consumer_key_secret,
'user_token' => $access_token,
'user_secret' => $access_token_secret
));
$fileName = fileManager::getInstance()->saveFileToTempFolder($imageSource);
$media = new \CURLFile("{$fileName}");
$code = $tmhOAuth->request ('POST', 'https://upload.twitter.com/1.1/media/upload.json', array('media' => $media) , true, true);
if ($code."" == "200") {
$tmhOAuth1 = new \tmhOAuth(array(
'consumer_key' => $twitter_consumer_key,
'consumer_secret' => $twitter_consumer_key_secret,
'user_token' => $access_token,
'user_secret' => $access_token_secret
));
$media_id = array($response->media_id_string);
$twitterAPICallParams = array(
'media_ids' => $media_id,
'status' => $message,
'in_reply_to_status_id' => $in_reply_to_status_id
);
$code = $tmhOAuth1->request('POST', 'https://api.twitter.com/1.1/statuses/update.json', $twitterAPICallParams);

           }

`

Thank you so much for all the prompt replies. Do you have any example for the chunked upload for videos? That would be great help.

from tmhoauth.

themattharris avatar themattharris commented on July 29, 2024

great! congrats on finding the problem.
i don't have a chunked example written up, i'll try and find time tomorrow to create one though.

you shouldn't need to create new tmhOAuth instances though. calls to request (or better user_request in your case) call reset_request_parameters which empties any param arrays (and a few other things).

from tmhoauth.

sonalmahajan01 avatar sonalmahajan01 commented on July 29, 2024

Yeah even i thought the same but if i didn't reinitialize the tmhOAuth, it gave me Bad request. and checking further realized that the auth information was not being sent in second request.

from tmhoauth.

sonalmahajan01 avatar sonalmahajan01 commented on July 29, 2024

Hi,
I have implemented the video chunked upload, in case it would be helpful for others if you include it in your examples.

`
// If there is an attachment, store to temp folder
$media_path = fileManager::getInstance()->saveFileToTempFolder($videoSource);
$videoFileSize = (int)filesize($media_path);

//Instanciate $tmhOAuth and do the 3 steps for uploding videos:
$tmhOAuthUpload = new \tmhOAuth(array(
'consumer_key' => $twitter_consumer_key,
'consumer_secret' => $twitter_consumer_key_secret,
'user_token' => $access_token,
'user_secret' => $access_token_secret
));

$code = $tmhOAuthUpload->user_request(array(
'method' => 'POST',
'url' => 'https://upload.twitter.com/1.1/media/upload.json',
'params' => array(
'command' => 'INIT',
'media_type' => 'video/mp4',
'total_bytes' => $videoFileSize
)
));

$responseStr = $tmhOAuthUpload->response['response'];
$code = substr($code, 0, 1); //the response is ok if it starts with 2, all others means failure

if ($code != '2') {
return new ApiResponse(ApiResponse::ERROR, VSocialErrorHandling::ERROR_RESPONSE_FROM_TWITTER, "Response :".$responseStr);
}

//Retrieve media id returned by Twitter
$media_id = json_decode($responseStr)->media_id_string;
Logger::info('Media Id : '.$media_id);
//Handle video/media upload with the Append loop
$fp = fopen($media_path, 'r');
$segment_id = 0;
while (! feof($fp)) {
$chunk = fread($fp, 1048576); // 1MB per chunk for this sample
$code = $tmhOAuthUpload->request('POST',
'https://upload.twitter.com/1.1/media/upload.json',
array(
"command" => "APPEND",
"media_id" => $media_id,
'media_data' => base64_encode($chunk),
"segment_index" => $segment_id
)
);

$responseStr = $tmhOAuthUpload->response['response'];
$code = substr($code, 0, 1);
if ($code != '2') {
	return new ApiResponse(ApiResponse::ERROR, VSocialErrorHandling::ERROR_RESPONSE_FROM_TWITTER, "Response :".$responseStr);
}
			
$segment_id++;

}

//FINALIZE
$code = $tmhOAuthUpload->request( 'POST', 'https://upload.twitter.com/1.1/media/upload.json',
array(
"command" => "FINALIZE",
"media_id" => $media_id,
)
);
$responseStr = $tmhOAuthUpload->response['response'];
if ($code != '201') { //status for created
return new ApiResponse(ApiResponse::ERROR, VSocialErrorHandling::ERROR_RESPONSE_FROM_TWITTER, "Response :".$responseStr);
}

//After having uploaded the video, we have to check the status of the Video to see if the video has been //processed only then can we attach it to the tweet
$videoCount = 0;
$statusCode = 0;
do {
$statusCode = $tmhOAuthUpload->request('GET', 'https://upload.twitter.com/1.1/media/upload.json', array("command" => "STATUS", "media_id" => $media_id));

if ($statusCode != '200'){ 
	sleep(2); 
}
$videoCount++;

} while ($statusCode != '200' && $videoCount < 3);

if ($statusCode != '200' && $videoCount == 3) { //the attenpts were not successfull
return new ApiResponse(ApiResponse::ERROR, VSocialErrorHandling::ERROR_RESPONSE_FROM_TWITTER, "Response : There was some issue while loading the video. Please try again.");
}

//finally we attach the video to the tweet
$tmhOAuth = new \tmhOAuth(array(
'consumer_key' => $twitter_consumer_key,
'consumer_secret' => $twitter_consumer_key_secret,
'user_token' => $access_token,
'user_secret' => $access_token_secret
));

$code = $tmhOAuth->request('POST', $tmhOAuth->url('1.1/statuses/update'), array('media_ids' => $media_id, 'status' => $text), true);
$responseStr = $tmhOAuth->response['response'];

if ($code != '200') { //status for created
return new ApiResponse(ApiResponse::ERROR, VSocialErrorHandling::ERROR_RESPONSE_FROM_TWITTER, "Response :".$responseStr);
}

// Set the user
if (isset($response->source->user)) {
$userResponse = $response->source->user;
}`

Appreciate all the quick response. I am closing the issue.

Thanks and Regards,
Sonal

from tmhoauth.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.