Text gets auto Tweeted while Media (image) does not - php

I am using this ridiculously simple script to auto Tweet on behalf of a user.
$image = 'test.jpg';
// Insert your keys/tokens
$consumerKey = 'xxx';
$consumerSecret = 'xxx';
$OAuthToken = 'xxx';
$OAuthSecret = 'xxx';
// Full path to twitterOAuth.php (change OAuth to your own path)
require_once('connect/twitter/twitteroauth.php');
$params = array(
'status' => $_REQUEST['content'],
'media[]' => '#{$image}'
);
// create new instance
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $OAuthToken, $OAuthSecret);
// Send tweet
$tweet->post('statuses/update', $params);
Upon executing this code, text gets Tweeted on my twitter account however the image does not. I have been previously advised to only attempt tweeting local files and I was advised this is the proper way to specify the media[].
Why isnt this posting the image while its posting the text to twitter?

You need to call statuses/update_with_media - not statuses/update (which you are currently using).
For more information, please read the documentation.

I guess you are using the Abraham Library. It won't work for Tweet with Media. I was in the position last month & this library helped me. Just Download & Copy the twitteroauth folder from the extracted folder & paste it in your project folder by renaming it into twitteroauth1. Why because, there might a folder twitteroauth or files twitteroauth.php & 'oAuth.php` already present in the Abraham Library.
Now, you just want to edit the include or require_once as
require_once('twitteroauth1/twitteroauth.php');
The new twitteroauth.php will help to tweet with media. Also, in your code, it's not
$tweet->post('statuses/update', $params);
Instead, it should be,
$tweet->post('statuses/update_with_media', $params);

Related

Video uploaded with Youtube API still marked as private after project was approved

I've been struggling with this problem for a while.
I'm trying to upload videos to Youtube using the Youtube V3 API. I've added the correct scopes to the OAuth Consent screen and the project was approved recently (went through the whole audit thingie), but the videos are still marked as private when they are uploaded.
I've tested with different video files on different accounts.
I've created new API keys, even restricted the keys and still no success.
I'm using the latest version of the Google APIs Client Library for PHP.
Any help would be appreciated. I don't even know how to contact Google for support on this one.
Edit: This is the code snippet that handles video upload
//Code that retrieves the api auth data is not included
//Also not included is the code that prepares the video path, title, description and category
$google_client = new \Google_Client();
// Offline because we need to get refresh token
$google_client->setAccessType('offline');
// Name of the google application
$google_client->setApplicationName($appName);
// Set the client_id
$google_client->setClientId($clientId);
// Set the client_secret
$google_client->setClientSecret($clientSecret);
// Redirects to same url
$google_client->setRedirectUri($scriptUri);
// Set the api key
$google_client->setDeveloperKey($apiKey);
// Load required scopes
$google_client->setScopes(array(
'https://www.googleapis.com/auth/youtube.upload https://www.googleapis.com/auth/youtube https://www.googleapis.com/auth/userinfo.profile'
));
// Refresh token
$google_client->refreshToken($refresh_token);
// Get access token
$new_access_token = $google_client->getAccessToken();
// Set access token
$google_client->setAccessToken($new_access_token);
// Define service object for making API requests.
$youtube_service = new \Google_Service_YouTube($google_client);
// Define the $youtube_video object, which will be uploaded as the request body.
$youtube_video = new \Google_Service_YouTube_Video();
// Add 'snippet' object to the $youtube_video object.
$video_snippet = new \Google_Service_YouTube_VideoSnippet();
// Add category, title, description
$video_snippet->setCategoryId($video_category);
$video_snippet->setTitle($video_title);
$video_snippet->setDescription($video_description);
$youtube_video->setSnippet($video_snippet);
// Add 'status' object to the $youtube_video object.
$video_status = new \Google_Service_YouTube_VideoStatus();
$video_status->setPrivacyStatus('public');
$youtube_video->setStatus($video_status);
//Get MIME
$file_info = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_file($file_info, $video_path);
$upload = $youtube_service->videos->insert('snippet,status', $youtube_video, array(
'data' => file_get_contents($video_path),
'mimeType' => $mime_type,
'uploadType' => 'multipart'
));

Vimeo API 2016 how to display list of users videos

I'm having trouble finding a simplified tutorial for using the Vimeo API I know I need to include vimeo.php and the following
include 'vimeo.php';
$vimeo = new phpVimeo('Client Identifier', 'Client Secrets');
$videos = $vimeo->call('vimeo.videos.getUploaded', array('user_id' => "user1877648"));
print_r($videos);
I've copied and pasted the fields I've used from the access Authentication in case that's where the issue is, I've also read that for simple calls to the API don't need access tokens?
I could really do with some pointers as to how I get a list of vimeo thumbs linking to the vimeo url from a specific user? I was using the older code and up until recently it worked well.
Dashron pointed you to all of the correct places to find the documentation needed to do what you are trying to do.
However, here is an example of how you would do it.
You need to download / clone the Vimeo PHP Library (found here: https://github.com/vimeo/vimeo.php).
Then go to Vimeo and create an app so you can acquire a client id and client secret (https://developer.vimeo.com/api/start).
Now that you have a client id, client secret, and the vimeo library, you can create a simple script to load all of the videos from a specific user. Here is an example:
<?php
// include the autoload file from the vimeo php library that was downloaded
include __DIR__ . '/vimeo/autoload.php';
// The client id and client secret needed to use the vimeo API
$clientId = "";
$clientSecret = "";
// when getting an auth token we need to provide the scope
// all possible scopes can be found here https://developer.vimeo.com/api/authentication#supported-scopes
$scope = "public";
// The id of the user
$userId = "alexbohs";
// initialize the vimeo library
$lib = new \Vimeo\Vimeo($clientId, $clientSecret);
// request an auth token (needed for all requests to the Vimeo API)
$token = $lib->clientCredentials($scope);
// set the token
$lib->setToken($token['body']['access_token']);
// request all of a user's videos, 50 per page
// a complete list of all endpoints can be found here https://developer.vimeo.com/api/endpoints
$videos = $lib->request("/users/$userId/videos", ['per_page' => 50]);
// loop through each video from the user
foreach($videos['body']['data'] as $video) {
// get the link to the video
$link = $video['link'];
// get the largest picture "thumb"
$pictures = $video['pictures']['sizes'];
$largestPicture = $pictures[count($pictures) - 1]['link'];
}
Keep in mind that the vimeo API returns "pages" of videos. So if the user has more than 50 videos per page you would need to do a request for each page by specifying the page number using "page" parameter (Change ['per_page' => 50] to ['per_page' => 50, 'page' => #].
This is the old, Advanced API. It is deprecated.
The new PHP library is here: https://github.com/vimeo/vimeo.php
The new API docs are here: https://developer.vimeo.com/api
The endpoint to retrieve all of your videos is https://api.vimeo.com/me/videos (https://developer.vimeo.com/api/endpoints/me#/videos)

How to get user information (like screen_name, description, etc) from id_str using Twitter API?

Suppose I have this text "retweet of tomgabi https://..." in a quoted_status, collected using https://api.twitter.com/1.1/statuses/user_timeline.json. When I open this link in a browser I can see a lot of information about the user (tweet, screen_name, description, etc). The URL change to https://twitter.com/wiltonpfilho/status/715320170655440896. How can I get, using the Twitter API, that user information (tweet, screen_name, description, etc) from this new URL. Can I access user information from id_str (in URL)?
Check this Link
So you REALLY don't want to do this client side anymore. (Just went through numerous docs, and devs suggest to do all oAuth server-side)
What you need to do:
First: sign up on https://dev.twitter.com, and make a new application.
Second: NOTE: Your Consumer Key / Secret along with Access Token / Secret
Third: Download Twitter oAuth Library (In this case I used the PHP Library https://github.com/abraham/twitteroauth , additional library located here: https://dev.twitter.com/docs/twitter-libraries)
Fourth: (If using php) Make sure cURL is enabled, if your running on a LAMP here's the command you need:
sudo apt-get install php5-curl
Fifth: Make a new PHP file and insert the following: Thanks to Tom Elliot http://www.webdevdoor.com/php/authenticating-twitter-feed-timeline-oauth/
<?php
session_start();
require_once("twitteroauth/twitteroauth/twitteroauth.php"); //Path to twitteroauth library you downloaded in step 3
$twitteruser = "twitterusername"; //user name you want to reference
$notweets = 30; //how many tweets you want to retrieve
$consumerkey = "12345"; //Noted keys from step 2
$consumersecret = "123456789"; //Noted keys from step 2
$accesstoken = "123456789"; //Noted keys from step 2
$accesstokensecret = "12345"; //Noted keys from step 2
function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
return $connection;
}
$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);
echo json_encode($tweets);
echo $tweets; //testing remove for production
?>
And boom, you're done. I know this isn't a pure js solution but again reading through the new Twitter API 1.1 docs they REALLY don't want you to do this client site. Hope this helps!
Use statuses/lookup.
For example, https://api.twitter.com/1.1/statuses/lookup.json?id=715320170655440896

Uploading video to YouTube using Dropbox public link?

I am using youtube ZEND gdata api to upload videos to My youtube account.But now I need to upload videos to YouTube which are stored in my DropBox account.I have public links or direct links for video files.Code I am using is:
<?php
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_YouTube');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_App_Exception');
$developerKey = '******************';
$applicationId = '*********';
$clientId = '';
$video_title = 'test movie';
$video_description = 'test movie';
$video_category = 'Entertainment';
$video_tags = 'test,movie';
$path_of_uploaded_file = 'http://dl.dropbox.com/uhh/336/test.wmv';
$authenticationURL= 'https://www.google.com/youtube/accounts/ClientLogin';
$httpClient = Zend_Gdata_ClientLogin::getHttpClient(
$username = '*****',
$password = '*****',
$service = 'youtube',
$client = null,
$source = '*******', // a short string identifying your application
$loginToken = null,
$loginCaptcha = null,
$authenticationURL);
$yt = new Zend_Gdata_YouTube($httpClient, $applicationId, $clientId, $developerKey);
$yt = new Zend_Gdata_YouTube($httpClient);
// create a new VideoEntry object
$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
// create a new Zend_Gdata_App_MediaFileSource object
$filesource = $yt->newMediaFileSource($path_of_uploaded_file);
..
.
.
.
.
.?>
Error I am getting is:File to be uploaded at http://dl.dropbox.com/uhh/336/test.wmv does not exist or is not readable.
I don't why YouTube is throwing this error even though the URL is direct(Public) link. I am not getting what's wrong with my code and why it is not working.Please help :)
Just in case other are still looking for answers, I had a similar problem reading txt files with an app I wrote from Dropbox. I discovered that I wasn't using the right direct-link format. The correct link (for this threads example link) would be:
dl.dropboxusercontent.com/uhh/336/test.wmv
Just changing 'www' to 'dl' will work for web-browser as they handle redirects automatically. For apps and scripts you would have to integrate handling of redirects. Or just format the direct-link correctly.
It's impossible to answer this question definitively given the information provided, but we can narrow it down to a few possibilities. I would recommend you run down this quick debugging checklist:
If you copy/paste that exact link into your web browser, using the same credentials, are you able to download the video?
If not, then it's either a problem with the credentials or the URL itself.
If you are able to download it from the browser, then it means there's something wrong with your code or an issue with YouTube.
If you run through the above steps but you're still at an impasse, please post your results as a comment and we'll delve deeper.
The file you're uploading to YouTube must be on a local path.
Download the file locally using cURL
Set newMediaFileSource to the local file's path
In dropbox "get link" gives "link of dropbox page".
Try this.
Open up video page on dropbox.
right click on "download" and then "copy link location"
Hope this helps

Twitter OAuth (PHP): Need good, basic example to get started

Using Facebook's PHP SDK, I was able to get Facebook login working pretty quickly on my website. They simply set a $user variable that can be accessed very easily.
I've had no such luck trying to get Twitter's OAuth login working... quite frankly, their github material is confusing and useless for someone that's relatively new to PHP and web design, not to mention that many of the unofficial examples I've tried working through are just as confusing or are outdated.
I really need some help getting Twitter login working--I mean just a basic example where I click the login button, I authorize my app, and it redirects to a page where it displays the name of the logged in user.
I really appreciate your help.
EDIT I'm aware of the existence of abraham's twitter oauth but it provides close to no instructions whatsoever to get his stuff working.
this one is the basic example of getting the url for authorization and then fetching the user basic info when once u get back from twitter
<?php
session_start();
//add autoload note:do check your file paths in autoload.php
require "ret/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
//this code will run when returned from twiter after authentication
if(isset($_SESSION['oauth_token'])){
$oauth_token=$_SESSION['oauth_token'];unset($_SESSION['oauth_token']);
$consumer_key = 'your consumer key';
$consumer_secret = 'your secret key';
$connection = new TwitterOAuth($consumer_key, $consumer_secret);
//necessary to get access token other wise u will not have permision to get user info
$params=array("oauth_verifier" => $_GET['oauth_verifier'],"oauth_token"=>$_GET['oauth_token']);
$access_token = $connection->oauth("oauth/access_token", $params);
//now again create new instance using updated return oauth_token and oauth_token_secret because old one expired if u dont u this u will also get token expired error
$connection = new TwitterOAuth($consumer_key, $consumer_secret,
$access_token['oauth_token'],$access_token['oauth_token_secret']);
$content = $connection->get("account/verify_credentials");
print_r($content);
}
else{
// main startup code
$consumer_key = 'your consumer key';
$consumer_secret = 'your secret key';
//this code will return your valid url which u can use in iframe src to popup or can directly view the page as its happening in this example
$connection = new TwitterOAuth($consumer_key, $consumer_secret);
$temporary_credentials = $connection->oauth('oauth/request_token', array("oauth_callback" =>'http://dev.crm.alifca.com/twitter/index.php'));
$_SESSION['oauth_token']=$temporary_credentials['oauth_token']; $_SESSION['oauth_token_secret']=$temporary_credentials['oauth_token_secret'];$url = $connection->url("oauth/authorize", array("oauth_token" => $temporary_credentials['oauth_token']));
// REDIRECTING TO THE URL
header('Location: ' . $url);
}
?>
I just tried abraham's twitteroauth from github and it seems to work fine for me. This is what I did
git clone https://github.com/abraham/twitteroauth.git
Upload this into your webhost with domain, say, www.example.com
Go to Twitter Apps and register your application. The changes that you need are (assuming that you will use abraham's twitteroauth example hosted at http://www.example.com/twitteroauth)
a) Application Website will be http://www.example.com/twitteroauth
b) Application type will be browser
c) Callback url is http://www.example.com/twitteroauth/callback.php (Callback.php is included in the git source)
Once you do this, you will get the CONSUMER_KEY and CONSUMER_SECRET which you can update in the config.php from the twitteroauth distribution. Also set the callback to be the same as http://www.example.com/twitteroauth/callback.php
Thats it. If you now navigate to http://www.example.com/twitteroauth, you will get a "Signin with Twitter", that will take you to Twitter , authorize the request and get you back to the index.php page.
EDIT:
Example will not work but do not worry. Follow the above steps and upload to server.
Make sure you rename the file from github repository i.e. config-sample.php->config.php
if you want to see a working sample, find it here
Here are some OAuth 1.0A PHP libraries with examples:
tmhOAuth
Oauth-php
Twitter async
Twitter async provides documentation on how to simply sign in a user as you asked for.
Here is the step by step guide to integrate Twitter OAuth API to Web-application using PHP. Please following tutorial.
http://www.smarttutorials.net/sign-in-with-twitter-oauth-api-using-php/
You need to create Twitter App First By going thorugh following URL
https://apps.twitter.com/
Then you need to provide necessary information for the twitter app. Once your provided all the information and then save it. You will get Twitter application Consumer Key and Consumer secret.
Please download the source file from above link, and just replace TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET and TWITTER_OAUTH_CALLBACK with your Consumer Key (API Key), Consumer Secret (API Secret) and callback URL. Then upload this to your server. Now it will work successfully.
Abraham's Twitteroauth has a working demo here:
https://github.com/abraham/twitteroauth-demo
Following the steps in the demo readme worked for me. In order to run composer on macOS I had to do this after installing it: mv composer.phar /usr/local/bin/composer
IMO the demo could be a lot simpler and should be included in the main twitteroauth repo.
I recently had to post new tweets to Twitter via PHP using V2 of their API but couldn’t find any decent examples online that didn’t use V1 or V1.1. I eventually figured it out using the great package TwitterOAuth.
Install this package via composer require abraham/twitteroauth first (or manually) and visit developer.twitter.com, create a new app to get the credentials needed to use the API (see below). Then you can post a tweet based on the code below.
use Abraham\TwitterOAuth\TwitterOAuth;
// Connect
$connection = new TwitterOAuth($twitterConsumerKey, // Your API key
$twitterConsumerSecret, // Your API secret key
$twitterOauthAccessToken, // From your app created at https://developer.twitter.com/
$twitterOauthAccessTokenSecret); // From your app created at https://developer.twitter.com/
// Set API version to 2
$connection->setApiVersion('2');
// POST the tweet; the third parameter must be set to true so it is sent as JSON
// See https://developer.twitter.com/en/docs/twitter-api/tweets/manage-tweets/api-reference/post-tweets for all options
$response = $connection->post('tweets', ['text' => 'Hello Twitter'], true);
if (isset($response['title']) && $response['title'] == 'Unauthorized') {
// Handle error
} else {
var_dump($response);
/*
object(stdClass)#404 (1) {
["data"]=>
object(stdClass)#397 (2) {
["id"]=>
string(19) "0123456789012345678"
["text"]=>
string(13) "Hello Twitter"
}
}
*/
}

Categories