gpt4 book ai didi

php - Laravel - OAuth2 和 Facebook

转载 作者:行者123 更新时间:2023-11-30 02:49:08 26 4
gpt4 key购买 nike

我正在使用 Laravel 开发一个 RESTful API,我想使用 OAuth 实现以下场景(我使用以下包:https://github.com/lucadegasperi/oauth2-server-laravel):

  1. 用户登录 Facebook 并使用他的移动设备授予权限;
  2. 移动设备将 Facebook 访问 token 发送到我的 API,OAuth 生成我的特定于应用程序的访问 token 并将其返回给用户。

我已经为本地账户实现了密码流程。有人可以向我解释如何实现上述场景吗?

最佳答案

我还没有尝试过你提到的包,但我成功地使用了 https://github.com/artdarek/oauth-4-laravel .那里的示例应该可以让您轻松入门。

喜欢这个 FB 实现:

/**
* Login user with facebook
*
* @return void
*/
public function loginWithFacebook() {

// get data from input
$code = Input::get( 'code' );

// get fb service
$fb = OAuth::consumer( 'Facebook' );

// check if code is valid

// if code is provided get user data and sign in
if ( !empty( $code ) ) {

// This was a callback request from facebook, get the token
$token = $fb->requestAccessToken( $code );

// Send a request with it
$result = json_decode( $fb->request( '/me' ), true );

$message = 'Your unique facebook user id is: ' . $result['id'] . ' and your name is ' . $result['name'];
echo $message. "<br/>";

//Var_dump
//display whole array().
dd($result);

}
// if not ask for permission first
else {
// get fb authorization
$url = $fb->getAuthorizationUri();

// return to facebook login url
return Redirect::to( (string)$url );
}
}

关于php - Laravel - OAuth2 和 Facebook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24509244/

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