Good evening all,
I am using the PHP code from https://github.com/abraham/twitteroauth
The login works perfectly but when trying to post a status to Twitter i get the following error:
stdClass Object ( [errors] => Array ( [0] => stdClass Object ( [code] => 220 [message] => Your credentials do not allow access to this resource. ) ) )
I am using the following code:
session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'],
$_SESSION['oauth_token_secret']);
$token_credentials = $connection->getAccessToken($_REQUEST['oauth_verifier']);
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $token_credentials['oauth_token'],
$token_credentials['oauth_token_secret']);
$account = $connection->get('account/verify_credentials');
$status = $connection->post('statuses/update', array('status' => 'Text of status here'));
I have followed the same instructions as the website. Where am i going wrong?
In your case do you have to create an api on twitter ?
https://apps.twitter.com/
If you did, check the configuration of your api.
this link could be helpful.
https://dev.twitter.com/docs/auth/oauth/faq
Related
I'm trying to register a webhook url on twitter app and I'm using this package twitteroauth.
But facing following error after executing Twitter endpoint to register webhook.
stdClass Object(
[errors] => Array
(
[0] => stdClass Object
(
[code] => 214
[message] => Webhook URL does not meet the requirements. Invalid CRC token or json response format.
)
)
)
I was tried to register webhook through code as follows:
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_POST['oauth_token'], $_POST['oauth_token_secret']); $webhookURL = "https://example.com/twitter/webhook"; $ucontent = $connection->post('account_activity/all/my_env_name/webhooks', array('url' => $webhookURL));
After reading some posts regarding this issue, I was tried the following code with url encoding:
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_POST['oauth_token'], $_POST['oauth_token_secret']); $webhookURL = "https://example.com/twitter/webhook"; $ucontent = $connection->post('account_activity/all/my_env_name/webhooks', array('url' => urlencode($webhookURL)));
I also read in some articles and regenerate the keys, tokens of my app and newly generated key, tokens in code.
But not get success to register webhook url.
Execute the Twitter endpoint to get the webhook related details, but its giving empty array
{ "environments": [ { "environment_name": "my-environment-name", "webhooks": [] } ] }
Expected output after successful webhook url registration in this array response:
{ "environments": [ { "environment_name": "my-environment-name", "webhooks": [ { "id" => webhook-id-here, "url" => https://example.com/twitter/webhook, "valid" => 1, "created_timestamp" => some-date-time } ] } ] }
Please help me for this issue.
My problem is quite strange (at least to me) as I have a request URL that works in the console but throws the Sorry, that page does not exist error in my php script, even though the connection is up and running.
So this
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_secret']);
$user = $connection->get('account/verify_credentials');
print_r($user);
works great, the $user data is printed out on the screen.
However, I am unable to check a friendship status as:
$x = $connection->get('https://api.twitter.com/1.1/friendships/show.json?source_id=707482092&target_id=755811768&target_screen_name=assetspersonifi');
As I get the error.
When I put this request into the Twitter API console, it gives back the json that I don't receive in my php code.
I'm using Abraham's twitteroauth library but this does not work either:
$follows_faelazo = $connection->get('friendships/exists', array('user_a' => 'blfarago', 'user_b' => 'faelazo'));
if(!$follows_faelazo){
echo 'You are NOT following #faelazo!';
$connection->post('friendships/create', array('screen_name' => 'faelazo'));
} else {
print_r($follows_faelazo);
}
stdClass Object ( [errors] => Array ( [0] => stdClass Object ( [message] => Sorry, that page does not exist [code] => 34 ) ) )
I read that friendships/exists API is no longer supported by the Twitter API and I should use friendships/show but how if it's not working as you see above?
To prove that everything else is working, I can follow others with
$connection->post('friendships/create', array('screen_name' => 'faelazo'));
Why?
I found a way. Here's the documentation
$following = $connection->get('friendships/show', array(
'source_screen_name' => $_SESSION['username'],
'target_screen_name' => $screen_name_to_follow,
));
An alternative would be
$following = $connection->get('friendships/lookup', array('screen_name' => $screen_name_to_follow));
Look it up in Twitter doc.
I am trying to use codebird to tweet using PHP. Initially I was unable to get Access Token but after I defined CallbackURL in settings that issue seems to be resolved. Now it is returning oauth token:
Codebird\Codebird Object ( [_oauth_token:protected] => codehere [_oauth_token_secret:protected] => codehere [_return_format:protected] => 0 [_supported_media_files:protected] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [_version:protected] => 3.0.0-dev [_use_curl:protected] => 1 [_timeout:protected] => 10000 [_connectionTimeout:protected] => 3000 )
But when i try to tweet i get following error:
stdClass Object ( [errors] => Array ( [0] => stdClass Object ( [code]
=> 89 [message] => Invalid or expired token. ) ) [httpstatus] => 401 [rate] => )
Following is my code
Codebird\Codebird::setConsumerKey('copy+paste from twitter', 'copy+paste from twitter'); // I changed it to my settings
$cb = \Codebird\Codebird::getInstance();
if (! isset($_SESSION['oauth_token'])) {
// get the request token
$reply = $cb->oauth_requestToken(array(
'oauth_callback' => 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']
));
// store the token
$cb->setToken($reply->oauth_token, $reply->oauth_token_secret);
$_SESSION['oauth_token'] = $reply->oauth_token;
$_SESSION['oauth_token_secret'] = $reply->oauth_token_secret;
$_SESSION['oauth_verify'] = true;
// redirect to auth website
$auth_url = $cb->oauth_authorize();
header('Location: ' . $auth_url);
die();
} elseif (isset($_GET['oauth_verifier']) && isset($_SESSION['oauth_verify'])) {
// verify the token
$cb->setToken($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
unset($_SESSION['oauth_verify']);
// get the access token
$reply = $cb->oauth_accessToken(array(
'oauth_verifier' => $_GET['oauth_verifier']
));
// store the token (which is different from the request token!)
$_SESSION['oauth_token'] = $reply->oauth_token;
$_SESSION['oauth_token_secret'] = $reply->oauth_token_secret;
// send to same URL, without oauth GET parameters
header('Location: ' . basename(__FILE__));
die();
}
// assign access token on each page load
$cb->setToken($_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
print_r($cb);
$params = array(
'status' => 'Auto Post on Twitter with PHP http://goo.gl/OZHaQD #php #twitter'
);
$reply = $cb->statuses_update($params);
print_r($reply);
Thanks in advance for the assistance.
Is your callback address the one registered with Twitter in the app definition?
Do you have "read and write" access in the app definition?
Have you exceeded the rate limit for posting?
I'd check those things first, as I don't see anything obviously missing from your code snippet.
I want search for tweets in twitter. It is not working.
$parameters = array('q' => 'oauth');
$result = $connection->get('search', $parameters);
But when I do a user search it working perfectly.
$parameters = array('q' => 'oauth');
$result = $connection->get('users/search', $parameters);
I have also tried the below and that is also not working
$parameters = array('q' => 'oauth');
$result = $connection->get('search/tweets', $parameters);
What could be the reason?
Error message
stdClass Object
(
[errors] => Array
(
[0] => stdClass Object
(
[message] => Sorry, that page does not exist
[code] => 34
)
)
)
You seem to be using the library TwitterOAuth by Abraham Williams.
I know this is an old question but I have just had the same problem as OP and it might keep happening to anyone else since apparently this library has not been updated for a while.
The problem seems to be that Twitter is no longer accepting requests via the version 1 of their API. You have to change the $host variable in the file twitteroauth.php as follows:
/* Set up the API root URL. */
//public $host = "https://api.twitter.com/1/";
public $host = "https://api.twitter.com/1.1/";
EDIT:
I'm sorry for making this thread as I've figured out the problem.
Here's what happened:
First, I noticed that the URL being sent lacked a "/" between .com and account/verify_credentials. When I updated that, I got a 404 error. Then I looked at the twitteroauth.php and I noticed that the top one:
/* Set up the API root URL. */
public $host = "https://api.twitter.com/1/";
// public $host = "https://dev.twitter.com";
was commented out. I uncommented it, and commented out the lower one (which I'm guessing is no defunct or not used?) and now it works perfectly fine!
Thanks everybody, and thanks Mr. Williams for a great Oauth library!
I've been implementing Abraham Williams Twitter OAuth library and it's been going well so far until I tried to do something with it after connecting to Twitter. That's when I was getting a 303 HTTP code. I then changed my implementation to the way he implements it on his github site, and I still get the same issue. Does anyone have an idea of what's happening?
apitest.php is where the problem occurs, where the $connection variable has a 303 HTTP code. The corresponding $connection variables in redirect.php and callback.php both return a 200. By the way, all of the files are in the same directory.
Here's the code:
connect.php (The HTML on this page has a button that goes to redirect.php)
<?php
/**
* #file
* Check if consumer token is set and if so send user to get a request token.
*/
/**
* Exit with an error message if the CONSUMER_KEY or CONSUMER_SECRET is not defined.
*/
require_once('twitconfig.php');
if (CONSUMER_KEY === '' || CONSUMER_SECRET === '') {
echo 'A consumer key and secret are required.';
exit();
}
?>
redirect.php
<?php
/* Start session and load library. */
session_start();
require_once('twitteroauth.php');
require_once('twitconfig.php');
/* Build TwitterOAuth object with client credentials. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
/* Get temporary credentials. */
$request_token = $connection->getRequestToken(OAUTH_CALLBACK);
/* Save temporary credentials to session. */
$_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
/* If last connection failed don't display authorization link. */
switch ($connection->http_code) {
case 200:
/* Build authorize URL and redirect user to Twitter. */
$url = $connection->getAuthorizeURL($token);
header('Location: ' . $url);
break;
default:
/* Show notification if something went wrong. */
echo 'Could not connect to Twitter. Refresh the page or try again later.';
}
?>
callback.php
<?php
/**
* #file
* Take the user when they return from Twitter. Get access tokens.
* Verify credentials and redirect to based on response from Twitter.
*/
/* Start session and load lib */
session_start();
require_once('twitteroauth.php');
require_once('twitconfig.php');
/* If the oauth_token is old redirect to the connect page. */
if (isset($_REQUEST['oauth_token']) && $_SESSION['oauth_token'] !== $_REQUEST['oauth_token']) {
$_SESSION['oauth_status'] = 'oldtoken';
header('Location: ./clearsessions.php');
}
/* Create TwitteroAuth object with app key/secret and token key/secret from default phase */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
/* Request access tokens from twitter */
$access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);
/* Save the access tokens. Normally these would be saved in a database for future use. */
$_SESSION['access_token'] = $access_token;
/* Remove no longer needed request tokens */
unset($_SESSION['oauth_token']);
unset($_SESSION['oauth_token_secret']);
/* If HTTP response is 200 continue otherwise send to connect page to retry */
if (200 == $connection->http_code) {
/* The user has been verified and the access tokens can be saved for future use */
$_SESSION['status'] = 'verified';
header('Location: ./apitest.php');
} else {
/* Save HTTP status for error dialog on connnect page.*/
header('Location: ./clearsessions.php');
}
?>
apitest.php (where the problem is seen via a print_r)
<?php
session_start();
require_once('twitteroauth.php');
require_once('twitconfig.php');
if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) {
header('Location: ./clearsessions.php');
}
/* Get user access tokens out of the session. */
$access_token = $_SESSION['access_token'];
/* Create a TwitterOauth object with consumer/user tokens. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
/* Get logged in user to help with tests. */
$user = $connection->get('account/verify_credentials');
print_r($connection);
print_r($_SESSION);
?>
The output of the first print statement is:
TwitterOAuth Object (
[http_code] => 303
[url] => https://dev.twitter.comaccount/verify_credentials.json?oauth_consumer_key=UoXXKdfnVHh2fYD8Csw&oauth_nonce=09bc369393a8eab3a1dc36eaa6230b45&oauth_signature=pe5dYtYq%2FvlGb0c%2BnRR%2BQAXI0Ec%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1314249851&oauth_token=351699791-F6stFFk81poF8ooORgLLxaE2cpSdDT2Vr0Tz9yJm&oauth_version=1.0
[host] => https://dev.twitter.com
[timeout] => 30
[connecttimeout] => 30
[ssl_verifypeer] =>
[format] => json
[decode_json] => 1
[http_info] => Array ( [url] => https://dev.twitter.comaccount/verify_credentials.json?oauth_consumer_key=UoXXKdfnVHh2fYD8Csw&oauth_nonce=09bc369393a8eab3a1dc36eaa6230b45&oauth_signature=pe5dYtYq%2FvlGb0c%2BnRR%2BQAXI0Ec%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1314249851&oauth_token=351699791-F6stFFk81poF8ooORgLLxaE2cpSdDT2Vr0Tz9yJm&oauth_version=1.0
[content_type] =>
[http_code] => 303
[header_size] => 565
[request_size] => 401
[filetime] => -1
[ssl_verify_result] => 1
[redirect_count] => 0
[total_time] => 0.053773
[namelookup_time] => 0.003355
[connect_time] => 0.006399
[pretransfer_time] => 0.047636
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => 0
[upload_content_length] => 0
[starttransfer_time] => 0.053629
[redirect_time] => 0
[certinfo] => Array ( ) )
[useragent] => TwitterOAuth v0.2.0-beta2
[sha1_method] => OAuthSignatureMethod_HMAC_SHA1 Object ( )
[consumer] => OAuthConsumer Object ( [key] => UoXXKdfnVHh2fYD8Csw [secret] => SyKfxRorvUs6THwj5l26TGEkFOCGf1vG86N7PoS97o [callback_url] => ) [token] => OAuthConsumer Object ( [key] => 351699791-F6stFFk81poF8ooORgLLxaE2cpSdDT2Vr0Tz9yJm [secret] => q8fHaTaFvOUL8XKYX89LSgubr2fSl1xHBUAi8MUqIc [callback_url] => )
[http_header] => Array ( [location] => http://guide.a.id.opendns.com/?url=dev%2Etwitter%2Ecomaccount%2Fverify%5Fcredentials%2Ejson%3Foauth%5Fconsumer%5Fkey%3DUoXXKdfnVHh2fYD8Csw%26oauth%5Fnonce%3D09bc369393a8eab3a1dc36eaa6230b45%26oauth%5Fsignature%3Dpe5dYtYq%252FvlGb0c%252BnRR%252BQAXI0Ec%253D%26oauth%5Fsignature%5Fmethod%3DHMAC%2DSHA1%26oauth%5Ftimestamp%3D1314249851%26oauth%5Ftoken%3D351699791%2DF6stFFk81poF8ooORgLLxaE2cpSdDT2Vr0Tz9yJm%26oauth%5Fversion%3D1%2E0
[content_length] => 0
[connection] => close
[date] => Thu, 25 Aug 2011 05:24:50 GMT
[server] => OpenDNS Guide ) )
The output of the second print statement is:
Array (
[access_token] => Array (
[oauth_token] => 351699791-F6stFFk81poF8ooORgLLxaE2cpSdDT2Vr0Tz9yJm
[oauth_token_secret] => q8fHaTaFvOUL8XKYX89LSgubr2fSl1xHBUAi8MUqIc
[user_id] => 351699791
[screen_name] => jibjib21 )
[status] => verified )
Does anyone have an idea of what's going on with this 303 error and how to resolve it? Any help would be greatly appreciated. Thanks!
A 303 response is a redirect. My guess is you need to add .json (or whatever format you are using) to the end of the URL you are requesting.
I.e. instead of 'account/verify_credentials', you need to request 'account/verify_credentials.json'.