【问题标题】:Twitter API : Not Getting User Email - Yii2Twitter API:未获取用户电子邮件 - Yii2
【发布时间】:2016-03-29 12:29:48
【问题描述】:

我收到错误,比如

未知属性 – yii\base\UnknownPropertyException

设置未知属性:yii\authclient\clients\Twitter::requestEmail

每当我在 web.php 中将'requestEmail' => 'true', 包含在'authClientCollection' => [ 中以用于components

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' =&gt; 'true', 行。它正在工作。我正在获取除email 之外的所有必需数据。但是,问题是:我没有收到email 的用户尝试登录。任何想法,我怎么能得到。任何提示/建议都会对我有很大帮助。谢谢。

【问题讨论】:

  • 我认为现在可以获取用户的电子邮件,请试试这个:stackoverflow.com/a/33867823/386579
  • 是的@shasikanth:我发送了那个请求。等待批准。让我们看看发生了什么 。 谢谢..
  • 嗨@shasikanth:我收到来自推特的邮件,说明“请求电子邮件访问权限”。我按照他们在邮件中所说的步骤进行操作。但是,即使我无法访问email。你对此有什么想法吗? ?
  • 不知道为什么它无法获取电子邮件。可能是您正在尝试在本地主机中。也可以尝试使用任何公共 URL。
  • 嗨@jekaby:我得到了解决方案。即使我将其发布为答案。现在,我得到了 email 的价值。感谢您的回复。

标签: php yii2 yii2-basic-app yii2-api


【解决方案1】:

我终于明白了。

此答案适用于那些刚刚安装 Twitter API卡在中间 的人。

一步一步来。

1) 如果您已经创建了“Consumer Key (API Key)”和“Consumer Secret (API Secret)”。 然后,直接转到Point-5。 别的, 在您的系统中运行此命令php composer.phar require --prefer-dist yiisoft/yii2-authclient "*"。并且,生成“Consumer Key (API Key)”和“Consumer Secret (API Secret)”。关注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() 和函数 oAuthSuccess($client) 中添加 auth 部分(正如我声明的那样)

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) 向 twitter 发送 Support Ticket 以将您的应用列入白名单。选择I need access to special permissions并填写必填字段并提交。

6) 几分钟/几小时后,您将收到一封说明/主题“请求电子邮件访问权限。”的电子邮件。电子邮件会提示您登录apps.twitter.com

登录成功后,

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

最后,

7) 转到子目录:

Root Folder -&gt; vendor -&gt; yiisoft -&gt; yii2-authclient -&gt; clients -&gt; 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 中,只有文件位置路径会被改变。 ]

相关搜索

【讨论】:

    猜你喜欢
    • 2017-08-12
    • 1970-01-01
    • 2012-05-10
    • 1970-01-01
    • 2014-12-16
    • 2016-11-11
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    相关资源
    最近更新 更多