Reputation: 7693
I am trying to give the tweeter posting UI from my website for users who has twitter account and post tweet into their home page. In addition I like to give access to webcam to cartoonize their face and put that image with the tweet. (my main target is cartoonize image posts)
Initially I take the Codebird PHP library to make a tweet. https://github.com/jublonet/codebird-php
I tested my first post like same example Codebird given in their github page.
require_once ('codebird.php');
\Codebird\Codebird::setConsumerKey('YOURKEY', 'YOURSECRET'); // static, see README
$cb = \Codebird\Codebird::getInstance();
/*You may either set the OAuth token and secret, if you already have them:*/
$cb->setToken('YOURTOKEN', 'YOURTOKENSECRET');
This part work nicely.
But this is only for the app developer. because YOURTOKEN and YOURTOKEN SECRET is in the https://apps.twitter.com/app/ page.
Therefor I try to run this code as given in Codebird sample.
<?php
require_once('./src/codebird.php');
\Codebird\Codebird::setConsumerKey('REPLACE_WITH_MY_CONSUMER_KEY');
$cb = \Codebird\Codebird::getInstance();
/*$cb->setToken('REPLACE_WITH_MY_CONSUMER_TOKEN');
session_start();
if (!empty($_SESSION['oauth_token'])) {
echo 'ttt';
var_dump($_SESSION['oauth_token']);
// get the request token
$reply = $cb->oauth_requestToken([
'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([
'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']);
$reply = $cb->statuses_update('status=Whohoo, I just Tweeted!');
?>
But it gives this error.
Fatal error: Uncaught Codebird\CodebirdCredentialsException: To call this API, the OAuth access token must be set.
Any clue for this ? or;
Are there anything easy method like facebook handle this thing very easy. It's redirect to facebook login page and after logged use can post whatever user like from the my website. Therefore any way to do something for the twitter , Like call native login window and getting the token and secret from that login session and post the tweet message from my website ?
Upvotes: 0
Views: 1316
Reputation: 7693
I just added callback url from the my application page (https://apps.twitter.com/app) and now it's work perfect. It will automatically redirect to login page (like bellow) if user not logged in or else ask permission to approve to get the token and secret key.
Call back URL from setting tab:
User redirect work fine if user not logged-in:
Upvotes: 0