ralphmorris/laravel-instagram-feed

为 Laravel 应用添加 OAuth Instagram API 集成

dev-master 2019-02-19 17:29 UTC

This package is auto-updated.

Last update: 2024-09-20 06:26:56 UTC


README

添加了与 Instagram API 的 OAuth 集成。允许用户将他们的 Instagram 账户连接到 Laravel 应用并显示他们的动态。

安装

您可以通过 composer 安装此包

composer require ralphmorris/laravel-instagram-feed

安装后,使用以下命令发布迁移和配置文件

php artisan vendor:publish --provider="RalphMorris\LaravelInstagramFeed\LaravelInstagramFeedServiceProvider"

然后运行

php artisan migrate

配置

在您的 .env 文件中输入您的 Instagram 客户端 API 的环境变量。您可以通过访问 https://www.instagram.com/developer/clients/manage/ 并按照步骤注册新的客户端来获取您的 API 密钥。

instagram_client_id=your-client-id
instagram_client_secret=your-client-secret

缓存

默认情况下,该包会缓存 Instagram 个人资料动态一段时间。这可以在发布的配置文件中控制。

覆盖使用的模型

您还可以创建自己的模型,如果需要向该类添加自己的方法,可以扩展默认的 RalphMorris\LaravelInstagramFeed\Models\InstagramProfile 模型。然后,您可以通过更新配置文件并指定自己的模型命名空间来告诉包使用它,而不是默认模型。

用法

将 HasInstagramTrait 特性添加到您的模型中,该模型是 Instagram 动态的所有者。例如,User 模型。

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use RalphMorris\LaravelInstagramFeed\Traits\HasInstagramTrait;

class User extends Authenticatable
{
    use HasInstagramTrait;
}

请参见以下控制器以查看发送用户到 Instagram 授权屏幕、接收回调然后检索用户 Instagram 访问令牌的基本流程。

这需要定义两个路由

Route::get('/instagram-connect', 'InstagramController@connect')->name('instagram.connect');
Route::get('/instagram/callback', 'InstagramController@callback')->name('instagram.callback');
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use RalphMorris\LaravelInstagramFeed\Instagram;

class InstagramController extends Controller
{
	private $instagram;

    public function __construct(Instagram $instagram)
    {
        $this->instagram = $instagram;
    }

    /**
     * Redirect the user to the Instagram auth page
     * 
     * @return Redirect
     */
    public function connect()
    {
        return redirect($this->instagram->getAuthUrl());
    }

    /**
     * Handles Instagram redirect callback on success/error after the user has confirmed/declined
     * the application to access their account.
     * 
     * @param  Request $request 
     * @return Redirect
     */
    public function callback(Request $request)
    {
        if ($request->error) {
            // handle the error from Instagram in your application
        }

        $data = $this->instagram->retrieveAccessToken($request->code);

        auth()->user()->storeInstagramProfile($data);

        return redirect()->route('home');
    }

}

在您将访问令牌存储在 InstagramProfile 模型中之后,您可以通过在 InstagramProfile 实例上调用 getFeed() 来获取动态。

例如

$feed = auth()->user()->instagram->getFeed();

API 错误

有时在获取动态的最新版本时抛出异常。这通常是因为访问令牌已失效,可能是由于在 Instagram 侧删除了应用程序的授权权限。当发生这些异常时,它们会在数据库中被标记为已发生错误,并且如果需要可以存储错误消息以供审查。

变更日志

请参见 CHANGELOG 以获取有关最近更改的更多信息。

贡献

请参见 CONTRIBUTING 以获取详细信息。

安全

如果您发现任何安全相关的问题,请通过电子邮件 ralph@bubblehubsolutions.co.uk 而不是使用问题跟踪器。

致谢

许可

MIT 许可证 (MIT)。请参见 许可文件 以获取更多信息。

Laravel 包模板

此包是使用 Laravel 包模板 生成的。