简体   繁体   中英

Magento2 how to get token by rest api?

$adminUrl='http://localhost/magento/index.php/rest/V1/integration/admin/token';

$data = array("username" => "myname", "password" => "mypassword");                                                                    
$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);
print_r($token);

I have try above code , but can not get a token , and return an error message ({"message":"Server cannot understand Content-Type HTTP header media type application/x-www-form-urlencoded"}, anyone know how to do,please help,Thanks.

//Authentication rest API magento2.Please change url accordingly your url
$adminUrl='http://127.0.0.1/magento2/index.php/rest/V1/integration/admin/token';
$ch = curl_init();
$data = array("username" => "wsuser", "password" => "password123");                                                                    
$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); 

Above peace of code will resolve your issue follow the block:-

http://blog.i13websolution.com/magento-2-rest-api-example/

You should try this one :

curl -X POST "https://magento.host/index.php/rest/V1/integration/customer/token" \
     -H "Content-Type:application/json" \
     -d '{"username":"user_example", "password":"123123q"}'

for more detail http://devdocs.magento.com/guides/v2.0/get-started/authentication/gs-authentication-token.html

The following code worked for me.

//Magento admin user data
$userData = array("username" => "adminuser", "password" => "adminpassword");
$ch = curl_init("http://yourdomain.com/rest/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Length: " . strlen(json_encode($userData))));

$token = curl_exec($ch);

Reference: http://inchoo.net/magento-2/magento-2-api/

Please change your $adminUrl

from:

http://localhost/magento/index.php/rest/V1/integration/admin/token

to:

http://localhost/rest/V1/integration/admin/token

That will work fine.

You can use this to get admin token.


$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://127.0.0.1/www.yourstoreurl.com/rest/V1/integration/admin/token",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\r\n  \"username\": \"admin\",\r\n  \"password\": \"pwd@123\"\r\n}",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "content-type: application/json",
    "postman-token: e3deae08-0436-af2d-0449-450720bb7086"
  ),
));

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

curl_close($curl);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM