PHPackages                             globalvisionmedia/oauth2-harvest - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. globalvisionmedia/oauth2-harvest

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

globalvisionmedia/oauth2-harvest
================================

Harvest OAuth 2.0 Client Provider for The PHP League OAuth2-Client - updates Nile Suan's provider for Harvest API v2

1.2(3y ago)015MITPHPPHP &gt;=5.5.0

Since Sep 12Pushed 3y ago1 watchersCompare

[ Source](https://github.com/globalvisionmedia/oauth2-harvest)[ Packagist](https://packagist.org/packages/globalvisionmedia/oauth2-harvest)[ RSS](/packages/globalvisionmedia-oauth2-harvest/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (1)Versions (4)Used By (0)

Harvest Provider for OAuth 2.0 Client - For Harvest API V2
==========================================================

[](#harvest-provider-for-oauth-20-client---for-harvest-api-v2)

Harvest OAuth 2.0 support for the PHP League's OAuth 2.0 Client With thanks to Nile Suan for the original harvest provider. This package updates it for V2 of the Harvest API

Installation
============

[](#installation)

```
composer require globalvisionmedia/oauth2-harvest

```

Obtaining a Harvest access key
==============================

[](#obtaining-a-harvest-access-key)

1. To get a key you will need to creare an OAuth2 application in the Harvest Developer area: ()
2. Click the Create new Auth2 application button to create a key
3. Select a name for yoru application
4. The redirect URL must be exactly the same (including the http:// or https://) as the redirectUri below and is the URL of your application. Access - select I need access to one account Product - select I want access to Harvest.
5. Note down the Client ID and Client Secret
6. You need to know your Account ID on Harvest. This is not shown when you create an OAuth2 app, but if you create a temporary "Personal access token" you are shown your Account ID. Note it and then you can delete the temporary personal access token.

Usage
=====

[](#usage)

Usage is the same as The League's OAuth client, using \\GlobalVisionMedia\\OAuth2\\MYOBClient\\Provider\\MYOB as the provider, except for the following:

1. Harvest requires you to set a User Agent in Guzzle (see Example, below)
2. You need to supply an Account ID to the Harvest API (see above)
3. Harvest's APIs are throttled - the documented limit is 15 calls per 100 seconds (The simplest option is to limit to 6 per second) - see example below

Instantiation
=============

[](#instantiation)

```
$provider = new \GlobalVisionMedia\OAuth2\HarvestClient\Provider\Harvest([
    'clientId'                => 'yourId',                // The Client ID assigned to you by Harvest
    'clientSecret'            => 'yourSecret',            // The Client Secret assigned to you by Harvest
    'redirectUri'             => 'yourRedirectUri',       // The Redirect URL you specified for your app on Harvest
    'accountId'               => 'yourAccountId,          // See point 6 above
    'userAgent'               => 'My User Agent'          // This can be any string but ideally should identify your application
]);

```

Tip (also applies to other providers)
=====================================

[](#tip-also-applies-to-other-providers)

```
When you instantiate your provider, you can also pass a second parameter containing a collaborator for your httpClient.
Doing that means you can define your own Guzzle client and do things such as:

  1. Setting Guzzle into debug mode, or
  2. Adding a rate limiter mildeware (composer require spatie/guzzle-rate-limiter-middleware)

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Spatie\GuzzleRateLimiterMiddleware\RateLimiterMiddleware;

define('CALLBACK_URI','https://xxx.yyy/zzzzzzzz.php');
define('HARVEST_CLIENT_ID','xxxxxxxxxxxxxxxxxxxxxxxx');
define('HARVEST_CLIENT_SECRET','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
define('HARVEST_USER_AGENT','My Harvest Integration');
define('HARVEST_ACCOUNT_ID','xxxxxx');

// Add a rate limiter
$stack=HandlerStack::create();
$stack->push(RateLimiterMiddleware::perSecond(6));
$options=['debug' => $debug, 'exceptions' => false, 'handler' => $stack];
$httpClient = new Client($options);

$this->provider = \GlobalVisionMedia\OAuth2\HarvestClient\Provider\Harvest([
    'redirectUri'       => CALLBACK_URI,
    'clientId'          => HARVEST_CLIENT_ID,
    'clientSecret'      => HARVEST_CLIENT_SECRET,
    'accountId'         => HARVEST_ACCOUNT_ID,
    'userAgent'         => HARVEST_USER_AGENT
  ],
  ['httpClient'         => $httpClient]);

```

Sample application
==================

[](#sample-application)

```
