cancel
Showing results for 
Search instead for 
Did you mean: 

Admin Token not working to create images

Admin Token not working to create images

This is the situation

 

I ask for an authorization admin key to create products and I got it and works just great but when I do the same to create images for those products I alway received an error message telling me 

 

(string) {"message":"Consumer is not authorized to access %resources","parameters":{"resources":"Magento_Catalog::catalog"}}

 So i tried two ways, 

1. Use the same token admin auth. for products in the call to create images, result the message above

2. Ask for a new token and the result is the same

 

Both of them use the same user, and this user has permissions like an administrator

 

2 REPLIES 2

Re: Admin Token not working to create images

There are mainly two reasons for this. 

 

1) Might be given token is expired - kindly generate new admin token with admin username and password and pass to header with bearer accesstoken and then check

 

2) Might be this token user have limited access , this user is restricted with the Magento_Catalog module , so kindly check your ACL from integration for this user and updates its with all the permissions.

 

If its still not work , then post your whole code or post man call for media upload , it will more help to understand the error.

if issue solved,Click Kudos & Accept as Solution

Re: Admin Token not working to create images

Hi Dave

 

Thank you for your help.

About your comments:

Spoiler
I generate a new token each time I create an image(s) for a product. And the user has administrative privileges.

So here is my code

 

This function asks for a new token 

    /*
     * Autenticacion del usuario con Magento 2
     * para obtener el token de entrada a los servicios de la API
     */
    private function firmarUsuario() {
        
        $adminUrl=API_REST_URL.'/integration/admin/token';

        $data = array("username" => API_REST_USER, "password" => API_REST_PASS);                                                                    
        $data_string = json_encode($data); 
        
        $ch = curl_init($adminUrl); 
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
            'Content-Type: application/json',                                                                                
            'Content-Length: ' . strlen($data_string))                                                                       
        );      
        
        $token = curl_exec($ch);
        $token=  json_decode($token);       
        
        return $token;
    }

This function creates the image or images for the product.

As a note: this function is called only when the product is already created

    /*
     * Comunicacion con base de datos para 
     * enviar la o las imegens del producto
     */
     private function crearImagenes($productos) {
            
        $url = API_REST_URL; 
        $conImagen = $productos->imagen;
        
        $peticionNum = 0;
        $listaSKU = '';
        
        if ($conImagen === '1') {
            $urlImagen = $this->obtieneURLImagen($productos->skuCCE);
            $respuesta = true;
        } else {
            $urlImagen = NULL;
            $respuesta = false;
        }                  

        $cont=1;

        foreach ($urlImagen->data AS $imagen) {

            $contentType    = 'image/'.pathinfo($imagen, PATHINFO_EXTENSION);
            
            $xmlImage = DIR_IMAGEN.$productos->skuCCE.'/'.$imagen;
            $xmlImage = $this->curl_get_contents($xmlImage);
            $xmlImage = base64_encode($xmlImage);           

            $dataString = array(
                'entry' => array(
                    'id'        => $productos->idProducto,
                    'mediaType' => 'image',
                    'label'     => $productos->skuCCE,
                    'position'  => 0,
                    'disabled'  => 'false',
                    'extension_attributes'  => array(
                        'video_content' => array( )
                    ),
                    'types'     => array(
                                    'image',
                                    'thumbnail',
                                    'small_image' ),
                    'file'      => $productos->skuCCE,
                    'content'   => array(
                                    'base64_encoded_data'   => $xmlImage,
                                    'type'                  => $contentType,
                                    'name'                  => $imagen )
                )
            );

            $dataDecode = json_encode($dataString);
            
            $url .= "/products/".$productos->skuCCE."/media";

            $curl = curl_init();

            $token = "Authorization: Bearer ".$this->firmarUsuario();
            $headers = array(   $token,
                                "Accept: application/json",
                                "Cache-Control: no-cache",
                                $contentType,
                                "User-Agent: Mozilla/4.0 (compatible)" );                     

            curl_setopt_array($curl, array(
                CURLOPT_URL => $url,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 30,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_POSTFIELDS => $dataString,
                CURLOPT_HTTPHEADER => $headers,
              ));

            $response = curl_exec($curl);
            $responseDecode = json_decode($response);
            $err = curl_error($curl);

            curl_close($curl);

            if ($err) {
              echo "cURL Error #:" . $err;
            } else {
              echo $response;
            }

            $cont++;

            if ($cont >= 8) {
                break;
            }
        }  
        $flagMagento=$productos->bandera;
        $flagMagento[4]='1';
        $strConsulta="UPDATE tblProductos SET bandera='".$flagMagento."' WHERE tblProductos.codProd=".$productos->codProducto;

        $serSQL = new serverBD();
        $serSQL->MySQL();
        $serSQL->execute($strConsulta);            
        
        return $respuesta;
    } 

and thank you again for your help