Skip to content

Commit

Permalink
Merge pull request #11 from codebar-ag/fix-refresh-token
Browse files Browse the repository at this point in the history
Refresh Access Token
  • Loading branch information
StanBarrows authored Feb 20, 2025
2 parents d5056e0 + 79b7e29 commit e138789
Show file tree
Hide file tree
Showing 5 changed files with 317 additions and 58 deletions.
9 changes: 7 additions & 2 deletions src/Authenticator/InstagramAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CodebarAg\LaravelInstagram\Authenticator;

use DateTimeImmutable;
use Illuminate\Support\Carbon;
use Saloon\Contracts\OAuthAuthenticator;
use Saloon\Http\PendingRequest;

Expand Down Expand Up @@ -52,10 +53,12 @@ public function getAccessToken(): string

/**
* Get the refresh token
*
* @throws \Exception
*/
public function getRefreshToken(): ?string
{
return $this->refreshToken;
throw new \Exception('Instagram does not provide refresh tokens. use getAccessToken() instead.');
}

/**
Expand All @@ -71,7 +74,9 @@ public function getExpiresAt(): ?DateTimeImmutable
*/
public function isRefreshable(): bool
{
return isset($this->refreshToken);
$created = Carbon::createFromTimestamp($this->getExpiresAt()->getTimestamp())->subDays(60);

return now()->diffInHours($created) > 24;
}

/**
Expand Down
55 changes: 1 addition & 54 deletions src/Connectors/InstagramConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,11 @@
namespace CodebarAg\LaravelInstagram\Connectors;

use CodebarAg\LaravelInstagram\Authenticator\InstagramAuthenticator;
use CodebarAg\LaravelInstagram\Requests\Authentication\GetAccessTokenRequest;
use CodebarAg\LaravelInstagram\Requests\Authentication\GetShortLivedAccessTokenRequest;
use CodebarAg\LaravelInstagram\Traits\AuthorizationCodeGrant;
use DateTimeImmutable;
use Saloon\Contracts\OAuthAuthenticator;
use Saloon\Exceptions\InvalidStateException;
use Saloon\Exceptions\OAuthConfigValidationException;
use Saloon\Helpers\OAuth2\OAuthConfig;
use Saloon\Http\Connector;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\Traits\OAuth2\AuthorizationCodeGrant;

class InstagramConnector extends Connector
{
Expand Down Expand Up @@ -61,51 +55,4 @@ protected function defaultOauthConfig(): OAuthConfig
->setTokenEndpoint('https://api.instagram.com/oauth/access_token')
->setUserEndpoint('/me');
}

/**
* Get the short lived access token.
*
* @template TRequest of \Saloon\Http\Request
*
* @param callable(TRequest): (void)|null $requestModifier
*
* @throws \Saloon\Exceptions\InvalidStateException
* @throws OAuthConfigValidationException
*/
public function getShortLivedAccessToken(string $code, ?string $state = null, ?string $expectedState = null, bool $returnResponse = false, ?callable $requestModifier = null): OAuthAuthenticator|Response
{
$this->oauthConfig()->validate();

if (! empty($state) && ! empty($expectedState) && $state !== $expectedState) {
throw new InvalidStateException;
}

$request = $this->resolveShortLivedAccessTokenRequest($code, $this->oauthConfig());

$request = $this->oauthConfig()->invokeRequestModifier($request);

if (is_callable($requestModifier)) {
$requestModifier($request);
}

$response = $this->send($request);

if ($returnResponse === true) {
return $response;
}

$response->throw();

return $this->createOAuthAuthenticatorFromResponse($response);
}

protected function resolveAccessTokenRequest(string $code, OAuthConfig $oauthConfig): Request
{
return new GetAccessTokenRequest($code, $oauthConfig);
}

protected function resolveShortLivedAccessTokenRequest(string $code, OAuthConfig $oauthConfig): Request
{
return new GetShortLivedAccessTokenRequest($code, $oauthConfig);
}
}
48 changes: 48 additions & 0 deletions src/Requests/Authentication/GetRefreshAccessTokenRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace CodebarAg\LaravelInstagram\Requests\Authentication;

use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Traits\Plugins\AcceptsJson;

class GetRefreshAccessTokenRequest extends Request
{
use AcceptsJson;

/**
* Define the method that the request will use.
*/
protected Method $method = Method::GET;

/**
* Define the endpoint for the request.
*/
public function resolveEndpoint(): string
{
return 'https://graph.instagram.com/refresh_access_token';
}

/**
* Requires the authorization code and OAuth 2 config.
*/
public function __construct(protected string $code) {}

/**
* Register the default data.
*
* @return array{
* grant_type: string,
* access_token: string,
* }
*/
public function defaultQuery(): array
{
return [
'grant_type' => 'ig_refresh_token',
'access_token' => $this->code,
];
}
}
Loading

0 comments on commit e138789

Please sign in to comment.