简体   繁体   English

Laravel-OAuth2和Facebook

[英]Laravel - OAuth2 and Facebook

I'm developing a RESTful API with Laravel and I would like to achieve the following scenario with OAuth (I'm using the following package: https://github.com/lucadegasperi/oauth2-server-laravel ): 我正在使用Laravel开发RESTful API,并希望通过OAuth实现以下方案(我正在使用以下软件包: https : //github.com/lucadegasperi/oauth2-server-laravel ):

  1. A user logs into Facebook and grants permission using his mobile device; 用户登录Facebook并使用其移动设备授予权限;
  2. The mobile device sends the Facebook access token to my API and OAuth generates my application-specific access token and returns it to the user. 移动设备将Facebook访问令牌发送到我的API,OAuth生成我的应用程序特定的访问令牌并将其返回给用户。

I have already implemented the password flow for local accounts. 我已经实现了本地帐户的密码流。 Could somebody explain me how to achieve the above scenario? 有人可以解释一下如何实现上述方案吗?

i haven't tried your mentioned package, but i was successful using https://github.com/artdarek/oauth-4-laravel . 我没有尝试过您提到的软件包,但是我使用https://github.com/artdarek/oauth-4-laravel成功。 the examples there should get you started with ease. 那里的例子应该使您轻松入门。

like this FB implementation: 像这样的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 );
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM