Witam.
Mam kod jak poniżej. Kiedy wprowadzę w zmienną refreshtoken ręcznie np: 'refreshtoken' => 'abc' wszystko jest ok. Ale jeśli 'refreshtoken' => $plik wywala błąd:
Kod
[Fatal error: Constant expression contains invalid operations in /webapi/oauth.php on line 10
Plik zawiera zwykły ciąg znaków.
Dlaczego i jak to poprawić?
Kod skryptu:
<?php
/**
* @author AlleREST.pl
* @link <a href="http://allerest.pl/logowanie-do-allegro-rest-api/" target="_blank">http://allerest.pl/logowanie-do-allegro-rest-api/</a>
*/
echo 'Twój Refresh Token: ' . $plik . ''; class AllegroOAuth2Client {
protected $providerSettings = [
'ClientId' => 'xxx',
'ClientSecret' => 'aaa',
'ApiKey' => 'bbb',
'RedirectUri' => 'ccc',
'AuthorizationUri' => 'https://allegro.pl/auth/oauth/authorize',
'TokenUri' => 'https://allegro.pl/auth/oauth/token',
'refreshtoken' => $plik
];
protected $headers = [
'Content-Type: application/x-www-form-urlencoded'
];
public function __construct
(array $customSettings = []) { $this->providerSettings = array_merge($this->providerSettings, $customSettings); $this->headers[] = 'Authorization: Basic '. base64_encode($this->providerSettings['ClientId'] . ':' . $this->providerSettings['ClientSecret']); }
public function tokenRequest($code) {
$curl = curl_init($this->providerSettings['TokenUri']);
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query([
'grant_type' => 'refresh_token',
'refresh_token' => $plik,
'redirect_uri' => $this->providerSettings['RedirectUri'],
'api_key' => $this->providerSettings['ApiKey']
]));
$result = ($result = curl_exec($curl)) === false ? false : json_decode($result);
if ($result === false) {
throw new Exception('Unrecognized error');
} else if (!empty($result->error)) { throw new Exception($result->error . ' - ' . $result->error_description);
} else {
return $result;
}
}
public function getAuthorizationUri() {
return $this->providerSettings['AuthorizationUri'] . '?' . http_build_query([
'response_type' => 'code',
'client_id' => $this->providerSettings['ClientId'],
'api_key' => $this->providerSettings['ApiKey'],
'redirect_uri' => $this->providerSettings['RedirectUri']
]);
}
}
// Indywidualne parametry można podać do konstruktora w postaci tablicy, lub bezpośrednio w definicji klasy.
$auth = new AllegroOAuth2Client();
// Dobrze byłoby sprawdzić od kogo przychodzi żądanie
if (!empty($_GET['code'])) { try
{
$result = $auth->tokenRequest($_GET['code']);
echo 'Twój Access Token: ' . $result->access_token . ''; echo '<br>Twój Refresh Token: ' . $result->refresh_token . ''; echo '<br>Twój expires_in: ' . $result->expires_in . '';
file_put_contents('.verkey', $result->refresh_token);
}
catch(Exception $e)
{
}
} else {
echo ' Logowanie: <a href="'.$auth->getAuthorizationUri().'">Zaloguj do Allegro</a>'; }
?>