Twitter API:未获取用户电子邮件 - Yii2 [英] Twitter API : Not Getting User Email - Yii2
问题描述
我遇到错误,比如
<块引用>未知属性 – yii\base\UnknownPropertyException
设置未知属性:yii\authclient\clients\Twitter::requestEmail
每当我包含 'requestEmail' =>'true',
在 'authClientCollection' =>[
for components
in web.php
web.php
$config = [..'组件' =>[..'authClientCollection' =>['类' =>'yii\authclient\Collection','客户' =>['推特' =>['类' =>'yii\authclient\clients\Twitter','请求电子邮件' =>'真的','消费者密钥' =>'IFK2OMG0rKIFK2Jt4rLvw','消费者秘密' =>'ImTprQzaOMG0rKZsZiPDIvwIFK2aOMG0rKZsZiPD',],],],],
UsersController.php(控制器)
class UsersController 扩展了 CommonController{..公共功能动作(){返回 [..'认证' =>['类' =>'yii\authclient\AuthAction','successCallback' =>[$this, 'oAuthSuccess'],],];}..公共函数 oAuthSuccess($client) {//从客户端获取用户数据$userAttributes = $client->getUserAttributes();var_dump($userAttributes);死;//对用户数据做一些事情.例如 $userAttributes['email']}}
login.php(查看)
<预><代码>..<p class="text-center"><?= yii\authclient\widgets\AuthChoice::widget(['baseAuthUrl' =>['/users/users/auth']]) ?></p>..但是,一旦我省略了行 'requestEmail' =>'true',
来自 web.php.它正在工作.除了email
,我得到了所有必需的数据.但是,问题是:我没有收到尝试登录的用户的电子邮件
.任何想法,我怎么能得到.任何提示/建议对我都有很大帮助.谢谢.
我终于明白了.
此答案适用于刚刚安装Twitter API 或卡在中间的用户.
一步一步来.
1) 如果您已经创建了Consumer Key (API Key)"&消费者秘密(API 秘密)".然后,直接转到第 5 点.别的,在您的系统中运行此命令 php composer.phar require --prefer-dist yiisoft/yii2-authclient "*"
.并且,生成Consumer Key (API Key)"&消费者秘密(API 秘密)".关注 创建新应用 &Twitter 应用文档
2) 在 web.php
$config = [..'组件' =>[..'authClientCollection' =>['类' =>'yii\authclient\Collection','客户' =>['推特' =>['类' =>'yii\authclient\clients\Twitter','消费者密钥' =>'生成的消费者密钥(API 密钥)','消费者秘密' =>'生成的消费者秘密(API秘密)',],],],],
3) 在 YourController.php (Controller) 中:在函数 actions()
中添加 auth
部分和, function oAuthSuccess($client)
(正如我所声明的)
class UsersController 扩展了 CommonController{..公共功能动作(){返回 [..'认证' =>['类' =>'yii\authclient\AuthAction','successCallback' =>[$this, 'oAuthSuccess'],],];}..公共函数 oAuthSuccess($client) {//从客户端获取用户数据$userAttributes = $client->getUserAttributes();var_dump($userAttributes);死;//对用户数据做一些事情.例如 $userAttributes['email']}..}
4) 在 YourView.php(视图)
<?= yii\authclient\widgets\AuthChoice::widget(['baseAuthUrl' =>['/users/users/auth']]) ?>
5) 向 Twitter 发送支持票以将其列入白名单你的应用程序.选择我需要访问特殊权限
&填写必填字段 &提交.
6) 几分钟/几小时后,您将收到一封电子邮件,说明/主题为请求电子邮件访问权限.".电子邮件会提示您登录 apps.twitter.com.
登录成功后,
- 点击您的
应用程序名称
. - 转到设置"标签,填写
隐私政策网址
&服务条款 URL
文本字段.通过更新设置
按钮保存. - 转到权限"选项卡,选中
从用户请求电子邮件地址
复选框.并且,通过Update Settings
按钮保存它. - 转到密钥和访问令牌"选项卡,然后在
应用程序操作
部分再次重新生成消费者密钥和秘密".立> - 重新生成
Consumer Key (API Key)
后Consumer Secret (API Secret)
将其保存到 Web.php 文件中. - 不要忘记遵循本节中的最后 2 点.
最后,
7) 转到子目录:
根文件夹 ->供应商 ->yiisoft ->yii2-authclient ->客户 ->Twitter.php
Twitter.php
改变
受保护的函数 initUserAttributes(){返回 $this->api('account/verify_credentials.json', 'GET');}
到
受保护的函数 initUserAttributes(){return $this->api('account/verify_credentials.json', 'GET', ['include_email' => 'true']);}
[注意:我使用的是 Yii2-App-Basic.在 Yii2-App-Advanced 中,只会更改文件位置路径.]
相关搜索:
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.
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 throughUpdate Settings
button. - Go to "Permissions" tab, Check
Request email addresses from users
checkbox. And, Save it throughUpdate 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 :
这篇关于Twitter API:未获取用户电子邮件 - Yii2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!