【问题标题】:Dynamic Laravel Socialite Configurations动态 Laravel 社交名流配置
【发布时间】:2017-02-21 06:43:40
【问题描述】:

我需要动态配置我的提供程序。

$config = [
    'client_id' = 'xxxxxxx',
    'client_token' = 'xxxxxxx',
    'redirect' = 'http://example.com/'
];
return Socialite::with($provider)->setConfig($config)->redirect();

可惜没有setConfig函数。

我需要设置provider、client_id、client_secret和动态重定向

有什么想法吗?

谢谢!

【问题讨论】:

标签: php laravel oauth oauth-2.0 laravel-socialite


【解决方案1】:

您可以使用 Socialite buildProvider 方法,例如:

$config = [
    'client_id'    = 'xxxxxxx',
    'client_token' = 'xxxxxxx',
    'redirect'     = 'http://example.com/'
];

return Socialite::buildProvider(\Laravel\Socialite\Two\FacebookProvider::class, $config);

\Laravel\Socialite\Two\FacebookProvider::class 将与https://github.com/laravel/socialite/tree/2.0/src 中的任一文件夹 One/Two 中提供的服务交换(如果不同)

【讨论】:

  • 还有一个,我不明白如何更改 \Laravel\Socialite\Two\FacebookProvider::class 服务,例如,如果我添加了额外的 instagram 提供程序...你能解释一下吗?
  • 您可以为 Instagram 提供商使用以下软件包 :) socialiteproviders.github.io/providers/instagram
  • 我明白了,它有效。当我设置 \Laravel\Socialite\One\TwitterProvider::class 时,我遇到了新问题
  • 对不起,我明白了,它有效。我遇到了新问题,当我设置 \Laravel\Socialite\One\TwitterProvider::class - 它返回错误“类型错误:参数 2 传递给 Laravel\Socialite\One\AbstractProvider::__construct() 必须是League\OAuth1\Client\Server\Server,给定字符串,在第 92 行的 /home/vagrant/Code/my-project/vendor/laravel/socialite/src/SocialiteManager.php 和 \Laravel\Socialite\One\BitbucketProvider 中调用::class同样的错误
  • 这在调用回调时不起作用,它给出未经授权的401错误。
【解决方案2】:

我使用以下服务提供者来自动为每个为空的提供者填写redirect

可以对其进行修改以即时更新您的配置。我想这完全取决于你想要做什么。

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class SocialServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        collect(config('services'))
            ->only(config('social.providers'))
            ->reject(function($config) {
                return array_get($config, 'redirect', false);
            })
            ->each(function($config, $key) {
                $url = url("login/{$key}/callback", [], true);

                config(["services.{$key}.redirect" => $url]);
            });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-20
    • 2017-10-07
    • 2021-02-10
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    • 2018-05-11
    相关资源
    最近更新 更多