Unable to access rest api : http://localhost/magento2/index.php/rest/V1/products The Next CEO...
Shortening a title without changing its meaning
About implicitly convert type 'int' to 'char', why it is different between `s[i] += s[j]` and `s[i] = s[i]+s[j] `
Why did the Drakh emissary look so blurred in S04:E11 "Lines of Communication"?
How does a dynamic QR code work?
Why did Batya get tzaraat?
Is the offspring between a demon and a celestial possible? If so what is it called and is it in a book somewhere?
Are British MPs missing the point, with these 'Indicative Votes'?
Could you use a laser beam as a modulated carrier wave for radio signal?
How do I secure a TV wall mount?
"Eavesdropping" vs "Listen in on"
Why was Sir Cadogan fired?
Can you teleport closer to a creature you are Frightened of?
Is it OK to decorate a log book cover?
Can Sri Krishna be called 'a person'?
What did the word "leisure" mean in late 18th Century usage?
Masking layers by a vector polygon layer in QGIS
How can I separate the number from the unit in argument?
What is the difference between 'contrib' and 'non-free' packages repositories?
Calculate the Mean mean of two numbers
Why did early computer designers eschew integers?
What steps are necessary to read a Modern SSD in Medieval Europe?
Read/write a pipe-delimited file line by line with some simple text manipulation
Traveling with my 5 year old daughter (as the father) without the mother from Germany to Mexico
It it possible to avoid kiwi.com's automatic online check-in and instead do it manually by yourself?
Unable to access rest api : http://localhost/magento2/index.php/rest/V1/products
The Next CEO of Stack OverflowMagento 2 API not authenticatingUnable to extend Orders Rest Api of MagentoAuthorization failed during category creation using REST APISorting issue while getting product from category ID using REST ApiDoubts regarding creating Magento Mobile AppMagento REST API - Unable to Login using buyer username and passwordLinnworks and Consumer is not authorizedUnable to get salesorders and Customers From Magento 2.2 using REST APImagento 2 Rest APi How to use default methodsError Consumer is not authorized to access %resources
I am new to Magento. Please help me understand how to fetch the products.
I believe there are some changes to be made in .htaccess file present in the root folder for this url: http://localhost/magento2/index.php/rest/V1/products to return the values.
This the response that I am getting :
Consumer is not authorized to access %resources
Magento_Catalog::products
.
Can someone please help me here.
magento-2.1 rest
bumped to the homepage by Community♦ 9 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I am new to Magento. Please help me understand how to fetch the products.
I believe there are some changes to be made in .htaccess file present in the root folder for this url: http://localhost/magento2/index.php/rest/V1/products to return the values.
This the response that I am getting :
Consumer is not authorized to access %resources
Magento_Catalog::products
.
Can someone please help me here.
magento-2.1 rest
bumped to the homepage by Community♦ 9 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I am new to Magento. Please help me understand how to fetch the products.
I believe there are some changes to be made in .htaccess file present in the root folder for this url: http://localhost/magento2/index.php/rest/V1/products to return the values.
This the response that I am getting :
Consumer is not authorized to access %resources
Magento_Catalog::products
.
Can someone please help me here.
magento-2.1 rest
I am new to Magento. Please help me understand how to fetch the products.
I believe there are some changes to be made in .htaccess file present in the root folder for this url: http://localhost/magento2/index.php/rest/V1/products to return the values.
This the response that I am getting :
Consumer is not authorized to access %resources
Magento_Catalog::products
.
Can someone please help me here.
magento-2.1 rest
magento-2.1 rest
edited Jun 14 '17 at 13:16
user55339
asked Jun 14 '17 at 12:58
user55339user55339
12
12
bumped to the homepage by Community♦ 9 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 9 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can not access the resources through browser url because of authentication.
this is the example coding below.
for username and password austhentication you need to create in admin side.
<?php
$userData = array("username" => "test", "password" => "mypassword");
$ch = curl_init("http://magento.m2/index.php/rest/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CUsRLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));
$token = curl_exec($ch);
$ch = curl_init("http://magento.m2/index.php/rest/V1/products/1");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
$result = curl_exec($ch);
var_dump($result);
for more info please follow the below links
http://inchoo.net/magento-2/magento-2-api/
http://www.dckap.com/blog/magento-2-webapi/
Magento 2 API not authenticating
add a comment |
It seems to be a problem with the User Role.
To get started with the REST API in Magento 2 using token-based authentication, you will need to create a web service User Role and register that role to a new Magento 2 Admin User.
- Login to the Magento 2 Admin Panel.
- Go to System > User Roles and tap the Add New Role
- Enter the Role Name.
- In Your Password field, enter the current password of your Magento 2
Admin. - Now, on the left side, click Role Resources.
- In the Resource Access, select only those that are required for your
web service. (in your case, products should be selected) - Once done, hit the Save Role
Now, create a new user for the newly created role through these steps:
- Go to System > All Users and hit the Add New User
- Enter the required information including User Name, First and Last
Name, Email, Password, etc. - Now, on the left side, click User Role and select the newly created
Role. - Once done, click the Save User
Now, I will pass the newly created username and password with the API URL in the initial connection and receive the token. This token will be saved in a variable, which will be passed in the header for further calls.
<?php
//API URL for authentication
$apiURL="http://magento-91647-257956.cloudwaysapps.com/index.php/rest/V1/integration/admin/token";
//parameters passing with URL
$data = array("username" => "apiaccess", "password" => "cloudways123");
$data_string = json_encode($data);
$ch = curl_init($apiURL);
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);
//decoding generated token and saving it in a variable
$token= json_decode($token);
?>
You can fetch almost everything using Magento 2 REST API. In this answer, I will be fetching All Modules, but you can check out the List of REST APIs for Magento Open Source and see what you needed.
Now, I will pass the token (fetched earlier) in the header with the API URL to get all the modules installed on Magento 2 store.
<?php
//API URL for authentication
$apiURL="http://magento-91647-257956.cloudwaysapps.com/index.php/rest/V1/integration/admin/token";
//parameters passing with URL
$data = array("username" => "apiaccess", "password" => "cloudways123");
$data_string = json_encode($data);
$ch = curl_init($apiURL);
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);
//decoding generated token and saving it in a variable
$token= json_decode($token);
//******************************************//
//Using above token into header
$headers = array("Authorization: Bearer ".$token);
//API URL to get all Magento 2 modules
$requestUrl='http://magento-91647-257956.cloudwaysapps.com/index.php/rest/V1/modules';
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
//decoding result
$result= json_decode($result);
//printing result
print_r($result);
?>
Once you execute the above code (with relevant information), run the file in the browser and you will see output similar to the following:

