Php OAuth签名\u将PesaPal API与Ruby一起使用时出现无效错误
我在用Ruby让PesaPal API为我工作,这真是一场噩梦 我很感激它可能不是最常用的API,但如果这里有任何在线用户有更多使用OAuth和/或PHP的经验,能够提供一双全新的眼睛,我将不胜感激 因此,PesaPal开发者网站位于: 他们没有透露太多关于如何在他们的网站上使用OAuth的线索,而且我对PHP的理解还不够透彻,无法确定我是否正确阅读了他们的文章 下面是我在Ruby中实现这一点的尝试:Php OAuth签名\u将PesaPal API与Ruby一起使用时出现无效错误,php,ruby,oauth,Php,Ruby,Oauth,我在用Ruby让PesaPal API为我工作,这真是一场噩梦 我很感激它可能不是最常用的API,但如果这里有任何在线用户有更多使用OAuth和/或PHP的经验,能够提供一双全新的眼睛,我将不胜感激 因此,PesaPal开发者网站位于: 他们没有透露太多关于如何在他们的网站上使用OAuth的线索,而且我对PHP的理解还不够透彻,无法确定我是否正确阅读了他们的文章 下面是我在Ruby中实现这一点的尝试: require 'oauth' require 'uri' key = '<my sa
require 'oauth'
require 'uri'
key = '<my sandbox key>'
sec = '<my sandbox secret>'
API_DOMAIN = 'https://demo.pesapal.com'
# An XML string of param data to include with our request
RAW_XML = %{<?xml version=\"1.0\" encoding=\"utf-8\"?><PesapalDirectOrderInfo xmlns:xsi=\"http://www.w3.org/2001/XMLSchemainstance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" Amount=\"12.34\" Description=\"Bob Test 1\" Type=\"MERCHANT\" Reference=\"808\" FirstName=\"Bo\" LastName=\"Tester\" Email=\"bodacious@bodacious.com\" xmlns=\"http://www.pesapal.com\" />}
# Escape the XML
@post_xml = URI.escape(RAW_XML)
# Create a new OAuth Consumer
@consumer = OAuth::Consumer.new(key, sec, {
site: API_DOMAIN,
scheme: :query_string
})
# The signed request object
@signed_request = @consumer.create_signed_request('get', "#{API_DOMAIN}/API/PostPesapalDirectOrderV4")
# Join the pesapal_request_data and oauth_callback with '&' for valid URL params
@params = {
oauth_callback: URI.escape('http://localhost:3000'),
pesapal_request_data: @post_xml,
}.map { |k,v| "#{k}=#{v}" }.join('&')
# This is the URL we should redirect to
puts redirect_url = "#{@signed_request.path}&#{@params}"
需要“oauth”
需要“uri”
键=“”
秒=“”
美国石油学会https://demo.pesapal.com'
#请求中包含的参数数据的XML字符串
原始XML=%{}
#转义XML
@post_xml=URI.escape(原始xml)
#创建新的OAuth使用者
@consumer=OAuth::consumer.new(键,秒{
地点:API_域,
scheme::查询字符串
})
#已签名的请求对象
@signed_request=@consumer.create_signed_request('get',“#{API_DOMAIN}/API/postesapaldirectorderv4”)
#使用“&”加入pesapal_请求_数据和oauth_回调,以获得有效的URL参数
@参数={
oauth_回调:URI.escape('http://localhost:3000'),
pesapal_请求_数据:@post_xml,
}.map{k,v}“{k}={v}}.join('&'))
#这是我们应该重定向到的URL
puts redirect_url=“#{@signed_request.path}{@params}”
当我尝试访问此代码返回的URL时,API返回:问题:签名无效|建议:>
谁能想到我做错了什么
谢谢我也遇到了同样的问题,我通过创建一个手动签名url的方法解决了这个问题。创建您自己的方法以获得如下oauth\u nonce
def nonce
Array.new( 5 ) { rand(256) }.pack('C*').unpack('H*').first
end
签名方法如下所示:
def signature
key = percent_encode( @consumer_secret ) + '&' + percent_encode( @token_secret )
digest = OpenSSL::Digest::Digest.new( 'sha1' )
hmac = OpenSSL::HMAC.digest( digest, key, @base_str )
Base64.encode64( hmac ).chomp.gsub( /\n/, '' )
end
这是@base_str实例变量:
@base_str = [@req_method,
percent_encode( req_url ),
percent_encode( query_string )
].join( '&' )
这是查询字符串方法:
def query_string
pairs = []
@params.sort.each { | key, val |
pairs.push( "#{ percent_encode( key ) }=#{ percent_encode( val.to_s ) }" )
}
pairs.join '&'
end
然后,您可以使用上面的签名方法来获取oauth_签名参数,以便在生成的url中使用该参数在此处查看pesapal RubyGem。。。它可以为您处理所有这些内容。我知道OP对解决Ruby域的解决方案感兴趣,但我觉得可能需要注意的是,我在PHP上遇到了类似的问题,解决方案是确保XML post数据结构中没有空白 错误:
<?xml version="1.0" encoding="utf-8"?>
<PesapalDirectOrderInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
Amount="200"
Currency="KES"
Description="Computer Accessory"
Type="MERCHANT"
Reference="dd19f9ede4db6f0a13b7053111f02825"
FirstName="Stack"
LastName="Overflow"
Email="so@stackoverflow.com"
PhoneNumber=""
xmlns="http://www.pesapal.com" >
<lineitems>
<lineitem uniqueid="29"
particulars="Keyboard"
quantity="1"
unitcost="200"
subtotal="200.00" >
</lineitem>
</lineitems>
</PesapalDirectOrderInfo>
<?xml version="1.0" encoding="utf-8"?><PesapalDirectOrderInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Amount="200" Currency="KES" Description="Computer Accessory" Type="MERCHANT" Reference="dd19f9ede4db6f0a13b7053111f02825" FirstName="Stack" LastName="Overflow" Email="so@stackoverflow.com" PhoneNumber="" xmlns="http://www.pesapal.com" ><lineitems><lineitem uniqueid="29" particulars="Keyboard" quantity="1" unitcost="200" subtotal="200.00" ></lineitem></lineitems></PesapalDirectOrderInfo>
正确:
<?xml version="1.0" encoding="utf-8"?>
<PesapalDirectOrderInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
Amount="200"
Currency="KES"
Description="Computer Accessory"
Type="MERCHANT"
Reference="dd19f9ede4db6f0a13b7053111f02825"
FirstName="Stack"
LastName="Overflow"
Email="so@stackoverflow.com"
PhoneNumber=""
xmlns="http://www.pesapal.com" >
<lineitems>
<lineitem uniqueid="29"
particulars="Keyboard"
quantity="1"
unitcost="200"
subtotal="200.00" >
</lineitem>
</lineitems>
</PesapalDirectOrderInfo>
<?xml version="1.0" encoding="utf-8"?><PesapalDirectOrderInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Amount="200" Currency="KES" Description="Computer Accessory" Type="MERCHANT" Reference="dd19f9ede4db6f0a13b7053111f02825" FirstName="Stack" LastName="Overflow" Email="so@stackoverflow.com" PhoneNumber="" xmlns="http://www.pesapal.com" ><lineitems><lineitem uniqueid="29" particulars="Keyboard" quantity="1" unitcost="200" subtotal="200.00" ></lineitem></lineitems></PesapalDirectOrderInfo>
Thank:)当我们在做这个项目的时候,这真是太遗憾了