Authenticate with http headers with savon 2.3.0 - ruby

I had this code in Savon v1:
client = Savon.client("http://www.server.com:9191/soapserver?wsdl")
service = client.request :get_authentication do
client.http.headers["username"] = "myuser"
client.http.headers["password"] = "mypass"
end
After the update to savon v2.3.0, I don't manage to retranslate. It should be something like
client = Savon.client do
wsdl "http://www.shab.ch:9191/soapserver?wsdl
end
service = client.call(:get_authentication, {username: "myuser", password: "mypass"})`
but the line service = client.call(..." does not work. Any idea?

I think what you want to do is:
gem "savon"
require "savon", "~>2.0"
...
client = Savon.client(headers: { username: "user", password: "password"},
wsdl: "http://www.example.com/?wsdl",
log: true,
log_level: :debug,
pretty_print_xml: true
#, and more options here if necessary)
That will inject the key/values pairs into the http headers.

That last code block of yours is missing a " on line #2 and has a ``` too much in the end. It should look like:
client = Savon.client do
wsdl "http://www.shab.ch:9191/soapserver?wsdl"
end
service = client.call(:get_authentication, {username: "myuser", password: "mypass"})
to not trigger any syntax errors.

This works for me after a lot of trial and error:
client = Savon.client( wsdl: "http://www.server.com:9191/soapserver?wsdl", <\br>
headers: {'username' => 'myuser', 'password' => 'mypass'} )
service = client.call(:get_authentication)
So the header injection I do before calling the :get_authentication function.

Related

Connect to a SOAP API with savon and specify Domain

With SoapUI, I am successfully connecting to a SOAP API with details such as:
Initial WSDL: http://11.11.1.11:7303/FOO/BAR/BAZ
Username: foo
Password: bar
Domain: example
But I am not sure how to do this with Savon. I am trying this (not quite understanding if ntlm is the correct setting here):
savon_defaults = {
:wsdl => "http://11.11.1.11:7303/FOO/BAR/BAZ",
:ntlm => ["foo", "bar", "example"],
:log_level => :debug,
:pretty_print_xml => true,
:log => true
}
#client = Savon.client(savon_defaults)
#client.call(:foo, :message => {:bar => "baz"})
The above outputs:
D, [2020-05-18T21:21:14.853929 #7156] DEBUG -- : HTTPI /peer GET request to 11.11.1.11 (httpclient)
Savon::HTTPError: HTTP error (401)
Any ideas?

How to authenticate oauth, Magento 2.0 SOAP in Ruby

Having a problem authenticating to Magento 2 SOAP with oauth credentials.
For a magento 1.9 version this is very straightforward:
client = Savon.client(
wsdl: "https://example.com/api/soap/?wsdl=1",
log: true,
pretty_print_xml: true
)
session_id = client.call(:login, message: { username: "username", apiKey: "key" })body[:login_response][:login_return]
client.call(:call, message:{resource_path: 'catalog_product.list', session_id: session_id}).body
Works as expected. With oauth it gets bit more complicated.
I tried all kind of different things, like:
client = Savon.client(
wsdl: "http://example.com/index.php/soap/default?wsdl_list=1",
soap_header: { 'Authorization:' => "Basic xxxx"},
pretty_print_xml: true
)
client.call(:call, message:{resource_path: 'catalogProductAttributeGroupRepositoryV1'}).body
Hope somebody already made an oauth-magento 2 soap integration and give me some pointers.
You need to create a new integration from admin to get authentication keys. These links will be useful resource:
https://devdocs.magento.com/guides/v2.0/get-started/authentication/gs-authentication-oauth.html
https://gist.github.com/rafaelstz/ecab668b80fece4d9acdb9c5358b3173

Soundcloud - Ruby creating a Playlist getting 422 (#soundcloud-ruby)

I suspect something at Soundcloud has changed because my code has not been altered and worked fine last year.
I see:
Error: HTTP status: 422 Unprocessable Entity, Status Code: 422, playlist_struct:{:title=>"Y11 - REVO - Sop", :description=>"Y11 - REVO - Sop newchoir", :tag_list=>"Sop", :tracks=>"219269586", :format=>"json", :oauth_token=>"..."}
My oauth_token works fine.
I call:
new_playlist = #client.post('/playlists', playlist_struct)
Where #client is defined using https://github.com/soundcloud/soundcloud-ruby as:
#client = SoundCloud.new({
:client_id => clientId,
:client_secret => clientSecret,
:username => email,
:password => password
})
And playlist_struct is per the error message.
Thoughts appreciated!
Regards, M.
Full code:
require 'rubygems'
require 'soundcloud'
require 'pp'
require 'logger'
def login
# http://soundcloud.com/you/apps
clientId = '...'
clientSecret = '...'
email = '...'
password = '...'
# register a new client, which will exchange the username, password for an access_token
# NOTE: the SoundCloud API Docs advise not to use the user credentials flow in a web app.
# In any case, never store the password of a user.
#client = SoundCloud.new({
:client_id => clientId,
:client_secret => clientSecret,
:username => email,
:password => password
})
# print logged in username
puts"h1. Logged in as " + #client.get('/me').username
# updating the users profile description
end
login()
playlist_struct = {
:title => "Hello"
}
new_playlist = #client.post('/playlists', playlist_struct)
#log.info ' OK: '+new_playlist.permalink_url
Looks like the playlist_struct now needs to include
playlist: {
...
}
Around the content.
As the code worked for a couple of years before hand I'd venture this is a silent change to the API.