If you need further help, check this complete guide: https://www.cloudways.com/blog/magento-2-rest-api/
add a comment |
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f179015%2funable-to-access-rest-api-http-localhost-magento2-index-php-rest-v1-products%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can not access the resources through browser url because of authentication.
this is the example coding below.
for username and password austhentication you need to create in admin side.
<?php
$userData = array("username" => "test", "password" => "mypassword");
$ch = curl_init("http://magento.m2/index.php/rest/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CUsRLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));
$token = curl_exec($ch);
$ch = curl_init("http://magento.m2/index.php/rest/V1/products/1");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
$result = curl_exec($ch);
var_dump($result);
for more info please follow the below links
http://inchoo.net/magento-2/magento-2-api/
http://www.dckap.com/blog/magento-2-webapi/
Magento 2 API not authenticating
add a comment |
You can not access the resources through browser url because of authentication.
this is the example coding below.
for username and password austhentication you need to create in admin side.
<?php
$userData = array("username" => "test", "password" => "mypassword");
$ch = curl_init("http://magento.m2/index.php/rest/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CUsRLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));
$token = curl_exec($ch);
$ch = curl_init("http://magento.m2/index.php/rest/V1/products/1");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
$result = curl_exec($ch);
var_dump($result);
for more info please follow the below links
http://inchoo.net/magento-2/magento-2-api/
http://www.dckap.com/blog/magento-2-webapi/
Magento 2 API not authenticating
add a comment |
You can not access the resources through browser url because of authentication.
this is the example coding below.
for username and password austhentication you need to create in admin side.
<?php
$userData = array("username" => "test", "password" => "mypassword");
$ch = curl_init("http://magento.m2/index.php/rest/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CUsRLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));
$token = curl_exec($ch);
$ch = curl_init("http://magento.m2/index.php/rest/V1/products/1");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
$result = curl_exec($ch);
var_dump($result);
for more info please follow the below links
http://inchoo.net/magento-2/magento-2-api/
http://www.dckap.com/blog/magento-2-webapi/
Magento 2 API not authenticating
You can not access the resources through browser url because of authentication.
this is the example coding below.
for username and password austhentication you need to create in admin side.
<?php
$userData = array("username" => "test", "password" => "mypassword");
$ch = curl_init("http://magento.m2/index.php/rest/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CUsRLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));
$token = curl_exec($ch);
$ch = curl_init("http://magento.m2/index.php/rest/V1/products/1");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
$result = curl_exec($ch);
var_dump($result);
for more info please follow the below links
http://inchoo.net/magento-2/magento-2-api/
http://www.dckap.com/blog/magento-2-webapi/
Magento 2 API not authenticating
edited Jun 15 '17 at 13:27
Manthan Dave
7,96621539
7,96621539
answered Jun 14 '17 at 13:53
JjoJjo
840214
840214
add a comment |
add a comment |
It seems to be a problem with the User Role.
To get started with the REST API in Magento 2 using token-based authentication, you will need to create a web service User Role and register that role to a new Magento 2 Admin User.
- Login to the Magento 2 Admin Panel.
- Go to System > User Roles and tap the Add New Role
- Enter the Role Name.
- In Your Password field, enter the current password of your Magento 2
Admin. - Now, on the left side, click Role Resources.
- In the Resource Access, select only those that are required for your
web service. (in your case, products should be selected) - Once done, hit the Save Role
Now, create a new user for the newly created role through these steps:
- Go to System > All Users and hit the Add New User
- Enter the required information including User Name, First and Last
Name, Email, Password, etc. - Now, on the left side, click User Role and select the newly created
Role. - Once done, click the Save User
Now, I will pass the newly created username and password with the API URL in the initial connection and receive the token. This token will be saved in a variable, which will be passed in the header for further calls.
<?php
//API URL for authentication
$apiURL="http://magento-91647-257956.cloudwaysapps.com/index.php/rest/V1/integration/admin/token";
//parameters passing with URL
$data = array("username" => "apiaccess", "password" => "cloudways123");
$data_string = json_encode($data);
$ch = curl_init($apiURL);
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);
//decoding generated token and saving it in a variable
$token= json_decode($token);
?>
You can fetch almost everything using Magento 2 REST API. In this answer, I will be fetching All Modules, but you can check out the List of REST APIs for Magento Open Source and see what you needed.
Now, I will pass the token (fetched earlier) in the header with the API URL to get all the modules installed on Magento 2 store.
<?php
//API URL for authentication
$apiURL="http://magento-91647-257956.cloudwaysapps.com/index.php/rest/V1/integration/admin/token";
//parameters passing with URL
$data = array("username" => "apiaccess", "password" => "cloudways123");
$data_string = json_encode($data);
$ch = curl_init($apiURL);
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);
//decoding generated token and saving it in a variable
$token= json_decode($token);
//******************************************//
//Using above token into header
$headers = array("Authorization: Bearer ".$token);
//API URL to get all Magento 2 modules
$requestUrl='http://magento-91647-257956.cloudwaysapps.com/index.php/rest/V1/modules';
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
//decoding result
$result= json_decode($result);
//printing result
print_r($result);
?>
Once you execute the above code (with relevant information), run the file in the browser and you will see output similar to the following:

If you need further help, check this complete guide: https://www.cloudways.com/blog/magento-2-rest-api/
add a comment |
It seems to be a problem with the User Role.
To get started with the REST API in Magento 2 using token-based authentication, you will need to create a web service User Role and register that role to a new Magento 2 Admin User.
- Login to the Magento 2 Admin Panel.
- Go to System > User Roles and tap the Add New Role
- Enter the Role Name.
- In Your Password field, enter the current password of your Magento 2
Admin. - Now, on the left side, click Role Resources.
- In the Resource Access, select only those that are required for your
web service. (in your case, products should be selected) - Once done, hit the Save Role
Now, create a new user for the newly created role through these steps:
- Go to System > All Users and hit the Add New User
- Enter the required information including User Name, First and Last
Name, Email, Password, etc. - Now, on the left side, click User Role and select the newly created
Role. - Once done, click the Save User
Now, I will pass the newly created username and password with the API URL in the initial connection and receive the token. This token will be saved in a variable, which will be passed in the header for further calls.
<?php
//API URL for authentication
$apiURL="http://magento-91647-257956.cloudwaysapps.com/index.php/rest/V1/integration/admin/token";
//parameters passing with URL
$data = array("username" => "apiaccess", "password" => "cloudways123");
$data_string = json_encode($data);
$ch = curl_init($apiURL);
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);
//decoding generated token and saving it in a variable
$token= json_decode($token);
?>
You can fetch almost everything using Magento 2 REST API. In this answer, I will be fetching All Modules, but you can check out the List of REST APIs for Magento Open Source and see what you needed.
Now, I will pass the token (fetched earlier) in the header with the API URL to get all the modules installed on Magento 2 store.
<?php
//API URL for authentication
$apiURL="http://magento-91647-257956.cloudwaysapps.com/index.php/rest/V1/integration/admin/token";
//parameters passing with URL
$data = array("username" => "apiaccess", "password" => "cloudways123");
$data_string = json_encode($data);
$ch = curl_init($apiURL);
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);
//decoding generated token and saving it in a variable
$token= json_decode($token);
//******************************************//
//Using above token into header
$headers = array("Authorization: Bearer ".$token);
//API URL to get all Magento 2 modules
$requestUrl='http://magento-91647-257956.cloudwaysapps.com/index.php/rest/V1/modules';
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
//decoding result
$result= json_decode($result);
//printing result
print_r($result);
?>
Once you execute the above code (with relevant information), run the file in the browser and you will see output similar to the following:

