8

So the official documentation is very terse (bad!), particularly for someone who hasn't worked with REST/Oauth in the past. I'm using Magento 1.8.1 CE.

Can somebody provide a simple PHP example of how to use Magento's REST API?

Using their example code (http://www.magentocommerce.com/api/rest/introduction.html) I get two errors:

Message: Undefined index: state
Message: Use of undefined constant OAUTH_AUTH_TYPE_URI - assumed 'OAUTH_AUTH_TYPE_URI'

Thus far I've done:

  • Install php oauth extension
  • Create at least one product in Magento
  • Configure resource permissions for Admin REST user for retrieving all product data for Admin
  • Create a Consumer

Any help is greatly appreciated.

EDIT: I narrowed down the problem to (surprise, surprise!) OAuth not working properly. I gave up and changed the Guest roles and attributes to allow all. Then, I tested things to see if they were working by navigating to:

http://yourdomain/api/rest/products
http://yourdomain/api/rest/products/1/images

Everything is indeed working. If/when I figure out how to properly deal with authentication, I'll post the answer.

7ochem
7,61516 gold badges54 silver badges82 bronze badges
asked Jun 30, 2014 at 5:15
2
  • You can use the examples listed in here for authentication. magentocommerce.com/api/rest/…. (scroll down until you see code) There is one for guest/customer and one for admin login. They are basically the same, only the url called is different. They worked nicely for me. Commented Jun 30, 2014 at 6:50
  • I'll have a look. I have guest working now, so I'm messing up authentication. Thanks for the tip. Also, did you have to create a oauth_admin.php file yourself? (sorry if this is explained in the linked article, but I figured I'd ask you since you have it working (:-). Commented Jun 30, 2014 at 6:53

1 Answer 1

2

So @user2636834 did you figure this one out already?

I remember when I was setting up a test scenario that I got the sames error(s). Seemed that I didn't have Oauth installed :O. So in short I did (running Ubuntu and following these instructions, also running into this error):

pecl install oauth

Then add extension=oauth.so to php.ini's "Dynamic Extensions" section. Then restart Apache

I tried the examples from the Magento site and got an error from the API:

"Invalid auth/bad request"

I had to add a 'Accept' => '*/*' header to the $oauthClient->fetch() call. After that it worked all just fine.

Hope you can get things running if you hadn't already.

answered Feb 14, 2015 at 19:56
1
  • I posted a similar problem here--> magento.stackexchange.com/questions/63611/…. I tried adding 'Accept' => '*/*' to $oauthClient->fetch() and still get a 403 Forbidden response. Someone else said they solved the 403 after installing SSL. Is that really necessary? (just to be clear, I have Oauth installed and I'm only getting a 403 error - no other errors) Commented Apr 15, 2015 at 12:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.