Skip to content

Commit 78a63e2

Browse files
committed
WIP
1 parent d5056e0 commit 78a63e2

File tree

4 files changed

+317
-56
lines changed

4 files changed

+317
-56
lines changed

src/Authenticator/InstagramAuthenticator.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace CodebarAg\LaravelInstagram\Authenticator;
44

55
use DateTimeImmutable;
6+
use Illuminate\Support\Carbon;
67
use Saloon\Contracts\OAuthAuthenticator;
78
use Saloon\Http\PendingRequest;
89

@@ -52,10 +53,12 @@ public function getAccessToken(): string
5253

5354
/**
5455
* Get the refresh token
56+
*
57+
* @throws \Exception
5558
*/
5659
public function getRefreshToken(): ?string
5760
{
58-
return $this->refreshToken;
61+
throw new \Exception('Instagram does not provide refresh tokens. use getAccessToken() instead.');
5962
}
6063

6164
/**
@@ -71,7 +74,9 @@ public function getExpiresAt(): ?DateTimeImmutable
7174
*/
7275
public function isRefreshable(): bool
7376
{
74-
return isset($this->refreshToken);
77+
Carbon::createFromTimestamp($this->getExpiresAt()->getTimestamp())->diffInDays();
78+
79+
return now()->subHours(24)->gt($this->getExpiresAt());
7580
}
7681

7782
/**

src/Connectors/InstagramConnector.php

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,11 @@
33
namespace CodebarAg\LaravelInstagram\Connectors;
44

55
use CodebarAg\LaravelInstagram\Authenticator\InstagramAuthenticator;
6-
use CodebarAg\LaravelInstagram\Requests\Authentication\GetAccessTokenRequest;
7-
use CodebarAg\LaravelInstagram\Requests\Authentication\GetShortLivedAccessTokenRequest;
6+
use CodebarAg\LaravelInstagram\Traits\AuthorizationCodeGrant;
87
use DateTimeImmutable;
98
use Saloon\Contracts\OAuthAuthenticator;
10-
use Saloon\Exceptions\InvalidStateException;
11-
use Saloon\Exceptions\OAuthConfigValidationException;
129
use Saloon\Helpers\OAuth2\OAuthConfig;
1310
use Saloon\Http\Connector;
14-
use Saloon\Http\Request;
15-
use Saloon\Http\Response;
16-
use Saloon\Traits\OAuth2\AuthorizationCodeGrant;
1711

1812
class InstagramConnector extends Connector
1913
{
@@ -61,51 +55,4 @@ protected function defaultOauthConfig(): OAuthConfig
6155
->setTokenEndpoint('https://api.instagram.com/oauth/access_token')
6256
->setUserEndpoint('/me');
6357
}
64-
65-
/**
66-
* Get the short lived access token.
67-
*
68-
* @template TRequest of \Saloon\Http\Request
69-
*
70-
* @param callable(TRequest): (void)|null $requestModifier
71-
*
72-
* @throws \Saloon\Exceptions\InvalidStateException
73-
* @throws OAuthConfigValidationException
74-
*/
75-
public function getShortLivedAccessToken(string $code, ?string $state = null, ?string $expectedState = null, bool $returnResponse = false, ?callable $requestModifier = null): OAuthAuthenticator|Response
76-
{
77-
$this->oauthConfig()->validate();
78-
79-
if (! empty($state) && ! empty($expectedState) && $state !== $expectedState) {
80-
throw new InvalidStateException;
81-
}
82-
83-
$request = $this->resolveShortLivedAccessTokenRequest($code, $this->oauthConfig());
84-
85-
$request = $this->oauthConfig()->invokeRequestModifier($request);
86-
87-
if (is_callable($requestModifier)) {
88-
$requestModifier($request);
89-
}
90-
91-
$response = $this->send($request);
92-
93-
if ($returnResponse === true) {
94-
return $response;
95-
}
96-
97-
$response->throw();
98-
99-
return $this->createOAuthAuthenticatorFromResponse($response);
100-
}
101-
102-
protected function resolveAccessTokenRequest(string $code, OAuthConfig $oauthConfig): Request
103-
{
104-
return new GetAccessTokenRequest($code, $oauthConfig);
105-
}
106-
107-
protected function resolveShortLivedAccessTokenRequest(string $code, OAuthConfig $oauthConfig): Request
108-
{
109-
return new GetShortLivedAccessTokenRequest($code, $oauthConfig);
110-
}
11158
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodebarAg\LaravelInstagram\Requests\Authentication;
6+
7+
use Saloon\Enums\Method;
8+
use Saloon\Http\Request;
9+
use Saloon\Traits\Plugins\AcceptsJson;
10+
11+
class GetRefreshAccessTokenRequest extends Request
12+
{
13+
use AcceptsJson;
14+
15+
/**
16+
* Define the method that the request will use.
17+
*/
18+
protected Method $method = Method::GET;
19+
20+
/**
21+
* Define the endpoint for the request.
22+
*/
23+
public function resolveEndpoint(): string
24+
{
25+
return 'https://graph.instagram.com/refresh_access_token';
26+
}
27+
28+
/**
29+
* Requires the authorization code and OAuth 2 config.
30+
*/
31+
public function __construct(protected string $code) {}
32+
33+
/**
34+
* Register the default data.
35+
*
36+
* @return array{
37+
* grant_type: string,
38+
* access_token: string,
39+
* client_secret: string,
40+
* }
41+
*/
42+
public function defaultQuery(): array
43+
{
44+
return [
45+
'grant_type' => 'ig_exchange_token',
46+
'access_token' => $this->code,
47+
];
48+
}
49+
}

0 commit comments

Comments
 (0)