无法检测到回调源于Laravel Socialite中的提供者 [英] Can't detect callback is originated from provider in Laravel Socialite
问题描述
在Socialite文档中,它说以下行获取了用户信息:
In Socialite documentation, it says the following line gets the user information:
$user = Socialite::driver('twitter')->user();
这自然假定回调源于Twitter.但是,在某些情况下会无意中请求回调路由,并且上面的行给出了这样的错误:
This naturally assumes that the callback were originated from Twitter. However, callback route is requested unintentionally in some cases, and the line above gives the error like that:
InvalidArgumentException in TwitterProvider.php line 15:
Invalid request. Missing OAuth verifier.
在执行上面的代码行之前,如何检测回调来自预期的位置(此处为Twitter)?我的日志文件中充满了这些错误消息.我认为这些错误来自搜索引擎机器人请求回调路由(Auth机制似乎正常工作).
How can I detect the callback is originated from expected place (it is Twitter here) prior to executing the line above? My log file is full of these error messages. I think these errors come from search engine bots requesting callback route (Auth mechanism seems to work properly).
推荐答案
使用Twitter时,Twitter处理的重定向URL将发送两个查询参数 oauth_token 和 oauth_verifier .
When working with twitter, redirect URL handled by twitter will send two query parameters oauth_token and oauth_verifier.
因此,您可以添加路由验证以检查URL是否包含参数.
So you can add route validation to check that the URL contains the parameters.
在控制器中添加验证
public function yourcallbackfunction()
{
$v = \Validator::make(request()->all(), [
'oauth_token' => 'required',
'oauth_verifier' => 'required'
];
if($v->fails()) {
//do something as it's not a valid twitter callback
}else {
$user = Socialite::driver('twitter')->user();
}
}
注意:
最好在try catch中处理社交名流调用,以捕获社交名流可能引发的其他错误,例如无效令牌错误或api上的通信错误.
NOTE:
It's better to handle the socialite call within a try catch to catch other errors that might be thrown by the socialite like invalid token errors or communication error on api.
这篇关于无法检测到回调源于Laravel Socialite中的提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!