coopbelvedere/laravel-basecamp-api 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

coopbelvedere/laravel-basecamp-api

最新稳定版本:v1.5.1

Composer 安装命令:

composer require coopbelvedere/laravel-basecamp-api

包简介

API Wrapper for Basecamp3

README 文档

README

Warning

This repo is now readonly and no longer maintained.

An API wrapper for Basecamp 3.

Prerequisites

Create an integration

Visit https://launchpad.37signals.com/integrations and register your app.

Add the credentials in your .env file as:

THIRTYSEVENSIGNALS_CLIENT_ID=your_client_id
THIRTYSEVENSIGNALS_CLIENT_SECRET=your_client_secret_key
THIRTYSEVENSIGNALS_REDIRECT_URI=http://localhost/login/basecamp/callback

And add the config variables to your config/services.php file:

    '37signals' => [
        'client_id' => env('THIRTYSEVENSIGNALS_CLIENT_ID'),
        'client_secret' => env('THIRTYSEVENSIGNALS_CLIENT_SECRET'),
        'redirect' => env('THIRTYSEVENSIGNALS_REDIRECT_URI')
    ],

OAuth2

The Basecamp3 API only supports 3-legged authentication and makes calls on behalf of one user. To receive an access token, you can use Laravel Socialite with the 37signals Socialite Driver. After you save the access token and the desired Basecamp3 account, you can now create an instance of the API wrapper.

composer require socialiteproviders/37signals

Add the socialite service provider to your config/app.php providers array key:

'providers' => [
    ...
    \SocialiteProviders\Manager\ServiceProvider::class,
]

Add the socialite event listener in your app/Providers/EventServiceProvider.php listen property:

protected $listen = [
    ...
    \SocialiteProviders\Manager\SocialiteWasCalled::class => [
        \SocialiteProviders\ThirtySevenSignals\ThirtySevenSignalsExtendSocialite::class.'@handle',
    ],
];

Installation

composer require coopbelvedere/laravel-basecamp-api
php artisan vendor:publish --provider="Belvedere\Basecamp\BasecampServiceProvider"

Add a user-agent to identify your app in your config/basecamp.php file. This is mandatory or any request will return a 400 error.

Usage

Retrieve your basecamp id, base uri (href), token and refresh token and initialize the API wrapper. Here's a basic example on the routes/web.php file to get you started.

Route::get('/login/basecamp', function () {
    return Socialite::driver('37signals')->redirect();
});

Route::get('/login/basecamp/callback', function () {
    $user = Socialite::driver('37signals')->user();

    Basecamp::init([
        'id' => $user->user['accounts'][0]['id'],
        'href' => $user->user['accounts'][0]['href'],
        'token' => $user->token,
        'refresh_token' => $user->refreshToken,
    ]);

    $projects = Basecamp::projects();
    dd($projects->index());
});

NOTE: You shouldn't initialize the API in the callback route, this is only to show you what data to keep from the socialite user. Save this data in the database, and/or the session to keep the connection active.

Caching

The client uses a Laravel Filesystem cache strategy by default. You can override this with your preferred Laravel cache store:

Basecamp::setCache(Cache::store('redis'));

Middlewares (optional)

You can optionally add an array of middlewares to the Guzzle Handler Stack. Here is an example for logging a request:

Basecamp::setMiddlewares([
    \GuzzleHttp\Middleware::log(
        Log::getLogger(),
        new \GuzzleHttp\MessageFormatter('{method} {uri} HTTP/{version} {req_body}')
    )
]);

Event Listener

The Client also comes with a middleware which will refresh an expired access token and automatically retry the intended endpoint. You can listen to the basecamp.refreshed_token event to update the access token in your app. The event returns 2 parameters, your basecamp user id and the new access token.

You can add something like this in your EventServiceProvider.php boot method:

Event::listen('basecamp.refreshed_token', function ($id, $token) {
    $user = \App\User::where('basecamp_id', $id)->first();
    $user->access_token = $token->access_token;
    $user->expires_at = \Carbon\Carbon::now()->addSeconds($token->expires_in);
    $user->save();
});

Pagination

Most collection of resources in Basecamp can be paginated.

// Get projects.
$projects = Basecamp::projects()->index();

// Get total of projects
$projects->total();

// Save the next page link for your next request
$nextPage = $projects->nextPage();

// Call the next page of projects.
$projects = Basecamp::projects()->index($nextPage);

Resources documentation

License

MIT

Copyright (c) 2017-present, Coopérative Belvédère Communication

统计信息

  • 总下载量: 24.23k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 19
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 18
  • Watchers: 5
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-10-22

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固