Skip to content

Commit

Permalink
setAccessToken
Browse files Browse the repository at this point in the history
  • Loading branch information
kawax committed May 13, 2018
1 parent 2b2becb commit e0360ee
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
20 changes: 20 additions & 0 deletions src/GoogleSheets/Sheets.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Revolution\Google\Sheets;

use Google_Service_Sheets;
use PulkitJalan\Google\Facades\Google;

use Illuminate\Support\Traits\Macroable;

Expand Down Expand Up @@ -50,6 +51,25 @@ public function getService(): Google_Service_Sheets
return $this->service;
}

/**
* set access_token and set new service
*
* @param string|array $token
*
* @return $this
*/
public function setAccessToken($token)
{
Google::setAccessToken($token);

if (isset($token['refresh_token']) and Google::isAccessTokenExpired()) {
Google::fetchAccessTokenWithRefreshToken();
}

return $this->setService(Google::make('sheets'))
->setDriveService(Google::make('drive'));
}

/**
* @param string $spreadsheetId
*
Expand Down
9 changes: 9 additions & 0 deletions src/GoogleSheets/SheetsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ public function setService($service);
*/
public function getService(): Google_Service_Sheets;

/**
* set access_token and set new service
*
* @param string|array $token
*
* @return $this
*/
public function setAccessToken($token);

/**
* @param string $spreadsheetId
*
Expand Down
11 changes: 1 addition & 10 deletions src/GoogleSheets/Traits/GoogleSheets.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use Revolution\Google\Sheets\SheetsInterface;

use PulkitJalan\Google\Facades\Google;

/**
* use at User model
*/
Expand All @@ -19,14 +17,7 @@ public function sheets()
{
$token = $this->sheetsAccessToken();

Google::setAccessToken($token);

if (isset($token['refresh_token']) and Google::isAccessTokenExpired()) {
Google::fetchAccessTokenWithRefreshToken();
}

return app(SheetsInterface::class)->setService(Google::make('sheets'))
->setDriveService(Google::make('drive'));
return app(SheetsInterface::class)->setAccessToken($token);
}

/**
Expand Down

0 comments on commit e0360ee

Please sign in to comment.