问题描述
我正在尝试为Web应用程序实现Facebook登录.这是FacebookConnect.php
I am trying to implement facebook login for a web-app.Here is the FacebookConnect.php
<?php
namespace Vendor\GiftBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Generator\UrlGenerator;
class FacebookConnectController extends Controller
{
/**
* @Route("/connect/facebook", name="connect_facebook")
*/
public function connectFacebookAction(Request $request)
{
// redirect to Facebook
$redir = $this->generateUrl('connect_facebook_check', array(), UrlGeneratorInterface::ABSOLUTE_URL);
$facebookOAuthProvider = $this->get('app.facebook_provider');
$url = $facebookOAuthProvider->getAuthorizationUrl([
// these are actually the default scopes
'scopes' => ['public_profile', 'email'],
'redirect_uri' => [$redir],
]);
return $this->redirect($url);
}
/**
* @Route("/connect/facebook-check", name="connect_facebook_check")
*/
public function connectFacebookActionCheck()
{
// will not be reached!
}
}
当我按下触发connectFacebookAction函数的按钮时,我得到了FB错误 redirect_uri URL必须是绝对的.就像它忽略了我在参数中给他的绝对URL.
When I hit the button which triggers the connectFacebookAction function I get the FB Error The redirect_uri URL must be absolute. It's like it ignores the absolute URL I have given him in the parameters.
我在做什么错了?
授权URL是否在任何情况下都更改或对于应用程序保持不变?我能把它硬编码进去,直到弄清楚如何解决这个问题?
Is the authorization URL changing in any circumstances or remains the same for the app? Could I just hardcode it in until I figure out how to fix this?
EDIT2 :我弄清楚了为什么URL是相对的而不是绝对的.这是我的服务配置:
EDIT2: I figured out why the URL is relative and not absolute.Here is my services configuration:
services:
app.facebook_provider:
class: League\OAuth2\Client\Provider\Facebook
arguments:
-
clientId: %facebook_app_id%
clientSecret: %facebook_app_secret%
graphApiVersion: v2.8
redirectUri: "@=service('router').generate('connect_facebook_check', {}, true)"
如何从服务中的路由生成绝对URL?
推荐答案
您是否正在使用League/oauth2-client的1.x版本(可以在composer.lock中签入)?那个忽略了getAuthorizationUrl中的redirect_uri 并使用您在构造函数中传递的那个.如果您使用的是该版本,请将app.facebook_provider的定义更改为要读取
Are you using version 1.x of league/oauth2-client (you can check in composer.lock)? That ignores the redirect_uri in getAuthorizationUrl and uses the one you pass in the constructor. If you're using that version, change the definition for app.facebook_provider to read
redirectUri: "@=service('router').generate('connect_facebook_check', {}, 0)"
请注意0而不是true. UrlGeneratorInterface常量已更改2.8 .
Note the 0 instead of true. The UrlGeneratorInterface constants have changed in 2.8.
这篇关于Symfony3 Facebook登录-redirect_uri URL被转换为相对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!