If you need further help, check this complete guide: https://www.cloudways.com/blog/magento-2-rest-api/
add a comment |
It seems to be a problem with the User Role.
To get started with the REST API in Magento 2 using token-based authentication, you will need to create a web service User Role and register that role to a new Magento 2 Admin User.
- Login to the Magento 2 Admin Panel.
- Go to System > User Roles and tap the Add New Role
- Enter the Role Name.
- In Your Password field, enter the current password of your Magento 2
Admin. - Now, on the left side, click Role Resources.
- In the Resource Access, select only those that are required for your
web service. (in your case, products should be selected) - Once done, hit the Save Role
Now, create a new user for the newly created role through these steps:
- Go to System > All Users and hit the Add New User
- Enter the required information including User Name, First and Last
Name, Email, Password, etc. - Now, on the left side, click User Role and select the newly created
Role. - Once done, click the Save User
Now, I will pass the newly created username and password with the API URL in the initial connection and receive the token. This token will be saved in a variable, which will be passed in the header for further calls.
<?php
//API URL for authentication
$apiURL="http://magento-91647-257956.cloudwaysapps.com/index.php/rest/V1/integration/admin/token";
//parameters passing with URL
$data = array("username" => "apiaccess", "password" => "cloudways123");
$data_string = json_encode($data);
$ch = curl_init($apiURL);
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);
//decoding generated token and saving it in a variable
$token= json_decode($token);
?>
You can fetch almost everything using Magento 2 REST API. In this answer, I will be fetching All Modules, but you can check out the List of REST APIs for Magento Open Source and see what you needed.
Now, I will pass the token (fetched earlier) in the header with the API URL to get all the modules installed on Magento 2 store.
<?php
//API URL for authentication
$apiURL="http://magento-91647-257956.cloudwaysapps.com/index.php/rest/V1/integration/admin/token";
//parameters passing with URL
$data = array("username" => "apiaccess", "password" => "cloudways123");
$data_string = json_encode($data);
$ch = curl_init($apiURL);
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);
//decoding generated token and saving it in a variable
$token= json_decode($token);
//******************************************//
//Using above token into header
$headers = array("Authorization: Bearer ".$token);
//API URL to get all Magento 2 modules
$requestUrl='http://magento-91647-257956.cloudwaysapps.com/index.php/rest/V1/modules';
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
//decoding result
$result= json_decode($result);
//printing result
print_r($result);
?>
Once you execute the above code (with relevant information), run the file in the browser and you will see output similar to the following:

