Twitter API:未获取用户电子邮件 - Yii2

Twitter API : Not Getting User Email - Yii2

我收到 错误 就像

Unknown Property – yii\base\UnknownPropertyException

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

每当我在 'authClientCollection' => [ 中包含 'requestEmail' => 'true', 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(控制器)

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(查看)

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

但是,一旦我从 web.php 中省略了行 'requestEmail' => 'true',。它的工作。我正在获取除 email 之外的所有必需数据。但是,问题是:我没有收到 email 用户尝试登录。任何想法,我怎么能得到。任何 hint/suggestion 都会对我有很大的帮助。谢谢。

终于明白了

此答案适用于刚刚安装 Twitter API卡在中间 .

的人

循序渐进。

1) 如果您已经创建了“Consumer Key (API Key)” & “消费者机密(API 机密)”。 然后,直接去Point-5. 别的, 运行 这个命令在你的系统中 php composer.phar require --prefer-dist yiisoft/yii2-authclient "*"。并且,生成“消费者密钥(API密钥)”和“消费者秘密(API秘密)”。关注 Create New App & Twitter App Documentation

2)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) : 在函数 [=17= 中添加 auth 部分] 并且,函数 oAuthSuccess($client) (正如我声明的那样)

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)YourView.php(查看)

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

5) 发送 Support Ticket 到 Twitter 以将您的应用列入白名单。 Select I need access to special permissions & 填写必填字段并提交。

6) 几次 minutes/Hours 后,您将收到一封电子邮件 stating/subject “请求电子邮件访问权限。”。电子邮件会提示您登录 apps.twitter.com

登录成功后,

  • 点击你的Application Name.
  • 转到“设置”选项卡,填写Privacy Policy URLTerms of Service URL文本字段。通过 Update Settings 按钮保存。
  • 转到“权限”选项卡,选中Request email addresses from users复选框。然后,通过 Update Settings 按钮保存它。
  • 转到“密钥和访问令牌”选项卡,然后在 Application Actions 中再次“重新生成消费者密钥和机密”部分。
  • 重新生成后 Consumer Key (API Key) & Consumer Secret (API Secret) 将其保存到 Web.php 文件。
  • 不要忘记关注本节中的最后 2 点。

最后,

7) 转到子目录:

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

Twitter.php

改变

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

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

[注意:我正在使用 Yii2-App-Basic。在 Yii2-App-Advanced 中,只有文件位置路径会发生变化。 ]

相关搜索

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