问题
I am using the latest Directus v8 release. I installed it and used it to design my schema and everything seems to be ok, but when I try to get a temporary token to authenticate a user I am getting a strange error about not being authorized, at the same time that the AUTH endpoint doesn't require previously authorized users (obviously).
- My Directus installation is located at htdocs/slotspanel
- Its URL is http://127.0.0.1/slotspanel
- The user's credentials are double-checked, they are correct
- I am using XAMPP with the latest PHP7
- mod_rewrite is enabled
I am trying to authenticate the user with the following script
<?php
$data = array(
'email' => 'user@mail.com',
'password' => 'UserPasswordHere'
);
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode( $data ),
'header'=> "Content-Type: application/json\r\n" .
"Accept: application/json\r\n"
)
);
$context = stream_context_create( $options );
$result = file_get_contents("http://127.0.0.1/slotspanel/auth/authenticate", false, $context);
$response = json_decode($result);
echo $response;
?>
And I'm getting the following error
Warning: file_get_contents(http://127.0.0.1/slotspanel/auth/authenticate): failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized in C:\xampp_new\htdocs\anotherdomain\index2.php on line 18
Error log
[2020-06-18 13:36:29] api[auth].ERROR: Directus\Exception\UnauthorizedException: Unauthorized request in C:\xampp_new\htdocs\src\helpers\app.php:287
Stack trace:
#0 C:\xampp_new\htdocs\vendor\slim\slim\Slim\App.php(518): Directus\{closure}
#1 C:\xampp_new\htdocs\src\core\Directus\Application\Http\Middleware\CorsMiddleware.php(71): Slim\App->__invoke
#2 [internal function]: Directus\Application\Http\Middleware\CorsMiddleware->__invoke
#3 C:\xampp_new\htdocs\vendor\slim\slim\Slim\DeferredCallable.php(57): call_user_func_array
#4 [internal function]: Slim\DeferredCallable->__invoke
#5 C:\xampp_new\htdocs\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func
#6 C:\xampp_new\htdocs\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(117): Slim\App->Slim\{closure}
#7 C:\xampp_new\htdocs\vendor\slim\slim\Slim\App.php(392): Slim\App->callMiddlewareStack
#8 C:\xampp_new\htdocs\vendor\slim\slim\Slim\App.php(297): Slim\App->process
#9 C:\xampp_new\htdocs\src\core\Directus\Application\Application.php(161): Slim\App->run
#10 C:\xampp_new\htdocs\slotspanel\index.php(5): Directus\Application\Application->run [] []
回答1:
This was asked and subsequently resolved on GitHub: https://github.com/directus/directus/discussions/2757#discussioncomment-27908
The request URL above http://127.0.0.1/slotspanel/auth/authenticate
was missing the project in the path. In this particular case, it was supposed to be http://127.0.0.1/slotspanel/slotspanel/auth/authenticate
来源:https://stackoverflow.com/questions/62448517/unable-to-get-temporary-token-with-directus-v8-api