POST http://localhost/oauth/token with proper parameters responds with string "Object of class DateTime could not be converted to string".
I am using [email protected]
The following codeline produces error:
vendor/league/oauth2-server/src/Grant/AbstractGrant.php@issueAccessToken:372
while ($maxGenerationAttempts-- > 0) {
$accessToken->setIdentifier($this->generateUniqueIdentifier());
try {
$this->accessTokenRepository->persistNewAccessToken($accessToken); // <-- THIS LINE
return $accessToken;
} catch (UniqueTokenIdentifierConstraintViolationException $e) {
if ($maxGenerationAttempts === 0) {
throw $e;
}
}
}
Please anybody let me know how to fix it asap.
That line will save the access token to the database, could you make sure that all the fields in the database have the correct type?
Just from the top of my head: probably something like a created_at or updated_at field has a varchar or text type, so eloquent might not be able to parse it correctly.
How can I find implementation of persistNewAccessToken() method?
It's in the AccessTokenRepository Class, but I'm pretty sure it's because the columns of your database are not the right type. See the migration files for the db column types.
Hi!
I have the same issue, using password Grant or conventional redirect authorization method.
I've discovered that the problem, at least in my case, is on a QueryHelper that I'm using for logging database queries.
public static function translate($sql, $bindings)
{
$query = $sql;
for ($j=0; $j < sizeof($bindings); $j++) {
$bindings[$j] = $bindings[$j] == '' ? "''" : $bindings[$j];
if (is_string($bindings[$j])) {
$bindings[$j] = "'" .$bindings[$j]. "'";
}
$query = str_replace_first('?', $bindings[$j], $query);
}
$query = trim(preg_replace("/\r\n|\n/", "", $query));
return $query;
}
I checked laravel log and got this:
/var/www/laravel/app/Helpers/QueryHelper.php(20): str_replace_first('?', Object(DateTime), 'insert intooa...')`
Seems like
str_replace_first('?', $bindings[$j], $query);
can't handle the DateTime object being assigned.
What's weird is that I haven't had this problem in any other query, maybe because of my model's setters and/or protected definitions of dates and stuff?.
Anyway, I guess I'll try to add a instanceOf checker before that line, and use format to transform it to string, maybe that will solve my issue.
(Edit)
I did the check and it worked:
public static function translate($sql, $bindings)
{
$query = $sql;
for ($j=0; $j < sizeof($bindings); $j++) {
$bindings[$j] = $bindings[$j] == '' ? "''" : $bindings[$j];
if (is_string($bindings[$j])) {
$bindings[$j] = "'" .$bindings[$j]. "'";
} elseif ($bindings[$j] instanceof DateTime) {
$bindings[$j] = $bindings[$j]->format('Y-m-d H:i:s');
}
$query = str_replace_first('?', $bindings[$j], $query);
}
$query = trim(preg_replace("/\r\n|\n/", "", $query));
return $query;
}
Now my query log still works and passport can keep generating the tokens.
Hi @Aragami23 can you help me with my case? I have this in my AppServiceProvider for my query loggin:
public function boot()
{
DB::listen(function ($query) {
// $query->sql
// $query->bindings
// $query->time
$q = str_replace(array('?'), array('\'%s\''), $query->sql);
$bindings = array_map(function($value) {
if ($value instanceof DateTime) { // <--------- your approach here
return $value->format('Y-m-d H:i:s');
}
else {
return $value;
}
}, $query->bindings);
$q = vsprintf($q, $bindings);
Log::debug($q);
});
}
but still not working
I've changed this if ($value instanceof DateTime) to if (is_a($value, 'DateTime')) and now is working:
public function boot()
{
if (\App::environment('local')) {
// The environment is local
DB::listen(function ($query) {
// $query->sql
// $query->bindings
// $query->time
$q = str_replace(array('?'), array('\'%s\''), $query->sql);
$bindings = array_map(function($value) {
if (is_a($value, 'DateTime')) {
return $value->format('Y-m-d H:i:s');
}
else {
return $value;
}
}, $query->bindings);
$q = vsprintf($q, $bindings);
Log::debug($q);
});
}
}
@arivasvera
Great! I just saw this, sorry I couldn't help before, by this time your already discovered that you couldn't use instanceof in your case.
I'm glad my comment helped a little.
Take care.
Closing this issue because it's already solved, old or not relevant anymore. Feel free to reply if you're still experiencing this issue.