Laravel Socialite - 通过令牌获取用户详细信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30543991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:40:07  来源:igfitidea点击:

Laravel Socialite - get user details by token

facebook-graph-apilaravellaravel-5

提问by Shaddow

I'am developing android application with facebook login and laravel REST API server. After user login on mobile, app get tokenwhich is sent to my server. On server I want get facebook user details by token.

我正在开发带有 facebook 登录和 laravel REST API 服务器的 android 应用程序。用户在移动设备上登录后,应用程序获取发送到我的服务器的令牌。在服务器上,我想通过令牌获取 facebook 用户详细信息。

Facebook SDKfor PHP provides methods for that:

Facebook SDKfor PHP 提供了以下方法:

$session = new FacebookSession('token');
$me = (new FacebookRequest($session, 'GET', '/me',))->execute()->getGraphObject(GraphUser::className());

Can Laravel Socialiteobtain user facebook details based on that token? Or just use that Facebook SDK?

Laravel Socialite可以根据该令牌获取用户 facebook 详细信息吗?或者只是使用那个Facebook SDK

采纳答案by Bogdan

There is a method getUserByTokenimplemented in the Facebook Service Provider, however it's protectedand only used internally to get the userdetails.

getUserByTokenFacebook Service Provider 中实现了一个方法,但它protected仅在内部使用以获取user详细信息。

So you're better off using the Facebook SDK.

所以你最好使用 Facebook SDK。

回答by Steven M

I submitted a pull request to the repo which was accepted to the 2.o branch. You should be able to call Socialte::driver('facebook')->userFromToken($token)now.

我向 repo 提交了一个 pull request,该请求被 2.o 分支接受。你Socialte::driver('facebook')->userFromToken($token)现在应该可以打电话了。

https://github.com/laravel/socialite/pull/126

https://github.com/laravel/socialite/pull/126

Cheers.

干杯。

回答by Co?ku DEM?RHAN

You can also extend your Socialite Facebook Provider.

For example;
Create a class as SocialConnect in App/Library folder

您还可以扩展您的社交名流 Facebook 提供商。

例如;
在 App/Library 文件夹中创建一个类作为 SocialConnect

namespace App\Library;

use Laravel\Socialite\Two\FacebookProvider;

    class SocialConnect extends FacebookProvider {

        public function userFromToken($access_token)
        {
            $user = $this->mapUserToObject($this->getUserByToken($access_token));
            return $user->setToken($access_token);
        }
    }

Then add these lines in boot method on your App/Providers/AppServiceProvider.php

然后在你的 App/Providers/AppServiceProvider.php 的 boot 方法中添加这些行

 $socialite = $this->app->make('Laravel\Socialite\Contracts\Factory');
    $socialite->extend(
        'SocialConnect',
        function ($app) use ($socialite) {
            $config = $app['config']['services.facebook'];
            return $socialite->buildProvider(SocialConnect::class, $config);
        }
    );

Ready for use!

准备启用!

Socialite::driver('SocialConnect')->userFromToken($accessToken)

You can also add new methods as you wish in your SocialConnect class.

您还可以根据需要在 SocialConnect 类中添加新方法。