Ive just read the manual(source:http://www-10.lotus.com/ldd/lcwiki.nsf/dx/Updating_a_status_message_ic301) of Lotus Connection API on updating a status message however I did not find
a sample script on how to update user status message?
I have made a basic Ruby scripts. See below:
url = "https://w3-connections.ibm.com/profiles/atom/mv/theboard/entry/status.do?userid=#{username}"
auth = 'Basic ' + Base64.encode64( "#{username}:#{password}" ).chomp
message = '<entry xmlns="http://www.w3.org/2005/Atom">
<title type="text">Hi!</title>
<category term="entry" scheme="http://www.ibm.com/xmlns/prod/sn/type" />
<category term="status" scheme="http://www.ibm.com/xmlns/prod/sn/message-type" />
<content type="text">Morning! Have a nice day ahead!</content>
</entry>'
resource = RestClient::Resource.new(url, { :headers => { 'Authorization' => auth } } )
response = resource.put message, :content_type => 'application/atom+xml'
puts response.inspect
I'm using RestClient(rest-client (1.6.7)) in Ruby for HTTP Authentication.
However, it didnt work as I expected. The error says "...400 Bad Request (RestClient::BadRequest)"
Is there something I'm missing?
Any help/ideas from you guys would greatly be appreciated. Thanks!
Thanks for the help and suggestion guys. After tinkering an hour, I've successfully made it. Heres the updated code that works!
class IbmConnections
def initialize(username, password)
#username = username
#password = password
end
def post_status_message
require 'rest_client'
atom = "
<entry xmlns='http://www.w3.org/2005/Atom'>
<title type='text'>Hi</title>
<category term='entry' scheme='http://www.ibm.com/xmlns/prod/sn/type' />
<category term='status' scheme='http://www.ibm.com/xmlns/prod/sn/message-type' />
<content type='text'>Morning! Have a nice day ahead!</content>
</entry>"
begin
url = "https://w3-connections.ibm.com/profiles/atom/mv/theboard/entry/status.do"
resource = authenticate url, #username, #password
response = resource.put atom, :content_type => 'application/atom+xml'
if response.empty?
return {:success => 'true', :message => "Successfully update your status!", :data => atom}
else
return {:success => 'false', :message => "Error occurred while posting to Connections! <br /> Please contact the administrator.", :data => atom}
end
rescue => error
logger.debug "Error: IbmConnections.post_it_connections(2)"
logger.debug error.inspect
return {:success => 'false', :message => "Error occurred while posting to Connections! <br /> Please contact the administrator.", :data => error.inspect}
end
end
def authenticate url, username, password
auth = 'Basic ' + Base64.strict_encode64("#{username}:#{password}")
RestClient::Resource.new(url, { :headers => { 'Authorization' => auth } } )
end
end
Related
I am using savon gem for accessing abc financial web services. I am getting error message user not authorize. I am using this code for accessing response
#wsdl="https://webservice.abcfinancial.com/wsdl/Prospect.wsdl"
#basic_auth=["user","pass"]
#headers={"Authorization" => "Basic"}
#client = Savon.client do |globals|
globals.wsdl #wsdl
globals.basic_auth #basic_auth
globals.headers #headers
end
#message = {
:clubNumber=> 'Club 0233',
:firstName=> 'abc',
:lastName=> 'def',
:gender=> "male"
}
response = #client.call(:insert_prospect, message: #message)
and I am getting an error message user not authorize. I searched it but didn't find any solution. Please help me.
They are using JAX-WS.
According to this article http://examples.javacodegeeks.com/enterprise-java/jws/application-authentication-with-jax-ws/ (and google) we should use login-password in http headers.
I don't have an account on the http://abcfinancial.com so I can't test the code.
Try it:
require 'savon'
user = 'myLogin'
password = 'myPassword'
client = Savon.client(
wsdl: 'https://webservice.abcfinancial.com/wsdl/Prospect.wsdl',
headers: {username: user,
password: password},
pretty_print_xml: true,
log: true
)
message = {
:arg0 => {
clubNumber: 'Club 0233',
:personal => {
:firstName => 'myName',
:lastName => 'myLastName',
:gender => 'male'
}
}
}
response = client.call(:insert_prospect, message: message)
Read this good article http://predic8.com/wsdl-reading.htm and turn on debugging (pretty_print_xml: true, log: true).
I want to use Yahoo Fantasy sport API in my web application, For that I am using OAuth for Yahoo login. I have consumer key and secret key and i passed the keys successfully, When I run the following code. It redirects to Yahoo login, It asks permission for accessing the user's credentials. If i give AGREE the page Redirects to https://api.login.yahoo.com/oauth/v2/request_auth and It shows the Verifying code. If i press the close button in verification code page, it's not callback to my URL.
#ts=Time.now.to_i
#callback_url = "http://localhost:3000/callback"
#nonce = SecureRandom.hex()
consumer = OAuth::Consumer.new("my consumerkey","secret key",
{ :site => 'https://api.login.yahoo.com',
:http_method => :post,
:scheme => :header,
:oauth_nonce => #nonce,
:request_token_path => '/oauth/v2/get_request_token',
:authorize_path => '/oauth/v2/request_auth',
:access_token_path => '/oauth/v2/get_token',
:oauth_callback => "http://localhost:3000/callback",
:oauth_timestamp => Time.now.to_i,
:oauth_signature_method => "HMAC-SHA-1",
:oauth_version => "1.0",
:oauth_callback_confirmed => true,
})
request_token = consumer.get_request_token
session[:request_token]=request_token
redirect_to request_token.authorize_url
access_token=request_token.get_access_token
access = ActiveSupport::JSON.decode(access_token.to_json)
if !(access.present?)
#response = "Response failed"
else
#response = access
end
Can you please tell me What changes to be made to get the callback for to get access_token.
I think you got confused while getting callback. change your code as follows, You will surely get access token to make Yahoo API call.
##access_token = nil
##request_token = nil
def get_request_token
##consumer = OAuth::Consumer.new('consumer key',
'secret key',
{
:site => 'https://api.login.yahoo.com',
:scheme => :query_string,
:http_method => :get,
:request_token_path => '/oauth/v2/get_request_token',
:access_token_path => '/oauth/v2/get_token',
:authorize_path => '/oauth/v2/request_auth'
})
##request_token = ##consumer.get_request_token( { :oauth_callback => 'http://localhost:3000/callback' } )
session[:request_token]=##request_token
redirect_to ##request_token.authorize_url
#redirect_to ##request_token.authorize_url( { :oauth_callback => 'http://localhost:3000/success' } )
end
def callback
request_token = ActiveSupport::JSON.decode(##request_token.to_json)
if !(request_token.present?)
$request_token_value = "Response failed"
else
$request_token_value = request_token
end
# access_token = ##request_token.get_access_token({:oauth_verifier=>params[:oauth_verifier],:oauth_token=>params[:oauth_token]})
##access_token = ##request_token.get_access_token(:oauth_verifier=>params[:oauth_verifier])
access_json = ActiveSupport::JSON.decode(##access_token.to_json)
puts "****************************"
puts $access_json
puts "****************************"
end
I was working on an app in which I need to work with ipn, but it seems it does not work well.
I am trying to get the notification in success action and have specified the correct url in paypal sandbox.
def success
topup = current_user.topups.last
logger.debug "topup -------->"
logger.debug topup.amount.to_i
# raise request
details = EXPRESS_GATEWAY.details_for(topup.express_token)
logger.debug "details ------->"
logger.debug details.payer_id
# raise params[:payer_id]
response = EXPRESS_GATEWAY.purchase(topup.price_in_cents,{
:ip => request.remote_ip,
:token => topup.express_token,
:payer_id => details.payer_id
})
logger.debug "Response starts here --------->"
logger.debug response
if response.success?
amount = topup.amount.to_i
current_user.credits = current_user.credits.to_i + amount
current_user.save!
flash[:success] = "Thank you for the top up"
# #account_history = current_user.account_histories.build
# #account_history.update_attribute(:type => "Top Up", :details => "", :amount => amount, :user_id => current_user.id)
redirect_to current_user
notify = Paypal::Notification.new request.raw_post
logger.info "Notifying --------->"
logger.info notify
logger.info notify.status
logger.info "Notifying 2 --------->"
logger.info notify.acknowledge
logger.debug notify.status
if notify.acknowledge
logger.debug "Notifying --------->"
logger.debug notify.mc_gross
logger.debug notify.txn_id
end
else
redirect_to root_url
end
end
notify.acknowledge does not return anything (it is blank)
After banging my head for quite some time I was able to solve the issue. Posting my solution just in case anyone is stuck in the similar situation :-
One important thing is that IPN sends post data to the IPN listener so make sure you have a route defined for this.
I also had a before_filter :authenticate_user! , because of which I was getting html code 401. Had to skip before filter for notify action.
def pay_with_account
topup = Topup.find(params[:id])
response = EXPRESS_GATEWAY.setup_purchase(topup.price_in_cents,{
:ip => request.remote_ip,
:currency_code => 'GBP',
:return_url => topups_success_url(topup),
:notify_url => topups_notify_url(topup),
:cancel_return_url => topups_cancel_url(topup),
# :allow_guest_checkout => true,
:items => [{:name => "Topup", :quantity => 1,:description => "Top up my account", :amount => topup.price_in_cents}]
})
topup.update_attribute :express_token, response.token
redirect_to EXPRESS_GATEWAY.redirect_url_for(response.token)
end
This is the return url :-
def success
topup = current_user.topups.last
details = EXPRESS_GATEWAY.details_for(topup.express_token)
response = EXPRESS_GATEWAY.purchase(topup.price_in_cents,{
:ip => request.remote_ip,
:currency_code => 'GBP',
:token => topup.express_token,
:payer_id => details.payer_id
})
if response.success?
amount = topup.amount.to_i
current_user.credits = current_user.credits.to_i + amount
current_user.save!
redirect_to user_payments_path(current_user)
flash[:success] = "Your transaction was successfully completed"
else
flash[:error] = "Your transaction could not be compelted"
redirect_to user_payments_path(current_user)
end
end
This is the notify url :-
def notify
notify = Paypal::Notification.new(request.raw_post)
if notify.acknowledge
#account_history = topup.user.account_histories.build
#account_history.update_attributes(:payment_type => "Top up",:status => notify.status, :amount => notify.gross)
if params[:payer_status] == "verified"
#account_history.update_attributes(:details => "Pay Pal #{#account_history.id}")
elsif params[:payer_status] == "unverified"
#account_history.update_attributes(:details => "Credit Card #{#account_history.id}")
end
#account_history.save
end
render :nothing => true
end
routes :-
get "topups/pay_with_account"
get "topups/pay_without_account"
get "topups/success"
get "topups/cancel"
get "topups/notify"
post "topups/notify
I'm trying to create simple PayPal Pay API operation, when I run this code from console it gave me response, that payment is created.
Now, when I'm tring to run it from my controller it gives me
Authentication+failed.+API+credentials+are+incorrect.
Here is my controller :
def pay
require 'httpclient'
require 'xmlsimple'
clnt = HTTPClient.new
credentials = {
'USER' => 'payer_1342623102_biz_api1.gmail.com',
'PWD' => '1342623141',
'SIGNATURE' => 'Ay2zwWYEoiRoHTTVv365EK8U1lNzAESedJw09MPnj0SEIENMKd6jvnKL '
}
header = {"X-PAYPAL-SECURITY-USERID" => "payer_1342623102_biz_api1.gmail.com",
"X-PAYPAL-SECURITY-PASSWORD" => "1342623141",
"X-PAYPAL-SECURITY-SIGNATURE" => "Ay2zwWYEoiRoHTTVv365EK8U1lNzAESedJw09MPnj0SEIENMKd6jvnKL ",
"X-PAYPAL-REQUEST-DATA-FORMAT" => "NV",
"X-PAYPAL-RESPONSE-DATA-FORMAT" => "XML",
"X-PAYPAL-APPLICATION-ID" => "APP-80W284485P519543T"
}
data = {"actionType" => "PAY",
"receiverList.receiver(0).email"=> "denmed_1342605975_biz#gmail.com",
"receiverList.receiver(0).amount" => "10",
"currencyCode" => "USD",
"cancelUrl" => "http://127.0.0.1:3000",
"returnUrl" => "http://127.0.0.1:3000",
"requestEnvelope.errorLanguage" => "en_US"}
uri = "https://svcs.sandbox.paypal.com/AdaptivePayments/Pay"
res = clnt.post(uri, data, header)
#xml = XmlSimple.xml_in(res.content)
payKey = #xml["payKey"].to_s()
payKey = payKey.tr("[]", "")
payKey = payKey[1..20]
redirect_to "https://svcs.sandbox.paypal.com/AdaptivePayments/Pay?cmd=_ap-payment&paykey=#{payKey}"
end
Is everything ok ? Can anyone suggest reason my request fails ?
One good man found my error. I redirect user to the wrong url.
This line:
redirect_to "https://svcs.sandbox.paypal.com/AdaptivePayments/Pay?cmd=_ap-payment&paykey=#{payKey}"
Should be:
redirect_to "https://sandbox.paypal.com/webscr?cmd=_ap-payment&paykey=#{paykey}"
I got the same error: I realized I forgot to include
sandbox_email_address: xxx#example.com
in my yml file
I'm using the Last.fm API and I'm trying to get a lot of info about a certain user and returning it in xml. So, here's the call in my view:
<%= form_tag fetch_user_path, :remote => true, :'data-type' => 'xml', :id => 'search' do %>
<%= text_field_tag :q %>
<% end %>
So, as you can see, it's expecting XML, and I'm correctly handling the callback using jQuery. Then, in my controller:
# fetch_controller.rb
def user
username = params[:q].gsub(' ','+')
get_info_url = "http://ws.audioscrobbler.com/2.0/?method=user.getinfo&user=#{username}&api_key=#{API_KEY}"
get_friends_url = "http://ws.audioscrobbler.com/2.0/?method=user.getfriends&user=#{username}&api_key=#{API_KEY}"
respond_to do |format|
format.xml {
begin
#info = Nokogiri::XML(open(get_info_url))
#friends = Nokogiri::XML(open(get_friends_url))
rescue Exception => e
if e.message == '400 Bad Request'
render xml: { :error => 'User not found.' }, :status => 400
else
render xml: { :error => 'Connection to Last.fm failed.' }, :status => 500
end
else
# Here, I want to render #info + #friends!
render xml: #info
end
}
end
This way, I'm correctly returning the xml returned by get_info_url. However, I want to join that xml to the xml returned by get_friends_url. How would I go about that?
Following Ben Miller's answer, I'm getting a parserror on my callback. I think it's got to to do with both xml files containing xml version. And maybe the combined file does not? I'm seeing that the xml files are being concatenated, here's how they look using console.log:
Error: Invalid XML: <?xml version="1.0"?>
<Combined>
<UserInfo>
<?xml version="1.0" encoding="utf-8"??>
<lfm status="ok">
<user>
# lots of info about the user
</user>
</lfm>
</UserInfo>
<FriendInfo>
<?xml version="1.0" encoding="utf-8"??>
<lfm status="ok">
<friends for="user" page="1" perpage="50" totalpages="2" total="96">
# lots of info about the user's friends
</friends>
</lfm>
</FriendInfo>
</Combined>
One option it so convert the two XML object to a string and concat them, then wrap in a new root node.
Or you could do it with Nokogiri builder
def user
username = params[:q].gsub(' ','+')
get_info_url = "http://ws.audioscrobbler.com/2.0/?method=user.getinfo&user=#{username}&api_key=#{API_KEY}"
get_friends_url = "http://ws.audioscrobbler.com/2.0/?method=user.getfriends&user=#{username}&api_key=#{API_KEY}"
respond_to do |format|
format.xml {
begin
info_xml = Nokogiri::XML(open(get_info_url))
friends_xml = Nokogiri::XML(open(get_friends_url))
builder = Nokogiri::XML::Builder.new do |xml_out|
xml_out.Combined {
xml_out.UserInfo {
node = info_xml.at_xpath("//user")
xml_out << node.to_xml.to_str
}
xml_out.FriendInfo {
node = friends_xml.at_xpath("//friends")
xml_out << node.to_xml.to_str
}
}
end
rescue Exception => e
if e.message == '400 Bad Request'
render xml: { :error => 'User not found.' }, :status => 400
else
render xml: { :error => 'Connection to Last.fm failed.' }, :status => 500
end
else
render xml: builder.to_xml
end
}
end
end