phpyii2yii2-basic-appyii2-api

Twitter API : Not Getting User Email - Yii2


I'm getting error like

Unknown Property – yii\base\UnknownPropertyException

Setting unknown property: yii\authclient\clients\Twitter::requestEmail

Whenever I am including 'requestEmail' => 'true', in 'authClientCollection' => [ for components in web.php

web.php

$config = [
  .
    .
  'components' => [
        .
        .
        'authClientCollection' => [
        'class' => 'yii\authclient\Collection',
        'clients' => [
        'twitter' => [
          'class' => 'yii\authclient\clients\Twitter',
          'requestEmail' => 'true',
          'consumerKey' => 'IFK2OMG0rKIFK2Jt4rLvw',
          'consumerSecret' => 'ImTprQzaOMG0rKZsZiPDIvwIFK2aOMG0rKZsZiPD',
        ],
      ],
    ],
],

UsersController.php (Controller)

class UsersController extends CommonController 
{
    .
    .
    public function actions() {
    return [
      .
      .
      'auth' => [
        'class' => 'yii\authclient\AuthAction',
        'successCallback' => [$this, 'oAuthSuccess'],
      ],
    ];
  }

    .
    .
    public function oAuthSuccess($client) {
      // get user data from client
      $userAttributes = $client->getUserAttributes();
      var_dump($userAttributes); die;
      // do some thing with user data. for example with $userAttributes['email']
  }

}

login.php (View)

.
.
<p class="text-center">
    <?= yii\authclient\widgets\AuthChoice::widget([
        'baseAuthUrl' => ['/users/users/auth']
   ]) ?>
</p>
.
.

But, as soon I'm omitting the line 'requestEmail' => 'true', from web.php. It's working. I'm getting all required data except email. But, problem is : I am not getting email of user trying to login. Any idea, how can i get. Any hint/suggestion will be a great help for me. Thanks.


Solution

  • At last, I got it.

    This answer is for those who just installed Twitter API or stuck in middle.

    Follow step by step.

    1) If you have already created "Consumer Key (API Key)" & "Consumer Secret (API Secret)". Then, directly go to Point-5. Else, Run this command php composer.phar require --prefer-dist yiisoft/yii2-authclient "*" in your system. And, generate "Consumer Key (API Key)" & "Consumer Secret (API Secret)". Follow Create New App & Twitter App Documentation

    2) In web.php

    $config = [
            .
              .
            'components' => [
                  .
                  .
                  'authClientCollection' => [
                  'class' => 'yii\authclient\Collection',
                  'clients' => [
                    'twitter' => [
                      'class' => 'yii\authclient\clients\Twitter',
                      'consumerKey' => 'Generated Consumer Key (API Key)',
                      'consumerSecret' => 'Generated Consumer Secret (API Secret)',
                    ],
                 ],
              ],
        ],
    

    3) In YourController.php (Controller) : Add auth section in function actions() And, function oAuthSuccess($client) (As I declared)

    class UsersController extends CommonController 
        {
              .
              .
              public function actions() {
                    return [
                      .
                      .
                      'auth' => [
                        'class' => 'yii\authclient\AuthAction',
                        'successCallback' => [$this, 'oAuthSuccess'],
                      ],
                    ];
                }
    
              .
              .
              public function oAuthSuccess($client) {
                // get user data from client
                $userAttributes = $client->getUserAttributes();
                var_dump($userAttributes); die;
                // do some thing with user data. for example with  $userAttributes['email']
              }
              .
              .
    
        }
    

    4) In YourView.php (View)

    <?= yii\authclient\widgets\AuthChoice::widget([
        'baseAuthUrl' => ['/users/users/auth']
    ]) ?>
    

    5) Send a Support Ticket to twitter for whitelisting your app. Select I need access to special permissions & Fill the required field & submit it.

    6) After Few minutes/Hours, You will get an email stating/subject "Request Email Access Granted.". Email will say you to login at apps.twitter.com.

    After sucessfull login,

    At The End,

    7) Go To Sub directories:

    Root Folder -> vendor -> yiisoft -> yii2-authclient -> clients -> Twitter.php

    Twitter.php

    Change

    protected function initUserAttributes()
    {
        return $this->api('account/verify_credentials.json', 'GET');
    }
    

    To

    protected function initUserAttributes()
    {
        return $this->api('account/verify_credentials.json', 'GET', ['include_email' => 'true']);
    }
    

    [Note: I am using Yii2-App-Basic. In Yii2-App-Advanced, Only File location path will get changed. ]

    Related Searched :