Twitter API : Not Getting User Email - Yii2

此生再无相见时 提交于 2019-12-02 05:46:24
Nana Partykar

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 :

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!