gpt4 book ai didi

facebook - 整合 FosUserBundle 和 HWIOAuthBundle

转载 作者:行者123 更新时间:2023-12-03 22:09:03 28 4
gpt4 key购买 nike

Integrating with FOSUserBundle 中实现安装和文档以及来自 A way to integrate FosUserBundle and HWIOAuthBundle 的评论和线索

从 Facebook connect API 获取正确的代码,我可以将我的 Facebook 帐户连接到该项目,我可以看到该应用程序已添加到我的 Facebook 应用程序中。但现在我想从 FB 获取用户数据并将其存储在我的数据库中。我有点明白该怎么做,但是函数 public function connect(UserInterface $user, UserResponseInterface $response)public function loadUserByOAuthUserResponse(UserResponseInterface $response) 之后没有被调用与FB连接。我确信这一点,因为我在其中添加了不存在的方法,就像 getWhatever 和 setSomething 一样,即使有语法错误,也没有发生任何事情,也没有记录任何日志,这意味着 100% 永远不会调用这个类。如何解决这个问题?以下是按照示例编辑的文件。提前致谢。

MyFOSUBUserProvider.php

namespace MarketplaceBundle\Security\Core\User;

use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider as BaseFOSUBProvider;
use Symfony\Component\Security\Core\User\UserInterface;

class MyFOSUBUserProvider extends BaseFOSUBProvider
{
/**
* {@inheritDoc}
*/
public function connect(UserInterface $user, UserResponseInterface $response)
{
// get property from provider configuration by provider name
// , it will return `facebook_id` in that case (see service definition below)
$property = $this->getProperty($response);
$username = $response->getUsername(); // get the unique user identifier

//we "disconnect" previously connected users
$existingUser = $this->userManager->findUserBy(array($property => $email));
if (null !== $existingUser) {
// set current user id and token to null for disconnect
// ...

$this->userManager->updateUser($existingUser);
}
// we connect current user, set current user id and token
// ...
$this->userManager->updateUser($user);
}

/**
* {@inheritdoc}
*/
public function loadUserByOAuthUserResponse(UserResponseInterface $response)
{
$userEmail = $response->getEmail();
$user = $this->userManager->findUserByEmail($userEmail);

// if null just create new user and set it properties
if (null === $user) {
$username = $response->getRealName();
$user = new User();
$user->setFirstName($response->getFirstName());
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
// ... save user to database

return $user;
}else{
$username = $response->getYamen();
$user = new User();
$user->setUsername($username);
}
// else update access token of existing user
$serviceName = $response->getResourceOwner()->getName();
$setter = 'set' . ucfirst($serviceName) . 'AccessToken';
$user->$setter($response->getAccessToken());//update access token

return $user;
}
}

安全.yml

security:
encoders:
FOS\UserBundle\Model\UserInterface: bcrypt

providers:
fos_userbundle:
id: fos_user.user_provider.username

firewalls:
...
secured_area:
pattern: ^/
oauth:
resource_owners:
facebook: /login_facebook
login_path: /login
failure_path: /login
oauth_user_provider:
service: my_user_provider

# Turn on anonymous for testings need.
anonymous: ~

access_control:
...

配置.yml

...
hwi_oauth:
connect:
account_connector: my_user_provider
firewall_names: [secured_area]
fosub:
username_iterations: 30
properties:
facebook: facebook_id
resource_owners:
facebook:
type: facebook
client_id: 'id'
client_secret: 'secret'
scope: "email,public_profile,user_friends,user_birthday,user_location"
options:
display: popup

服务.yml

parameters:
...
my_user_provider.class: MarketplaceBundle\Security\Core\User\MyFOSUBUserProvider

services:
...
hwi_oauth.user.provider.entity:
class: HWI\Bundle\OAuthBundle\Security\Core\User\OAuthUserProvider
my_user_provider:
class: "%my_user_provider.class%"
arguments: ['@fos_user.user_manager', { facebook: facebook_id }]

#routing.yml
hwi_oauth_redirect:
resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml"
prefix: /facebook/connect

facebook_login:
path: /login_facebook
...

facebookJsCallFunction

window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
appId : 'id', // App ID from the app dashboard
status : true, // Check Facebook Login status
xfbml : true, // Look for social plugins on the page
version : 'v2.5'
});
};

// Load the SDK asynchronously
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

function fb_login() {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// connected
alert('Already connected, redirect to login page to create token.');
console.log(response);
document.location = "{{ url("account") }}";
} else {
// not_authorized
FB.login(function(response) {
if (response.authResponse) {
document.location = "{{ url("account") }}";
} else {
alert('Cancelled.');
}
}, {scope: 'email,public_profile,user_friends,user_birthday,user_location'});
}
});
}

最佳答案

这是我自己的解决方案:

security:
firewalls:
main:
oauth:
oauth_user_provider:
# service: hwi_oauth.user.provider.fosub_bridge
service: acme.user_provider

即使我收到“用户帐户已禁用”。我的 FB 测试 App 测试用户的错误消息

关于facebook - 整合 FosUserBundle 和 HWIOAuthBundle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39467905/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com