If you need further help, check this complete guide: https://www.cloudways.com/blog/magento-2-rest-api/
It seems to be a problem with the User Role.
To get started with the REST API in Magento 2 using token-based authentication, you will need to create a web service User Role and register that role to a new Magento 2 Admin User.
- Login to the Magento 2 Admin Panel.
- Go to System > User Roles and tap the Add New Role
- Enter the Role Name.
- In Your Password field, enter the current password of your Magento 2
Admin. - Now, on the left side, click Role Resources.
- In the Resource Access, select only those that are required for your
web service. (in your case, products should be selected) - Once done, hit the Save Role
Now, create a new user for the newly created role through these steps:
- Go to System > All Users and hit the Add New User
- Enter the required information including User Name, First and Last
Name, Email, Password, etc. - Now, on the left side, click User Role and select the newly created
Role. - Once done, click the Save User
Now, I will pass the newly created username and password with the API URL in the initial connection and receive the token. This token will be saved in a variable, which will be passed in the header for further calls.
<?php
//API URL for authentication
$apiURL="http://magento-91647-257956.cloudwaysapps.com/index.php/rest/V1/integration/admin/token";
//parameters passing with URL
$data = array("username" => "apiaccess", "password" => "cloudways123");
$data_string = json_encode($data);
$ch = curl_init($apiURL);
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);
//decoding generated token and saving it in a variable
$token= json_decode($token);
?>
You can fetch almost everything using Magento 2 REST API. In this answer, I will be fetching All Modules, but you can check out the List of REST APIs for Magento Open Source and see what you needed.
Now, I will pass the token (fetched earlier) in the header with the API URL to get all the modules installed on Magento 2 store.
<?php
//API URL for authentication
$apiURL="http://magento-91647-257956.cloudwaysapps.com/index.php/rest/V1/integration/admin/token";
//parameters passing with URL
$data = array("username" => "apiaccess", "password" => "cloudways123");
$data_string = json_encode($data);
$ch = curl_init($apiURL);
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);
//decoding generated token and saving it in a variable
$token= json_decode($token);
//******************************************//
//Using above token into header
$headers = array("Authorization: Bearer ".$token);
//API URL to get all Magento 2 modules
$requestUrl='http://magento-91647-257956.cloudwaysapps.com/index.php/rest/V1/modules';
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
//decoding result
$result= json_decode($result);
//printing result
print_r($result);
?>
Once you execute the above code (with relevant information), run the file in the browser and you will see output similar to the following:

If you need further help, check this complete guide: https://www.cloudways.com/blog/magento-2-rest-api/
answered Nov 22 '18 at 11:58
Fayyaz KhattakFayyaz Khattak
1,5411022
1,5411022
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f179015%2funable-to-access-rest-api-http-localhost-magento2-index-php-rest-v1-products%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
