|
@@ -99,10 +99,11 @@ class TwitterService implements SmmPlatformInterface
|
|
|
'status' => true,
|
|
|
'data' => [
|
|
|
'accessToken' => $auth['access_token'],
|
|
|
- 'backupField1' => json_encode(['refresh_token'=>$refresh_token]),
|
|
|
+ 'backupField1' => '',
|
|
|
'userId' => $userId,
|
|
|
'userName' => $username,
|
|
|
'accessToken_expiresAt' => $expiresAt,
|
|
|
+ 'refresh_token' => $refresh_token,
|
|
|
]
|
|
|
];
|
|
|
} else {
|
|
@@ -381,4 +382,64 @@ class TwitterService implements SmmPlatformInterface
|
|
|
return ['status'=>false,'message' => "Upload failed: ".$e->getMessage()];
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public function refreshAccessToken($refreshToken)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $tokenUrl = 'https://api.twitter.com/2/oauth2/token';
|
|
|
+ $params = [
|
|
|
+ 'refresh_token' => $refreshToken,
|
|
|
+ 'grant_type' => 'refresh_token',
|
|
|
+ 'client_id' => $this->clientId,
|
|
|
+ ];
|
|
|
+
|
|
|
+ $ch = curl_init();
|
|
|
+ curl_setopt($ch, CURLOPT_URL, $tokenUrl);
|
|
|
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
+ curl_setopt($ch, CURLOPT_POST, true);
|
|
|
+ curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
|
|
|
+ curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
|
+ 'Authorization: Basic ' . base64_encode($this->clientId . ':' . $this->clientSecret),
|
|
|
+ 'Content-Type: application/x-www-form-urlencoded'
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $response = curl_exec($ch);
|
|
|
+ $error = curl_error($ch);
|
|
|
+ $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
+ curl_close($ch);
|
|
|
+
|
|
|
+ if ($error) {
|
|
|
+ throw new \Exception("cURL Error: " . $error);
|
|
|
+ }
|
|
|
+
|
|
|
+ $authData = json_decode($response, true);
|
|
|
+ if (!$authData || isset($authData['error'])) {
|
|
|
+ throw new \Exception("Token refresh failed: " . ($authData['error_description'] ?? 'Unknown error'));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!isset($authData['access_token'])) {
|
|
|
+ throw new \Exception("Access token not found in response");
|
|
|
+ }
|
|
|
+
|
|
|
+ $newAccessToken = $authData['access_token'];
|
|
|
+ $newRefreshToken = $authData['refresh_token'] ?? $refreshToken; // 使用新的refresh_token,若未返回则保留原值
|
|
|
+ $expiresIn = $authData['expires_in'] ?? 0;
|
|
|
+ $expiresAt = Carbon::now()->addSeconds($expiresIn)->format('Y-m-d H:i:s');
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'status' => true,
|
|
|
+ 'data' => [
|
|
|
+ 'access_token' => $newAccessToken,
|
|
|
+ 'refresh_token' => $newRefreshToken,
|
|
|
+ 'expires_at' => $expiresAt,
|
|
|
+ ],
|
|
|
+ ];
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error("Twitter刷新Token失败: " . $e->getMessage());
|
|
|
+ return [
|
|
|
+ 'status' => false,
|
|
|
+ 'data' => $e->getMessage(),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|