@@ -55,6 +55,75 @@ protected function initialize()
55
55
$ this ->apiRequestParameters [$ this ->accessTokenName ] = $ accessToken ;
56
56
}
57
57
58
+ /**
59
+ * {@inheritdoc}
60
+ */
61
+ protected function validateAccessTokenExchange ($ response )
62
+ {
63
+ $ collection = parent ::validateAccessTokenExchange ($ response );
64
+
65
+ if (!$ collection ->exists ('expires_in ' )) {
66
+ // Instagram tokens always expire in an hour, but this is implicit not explicit
67
+
68
+ $ expires_in = 60 * 60 ;
69
+
70
+ $ expires_at = time () + $ expires_in ;
71
+
72
+ $ this ->storeData ('expires_in ' , $ expires_in );
73
+ $ this ->storeData ('expires_at ' , $ expires_at );
74
+ }
75
+
76
+ return $ collection ;
77
+ }
78
+
79
+ /**
80
+ * {@inheritdoc}
81
+ */
82
+ public function maintainToken ()
83
+ {
84
+ if (!$ this ->isConnected ()) {
85
+ return ;
86
+ }
87
+
88
+ // Handle token exchange prior to the standard handler for an API request
89
+ $ exchange_by_expiry_days = $ this ->config ->get ('exchange_by_expiry_days ' ) ?: 45 ;
90
+ if ($ exchange_by_expiry_days !== null ) {
91
+ $ projected_timestamp = time () + 60 * 60 * 24 * $ exchange_by_expiry_days ;
92
+ if (!$ this ->hasAccessTokenExpired () && $ this ->hasAccessTokenExpired ($ projected_timestamp )) {
93
+ $ this ->exchangeAccessToken ();
94
+ }
95
+ }
96
+ }
97
+
98
+ /**
99
+ * Exchange the Access Token with one that expires further in the future.
100
+ *
101
+ * @return string Raw Provider API response
102
+ * @throws \Hybridauth\Exception\HttpClientFailureException
103
+ * @throws \Hybridauth\Exception\HttpRequestFailedException
104
+ * @throws InvalidAccessTokenException
105
+ */
106
+ public function exchangeAccessToken ()
107
+ {
108
+ $ exchangeTokenParameters = [
109
+ 'grant_type ' => 'ig_exchange_token ' ,
110
+ 'client_secret ' => $ this ->clientSecret ,
111
+ 'access_token ' => $ this ->getStoredData ('access_token ' ),
112
+ ];
113
+
114
+ $ response = $ this ->httpClient ->request (
115
+ 'https://graph.instagram.com/access_token ' ,
116
+ 'GET ' ,
117
+ $ exchangeTokenParameters
118
+ );
119
+
120
+ $ this ->validateApiResponse ('Unable to exchange the access token ' );
121
+
122
+ $ this ->validateAccessTokenExchange ($ response );
123
+
124
+ return $ response ;
125
+ }
126
+
58
127
/**
59
128
* {@inheritdoc}
60
129
*/
0 commit comments