Introduction ¶
This tutorial is not about allowing users to sign into your application by using any of the OAuth providers available with the official yii2-authclient extension, it is about how to interact with your Twitter account, the one you set for your Yii2 app to promote your site, using the Single Access Token technique.
It is quite simple, but I am sure this small tutorial will help more than one of you out there :)
Get your access tokens ¶
Go to https://apps.twitter.com/app/new and create your app. Once done, make note of the Consumer Key and Consumer Secret on your application settings page. Then generate your private access token (its normally the button under your Api keys).
After generating them, grab your Access Token and your Access Token Secret.
Lets REST ¶
First, make sure you add the official yii2-authclient extension to your application (follow its instructions for installation).
We are set, with your the extension installed and the Consumer Key, Consumer Secret, Access Token and Access Token Secret, its time to test if you are able to communicate with Twitter.
Configure the "params" section of your application configuration file to hold, your Twitter application keys information:
// ...
'params' => [
'twitterApiKey' => 'YOUR CONSUMER KEY',
'twitterApiSecret' => 'YOUR CONSUMER SECRET',
'twitterAccessToken' => 'YOUR ACCESS TOKEN',
'twitterAccessTokenSecret' => 'YOUR ACCESS TOKEN SECRET'
]
// ...
Done, lets test it...
use yii\authclient\clients\Twitter;
use yii\authclient\OAuthToken;
// create your OAuthToken
$token = new OAuthToken([
'token' => Yii::$app->params['twitterAccessToken'],
'tokenSecret' => Yii::$app->params['twitterAccessTokenSecret']
]);
// start a Twitter Client and configure your access token with your
// recently created token
$twitter = new Twitter([
'accessToken' => $token,
'consumerKey' => Yii::$app->params['twitterApiKey'],
'consumerSecret' => Yii::$app->params['twitterApiSecret']
]);
var_dump($twitter->api('statuses/home_timeline.json', 'GET'));die();
Final Notes ¶
If for some reason you have an "Invalid Token" request error, just go and regenerate them on your Twitter App settings.
facebook
How we can use facebook like this ?
Using access token
I guess you should not copy and paste your access token and access token secret into Yii application's configuration. From Twitter app keys page:
Instead of proposed method you should configure
authClientCollection
like it's shown inAuthClient
extension's documentation:'components' => [ 'authClientCollection' => [ 'class' => 'yii\authclient\Collection', 'clients' => [ 'twitter' => [ 'class' => 'yii\authclient\clients\Twitter', 'consumerKey' => 'YOUR CONSUMER KEY', 'consumerSecret' => 'YOUR CONSUMER SECRET' ] ] ] ] ...
And then implement authentication via Twitter. Each time when user logs in through AuthAction an appropriate access token of this user is saved in PHP session in OAuth1 client (what Twitter is). This means that there is no need to provide an access token if the user passed authentication, and you can make Twitter API call like this:
if (!\Yii::$app->user->isGuest) { /* @var $authClientCollection \yii\authclient\Collection */ $authClientCollection = \Yii::$app->authClientCollection; /* @var $twitter \yii\authclient\clients\Twitter */ $twitter = $authClientCollection->getClient('twitter'); $tweets = $twitter->api('statuses/home_timeline.json', 'GET'); ... }
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.