Issue trying to create device token via api

I created a device using the GUI. I would like to create the device’s token using the API. I get “401 Unauthorized”. Below is the code:

$device = 'device1'; // the device id
$user = 'sazaki'; // your user id
$url = "{$user}/devices/{$device}/tokens";
$data = array( 'token_name' => 'DoorAccess' );
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode( $data ),
'header'=> "Content-Type: application/json;charset=UTF-8\r\n"
. "Accept: application/json\r\n" )
,'ssl'=>array( 'verify_peer'=>false, 'verify_peer_name'=>false, ) );
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
$response = json_decode( $result );

I get the following error:
PHP Warning: file_get_contents( failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized

Any advice or help is appreciated.


This field is not my strongest, but I think that you are making the request without autorization, I mean, the platform has no idea who is trying to create the device’s token, you need to make the request with an authorized token (an authorized token to to create device’s tokens), so the platform can process your request.

Hope this helps.