编程语言
首页 > 编程语言> > PHP-Twitter API:无法获取用户电子邮件-Yii2

PHP-Twitter API:无法获取用户电子邮件-Yii2

作者:互联网

我收到类似的错误

Unknown Property – yii\base\UnknownPropertyException

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

每当我包含’requestEmail’=> ‘true’,在’authClientCollection’=>中[用于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>
.
.

但是,我很快就省略了“ requestEmail” =>行.来自web.php的“ true”.工作正常除了电子邮件,我正在获取所有必需的数据.但是,问题是:我没有收到尝试登录的用户的电子邮件.任何想法,我怎么能得到.任何提示/建议都将对我有很大的帮助.谢谢.

解决方法:

最后,我明白了.

该答案适用于刚刚安装Twitter API或陷入困境的用户.

循序渐进.

1)如果您已经创建了“消费者密钥(API密钥)”和“消费者机密(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)在YourController.php(Controller)中:在函数actions()中添加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,将您的应用列入白名单.选择我需要访问特殊权限&填写必填字段提交.

6)在几分钟/小时后,您将收到一封电子邮件,说明/主题“请求授予电子邮件访问权限”.电子邮件将告诉您登录apps.twitter.com.

成功登录后,

>单击您的应用程序名称.
>转到“设置”标签,填写隐私权政策网址和服务条款URL文本字段.通过“更新设置”按钮将其保存.
>转到“权限”标签,选中“从用户请求电子邮件地址”复选框.并且,通过“更新设置”按钮将其保存.
>转到“密钥和访问令牌”选项卡,然后在“应用程序操作”部分中再次“重新生成使用者密钥和秘密”.
>重新生成使用者密钥(API密钥)后,消费者秘密(API Secret)将其保存到Web.php文件.
>不要忘了遵循本节的最后2点.

在末尾,

7)转到子目录:

根文件夹->供应商-> yiisoft-> yii2-authclient->客户-> 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

标签:yii2-basic-app,php,yii2,yii2-api
来源: https://codeday.me/bug/20191013/1905422.html