Twitter Search API 1.1 returning 401 - php

I'm trying to do something very simple, find a Twitter share count for a given url. But with this new API, it has been rather difficult.
I'm writing this in PHP and I'm using the abraham/twitteroauth API.
At first I was using a url like this...
$tweets = $twitter->get("https://api.twitter.com/1.1/search/tweets.json");
as is show in the documentation here https://dev.twitter.com/rest/reference/get/search/tweets
However that continually resulted in 404's. So I changed the structure of my code according to this post Twitter API returning Error: 34 (404)
But now all I'm getting is 401's. That's Twitters error code 32, "Could not authenticate you."
And yes I've checked and double checked all my tokens and secrets. I've also tried things like adding a value to the callback url field.
Below is my code...
case 'twitter':
$consumerKey = '###';
$consumerSecret = '###';
$oauthToken = '###';
$oauthTokenSecret = '###';
$twitter = new TwitterOAuth($consumerKey, $consumerSecret, $oauthToken, $oauthTokenSecret);
$tweets = $twitter->get('search/tweets', ['q' => 'post_url_goes_here']);
$count = "I'll figure out how to sort this data later";
$response[$service] = $count ? $count : 0;
break;
Anyone else run into this problem?
Thanks!

Related

Google admin SDK custom scheme issue

I need help on custom scheme on Google.
I successfully get the user list from delegation.
Below is my code.
$client = new \Google_Client();
$client->setApplicationName('xx');
$scopes = array('www.googleapis.com/auth/admin.directory.user','www.googleapis.com/auth/admin.directory.userschema');
$client->setAuthConfig('C:\Users\xx\xx\public\client_secret.json');
$client->setScopes($scopes);
$user_to_impersonate = 'xx.sg';
$client->setSubject($user_to_impersonate);
$dir = new \Google_Service_Directory($client);
$r = $dir->users->get('xxx#xx.com');
dd($r);
Userscheme have been added to Google also.
But the custom scheme I got is empty.
https://i.stack.imgur.com/0JcfZ.png
If I use projection = full in developers.google.com/admin it works.
https://i.stack.imgur.com/9s3MQ.png
Can someone help?
The documentation says:
You can fetch custom fields in a user profile by setting the projection parameter in a users.get or users.list request to custom or full.
so I would replace your line:
$r = $dir->users->get('xxx#xx.sg');
with:
$optParams = array('projection' => 'full');
$r = $dir->users->get('xxx#xx.sg', $optParams);

how to post on the user's twitter wall via my app?

I have a code, where people can post messages on the app's wall via my app
$consumerKey = '';
$consumerSecret = '';
$accessToken = '';
$accessTokenSecret = '';
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
$tweetMessage = $_POST['message'];
if(strlen($tweetMessage)<=140)
{
$tweet->post('statuses/update', array('status' => $tweetMessage));
$day = date('Y-m-d H:i:s');
mysql_query("INSERT INTO users (message,data,social) VALUES ('".$_POST['message']."','".$day."','tw')");
}
how can I make the script to post messages from users on their own walls via my app ?
is it possible?
thank you!
Actually, now that I think of it, what you probably want here is not a Twitter application at all. Since you want your visitors to post messages themselves, the correct way to do this is by using Twitter Web Intents.

Abraham Twitter API 1.1 Oauth library only returning "null"

I've been trying to get the latest status back from a user's twitter feed using Abraham Williams' Twitter Oauth Library (https://github.com/abraham/twitteroauth) I followed the tutorial found at http://www.webdevdoor.com/php/authenticating-twitter-feed-timeline-oauth/ and created the get_tweet.php file as my index. When I run this on my website an all white page, with "null" in the top left corner is displayed.
As far as I am to understand my Oauth keys I have are valid, I am using 000webhost.com to host my website, my webserver is using PHP 5.2.17 and cURL is enabled, From the tutorial and sample files I have been using my index file should be correct, my website can be found at http://authortryndaadair.site90.net where the results of this call is being dispayed.
I have been able to troubleshoot a small amount, but I am unsure of what else could try to get this Api call working. Any help in solving this problem would be much appreciated.
Below is the contents of the index file substituting for get_tweet1.1.php:
session_start();
// Path to twitteroauth library
require_once("twitteroauth/twitteroauth/twitteroauth.php");
$twitteruser = "JaneSmith";
$notweets = 10;
$consumerkey = "123456";
$consumersecret = "789123";
$accesstoken = "456789";
$accesstokensecret = "1023456";
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);
Assuming you are running the latest TwitterOAuth code the get line should look like this.
$tweets = $connection->get(
"statuses/user_timeline",
array("screen_name" => $twitteruser, "count" => $notweets)
);

