Rails - request token for magento api REST
I have a rails app that needs to communicate with magento being on the same server as my rails app. I made all posts in SOAP, but it is very slow and I want to change everything to REST.
I have consumer key
and consumer secret
which I will send to magento to request a token.
DOCS: oauth github , magento oauth
@consumer = OAuth::Consumer.new("44a41ac2e67b", "89578e79570738d", { request_token_path: '/oauth/initiate', access_token_path: '/oauth/token' ,site: "http://shop.myproject.com" })
=> #<OAuth::Consumer:0x000000093955e0 @key="44a1edf5861edf37c", @secret="f87a0e4bfb7663fb78d", @options={:signature_method=>"HMAC-SHA1", :request_token_path=>"/oauth/initiate", :authorize_path=>"/oauth/authorize", :access_token_path=>"/oauth/token", :proxy=>nil, :scheme=>:header, :http_method=>:post, :oauth_version=>"1.0", :site=>"http://shop.myproject.com"}>
@request_token = @consumer.get_request_token
=> Got good response and request_token is good
From magento docs:
User Authorization
The second step is to request user authorization. After receiving the Request Token
from Magento, the application provides an authorization page to the user. The only
required parameter for this step is the Request Token (oauth_token value) received from
the previous step. The endpoint is followed by an oauth_token parameter with the value
set to the oauth_token value.
@access_token = @request_token.get_access_token
OAuth::Unauthorized: 400 Bad Request
Basically I'm on a page where the user exchange request token to access the token allows or enters the username and password. But since I have to do everything in the background, what is the step before I ask for an access token. I cannot find in the documentation how to do this and I have no php background.
Please ask me for any other details in the comments section.
source to share
Fix this by adding an authorized consumer path. Bad guide below
1) Create user
@consumer = OAuth::Consumer.new("44a41ac2e67b", "89578e79570738d", {
request_token_path: '/oauth/initiate',
access_token_path: '/oauth/token',
authorize_path: '/admin/oauth_authorize',
site: "http://shop.myproject.com"
})
2) Get the request token
@request_token = @consumer.get_request_token
3) Get authorization url
@authorized_url = @request_token.authorize_url
4) Click on it, enter credentials, click authorize
5) Extract oauth_verifier from url
6) Get an access token
@access_token = @request_token.get_access_token(oauth_verifier: OAUTH_VERIFIER)
Since the access token will not change, I serialized it using YAML.dump and saved it.
source to share