【问题标题】:Laravel Socialite: override Provider authorization URLLaravel Socialite:覆盖提供者授权 URL
【发布时间】:2019-11-15 20:05:40
【问题描述】:

我正在使用 Laravel 6.5、laravel/socialite 包和 SocialiteProviders/Providers

我的问题是关于 SocialiteProviders/Providers/Etsy,但可以扩展到 Laravel Socialite 的任何提供者。

默认情况下,使用 Etsy 进行授权时,所有 Etsy's permission scopes 均已启用。这是因为 API url 还没有定义任何 scope 查询参数:

urlTemporaryCredentials()方法:

public function urlTemporaryCredentials()
{
    return 'https://openapi.etsy.com/v2/oauth/request_token';
}

为了限制 Etsy 的一些权限,我需要添加范围参数来返回这样的 URL:

https://openapi.etsy.com/v2/oauth/request_token?scope=email_r%20listings_r

scope 查询参数将设置我需要的权限。

这里是我的SocialLoginController.php中调用授权的地方

protected function getAuthorizationFirst($provider)
{
    $socialite = Socialite::driver($provider);
    return $socialite->stateless()->redirect();
}

有没有办法覆盖第一个 URL 或为其添加查询参数?

【问题讨论】:

    标签: php laravel laravel-socialite


    【解决方案1】:

    我也在使用 Socialite,但对于 facebook,我发现的唯一方法就是这个

    return Socialite::driver($provider)->fields([
        'first_name', 'last_name', 'email', 'birthday', 'name'
    ])->scopes([
        'email', 'user_birthday'
    ])->redirect();
    

    【讨论】:

    • Call to undefined method SocialiteProviders\Etsy\Provider::fields()。我相信,->scope() 的用途也不是我想要的。
    • 在这种情况下,范围是声明您希望从 oAuth 中返回的文件..
    • socialiteproviders.netlify.com/providers/etsy.html,最后有$clientId = "secret"; $clientSecret = "secret"; $redirectUrl = "http://yourdomain.com/api/redirect"; $additionalProviderConfig = ['site' => 'meta.stackoverflow.com']; $config = new \SocialiteProviders\Manager\Config($clientId, $clientSecret, $redirectUrl, $additionalProviderConfig); return Socialite::with('Etsy')->setConfig($config)->redirect();这是你要找的吗?
    【解决方案2】:

    SocialProviders 包不允许在 Server.php 方法上进行自定义配置。

    目前唯一的方法是覆盖供应商文件本身:

    1)SocialiteProviders\Etsy\Server 或任何提供商的服务器文件中复制方法

    2) 将方法粘贴到新的App\Overrides\Etsy\Server 文件中,并添加您的查询参数:

    namespace App\Overrides\Etsy;
    use SocialiteProviders\Manager\OAuth1\Server as BaseServer;
    
    class Server extends BaseServer
    {
        public function urlTemporaryCredentials()
        {
            return 'https://openapi.etsy.com/v2/oauth/request_token?scope=email_r%20listings_r';
        }
    }
    

    3) 创建一个新的App\Providers\ExampleServiceProvider 文件并添加Server 类的别名:

    namespace App\Providers;
    use Illuminate\Support\ServiceProvider;
    
    class ExampleServiceProvider extends ServiceProvider
    {
        public function register()
        {
            $loader = \Illuminate\Foundation\AliasLoader::getInstance();
            $loader->alias('SocialiteProviders\Etsy\Server', 'App\Overrides\Etsy\Server');
        }
    }
    

    4)config/app.php 注册您的新服务提供商:

    'providers' => [
    
        //
    
        App\Providers\ExampleServiceProvider::class,
    ]
    

    【讨论】:

      猜你喜欢
      • 2014-07-10
      • 1970-01-01
      • 2017-07-23
      • 2020-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-24
      • 1970-01-01
      相关资源
      最近更新 更多