Load tweets with search term in stead of username

Now I load the tweets from twitter by using a username like this: (keys and tokens are filled in my code)
<?php
session_start();
require_once("twitteroauth/twitteroauth.php"); // PATH TO TWITTEROUTH LIBRARY
if(isset($_GET['twitteruser']) && !empty($_GET['twitteruser'])) {
$twitteruser = $_GET['twitteruser']; // USER
}
$notweets = 60; // NUMBER OF TWEETS
// OAUTH SETTINGS APPLICATION (https://dev.twitter.com/apps/)
$consumerkey = "";
$consumersecret = "";
$accesstoken = "";
$accesstokensecret = "";
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);
$data = json_encode($tweets);
?>
As you can see I load the tweets with a username and set the number of tweets.
Can this easily be changed to load tweets with a search term?
I've search on google but strangely I can't find much.
I've found this in the twitter API but that doesn't seem to work ..
https://api.twitter.com/1.1/search/tweets.json?q=%23freebandnames&since_id=24012619984051000&max_id=250126199840518145&result_type=mixed&count=4
source: https://dev.twitter.com/docs/api/1.1/get/search/tweets
(I changed the link to that link)
But I get this output with a var_dump:
string(319) "{"statuses":[],"search_metadata":{"completed_in":0.004,"max_id":2.50126199841e+17,"max_id_str":"250126199840518145","query":"%23freebandnames","refresh_url":"?since_id=250126199840518145&q=%23freebandnames&result_type=mixed&include_entities=1","count":4,"since_id":2.40126199841e+16,"since_id_str":"24012619984051000"}}".....
The Twitter search API only returns tweets from the past week or so. See more info here.
The Search API is not complete index of all Tweets, but instead an index of recent Tweets. At the moment that index includes between 6-9 days of Tweets.
Note that the search feature on Twitter's website does query historical tweets, it is just the API that does not support them.

how to post tweet using twitter 1.1 api and twitteroauth

I use the below code to retrieve my tweets and echo json. This works fine.
<?php
session_start();
require_once('includes/twitter/twitteroauth.php');
$twitteruser = "xxxxxx";
$notweets = 3;
$consumerkey = "xxxxxxx";
$consumersecret = "xxxxxx";
$accesstoken = "xxxxxxxx";
$accesstokensecret = "xxxxxx";
$connection = new TwitterOAuth($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);
?>
Now i want to send a tweet using the similar code but it doesnot work. I am not sure if the sending syntax is correct. so please someone help me.
<?php
session_start();
require_once('includes/twitter/twitteroauth.php'); //Path to twitteroauth library
$twitteruser = "xxxxxx";
$notweets = 3;
$consumerkey = "xxxxxxx";
$consumersecret = "xxxxxx";
$accesstoken = "xxxxxxxx";
$accesstokensecret = "xxxxxx";
// start connection
$connection = new TwitterOAuth($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
//message
$message = "Hi how are you";
//send message
$status = $connection->post('https://api.twitter.com/1.1/statuses/update.json', array('status' => $message));
?>
I was using twitteroauth.php to post tweets myself when the new API broke it. You are using the $connection->post correctly, but it appears that function does not work anymore. The easiest solution I found was to swap out the twitteroauth.php with J7mbo's twitter-api-php file for the new 1.1 API:
https://github.com/J7mbo/twitter-api-php
Here's his step-by-step instructions for using it. I think you'll be pleasantly surprised to find you can leave most of your code the same, just switch the twitteroauth calls with his function calls at the appropriate places:
Simplest PHP example for retrieving user_timeline with Twitter API version 1.1
He doesn't provide the specific example of posting a tweet, but here's what what you need for that function:
$url = 'https://api.twitter.com/1.1/statuses/update.json';
$requestMethod = 'POST';
$postfields = array(
'status' => 'Hi how are you' );
echo $twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
With the new twitter API, you won't need to provide your username/password. The authentication token will handle everything.
Just use the example:
$connection->post('statuses/update', array('status' =>$message));
Try it
you won't need to username/password you can post via API key read the tutorial here.
http://designspicy.com/learn-how-to-post-on-twitter-using-php-and-oauth/
The issue is about enconding the value need to be enconde :
Example
$status = $connection->post('https://api.twitter.com/1.1/statuses/update.json', array('status' => rawurlencode($message)));
If you check in the library recomended https://github.com/J7mbo/twitter-api-php
That the way they encode the params

Categories