使用Facebook SDK从iOS登录Laravel网络应用程序


Laravel webapp login from iOS with Facebook SDK

我有一个使用Laravel的网络应用程序。我在我的网络应用程序中注册了一些用户。不想创建帐户的用户可以使用Facebook/Google+登录。

为了建立Facebook/Google+连接,我使用了oAuth2连接:oauth-4-laravel

在iOS应用程序上,用户可以使用用户名和密码登录,但也可以使用FB/G+SDK使用Facebook/Goog+登录。

我的问题是,如何从iOS登录通过Facebook/Google+连接的网络应用程序用户。

artdarek/oauth-4-laravel需要社交网络提供的"代码",我不知道如何在iOS上获得它。

这里是想要通过网络浏览器连接到网络应用程序的用户的代码:

 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 (User::where('fb_id','=', $result['id'])->count() == 0) {
             $user = new User;
            $user->firstname = $result['first_name'];
            $user->lastname = $result['last_name'];
            $user->username = $result['email'];
            $user->email = $result['email'];
            $user->fb_id = $result['id'];
            $user->yearofbirth = substr($result['birthday'],6,9);
            $user->fk_role=3;
            if ($result['gender'] == 'male') {
                $user->sex = 1;
            }
            else{
                $user->sex = 0;
            }

            $user->save();
        }
        else{
            $user = User::where('fb_id','=', $result['id'])->first();
        }
    Auth::login($user);
    Userslog::log('desktop_facebook_login');
    return Redirect::to('/')->with('message', 'Logged in with Facebook');
    }
    // if not ask for permission first
    else {
        // get fb authorization
        $url = $fb->getAuthorizationUri();
        // return to facebook login url
         return Redirect::to( (string)$url );
    }
}

我终于在github、上找到了oauth-4-laravel问题的解决方案

我只需要使用iOS访问令牌,并使用该令牌将用户连接到网络应用程序。

这里的代码laravel代码使用令牌;)

use OAuth'OAuth2'Token'StdOAuth2Token;
$token_interface = new StdOAuth2Token(Input::get( 'token' ));       
$network = OAuth::consumer( 'Facebook' );
$network->getStorage()->storeAccessToken('Facebook', $token_interface);
$result = json_decode( $network->request( '/me' ), true );
$fb_id = $result['id'];

要继续,最好的方法是;在iOS、上使用Facebook/Google+登录

登录成功后,获取AccessToken并发送到webapp。然后,网络应用程序使用你的令牌将用户再次登录到Facebook/Google+,最后,用户得到验证,你可以将其登录到laravel网络应用程序。

祝你今天过得愉快。

我在这里也有同样的情况。

使用您提供的解决方案,您如何阻止其他应用程序发送自己的AccessToken并访问您的Laravel应用程序?

我假设您通过POST请求将令牌从iOS应用程序发送到Laravel应用程序的某个端点,因此任何知道该端点的开发人员都可以假装登录到您的移动应用程序,不是吗?