Twitter API : Not Getting User Email - Yii2

2019-03-01 07:41发布

问题:

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.

回答1:

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,

  • click on your Application Name.
  • Go to "Settings" tab, fill Privacy Policy URL & Terms of Service URL text fields. Save it through Update Settings button.
  • Go to "Permissions" tab, Check Request email addresses from users checkbox. And, Save it through Update Settings button.
  • Go to "Keys and Access Tokens" tab, And Again "Regenerate consumer key and secret" in Application Actions section.
  • After regenerating Consumer Key (API Key) & Consumer Secret (API Secret) save it to Web.php file.
  • Don't forget to follow Last 2 Points in this section.

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 :

  • yii2-framework-facebook-and-google-login-using-authclient-not-working
  • twitter-api-get-user-email