Unable to authorize to a OAuth 2 legged provider

I am trying to authenticate to a 2 legged OAuth(1.0) system from ruby irb as follows using oauth ruby gem,
Step 1:
require 'oauth'
irb(main):038:0> consumer = OAuth::Consumer.new("key", "secret", :site => "site", :scheme => :query_string)
I got a response for this as,
=> #<OAuth::Consumer:0xba14be2c #key="{got valid key here}", #secret="{got valid secret here}", #options={:signature_method=>"HMAC-SHA1", :request_token_path=>"/oauth/request_token", :authorize_path=>"/oauth/authorize", :access_token_path=>"/oauth/access_token", :proxy=>nil, :scheme=>:query_string, :http_method=>:post, :oauth_version=>"1.0", :site=> "https://example.com/">
Step 2:
irb(main):039:0> access_token = OAuth::AccessToken.new consumer
=> #<OAuth::AccessToken:0xba144b7c #token="", #secret="", #consumer=#<OAuth::Consumer:0xba147430 #key="{got valid key here}", #secret="{got valid secret here}", #options={:signature_method=>"HMAC-SHA1", :request_token_path=>"/oauth/request_token", :authorize_path=>"/oauth/authorize", :access_token_path=>"/oauth/access_token", :proxy=>nil, :scheme=>:query_string, :http_method=>:post, :oauth_version=>"1.0", :site=>"https://example.com/"}>, #params={}>
Please see Token is empty above. I should have got the token here??
Step 3: Then i tried all the paths which i got from above,
irb(main):041:0> access_token.get("/oauth/authorize")
irb(main):041:0> access_token.get("/oauth/request_token")
irb(main):041:0> access_token.get("/oauth/access_token")
But for all the request am getting response as,
=> #<Net::HTTPNotFound 404 Not Found readbody=true>
I dono where i am making mistake, am i doing the request correctly, if not how should i authenticate to the 2legged oauth system in ruby. Or should i ask the service provider to check on their side.
Token field is empty in step 2, that is the problem??.
Can anyone please guide me in this?? Thanks in advance.
maybe this code will help:
require 'oauth'
consumer_key = <your consumer key>
consumer_secret = <your consumer secret>
end_point = "https://www.example.com"
consumer = OAuth::Consumer.new(consumer_key, consumer_secret, {
:site => end_point,
:scheme => :header
})
parameters = "user_id=1"
resp = consumer.request(:post, '/get_user/', nil, {}, parameters, { 'Content-Type' => 'application/x-www-form-urlencoded' })

Rails savon, two wsdl

I'm developing on Rails and I'm using the gem Savon (http://savonrb.com).
What I have to do is to integrate myself with a service that I wsdl has two, one for authentication and the other for various functions.
There are two wsdl:
"https://.../authentication.asmx?wsdl"
"https://.../lists.asmx?wsdl"
What I do first is this:
client = Savon::Client.new do |wsdl, http|
http.auth.ssl.verify_mode = :none
wsdl.document = "https://.../authentication.asmx?wsdl"
end
response = client.request :soap, :login, :body => {:username => "...", :password => "..."}
Once logged in I would need to use the second wsdl to perform my function, but I can not declare another client within authentication lose the first thing I said.
response = client.request :get_list_collection do
soap.endpoint = URI(URI.escape("https://.../lists.asmx?wsdl"))
end
I've tried many but I found no solution.
Any idea how to solve this problem?

Resources