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 :)
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.
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();
If for some reason you have an "Invalid Token" request error, just go and regenerate them on your Twitter App settings.