问题描述
Ruby中是否有任何库可以生成Signature,'X-PAYPAL-AUTHORIZATION'
标头,所以该标头需要代表通过Paypal Permissions API授权我们的帐户持有人进行调用.我已经完成了权限流,并获得了所需的访问令牌tokenSecret.我感觉我生成的签名不正确,因为所有使用生成的"X-PAYPAL-AUTHORIZATION"的呼叫均失败.它们给出以下错误:
对于NVP通话,我得到: You do not have permissions to make this API call
Is there any library in Ruby that generates the Signature, 'X-PAYPAL-AUTHORIZATION'
header that is required to make calls on behalf of the account holder who has authorized us through the paypal Permissions API.I am done with the permissions flow and get the required access token, tokenSecret. I feel I am generating the signature incorrectly as all my calls with the the generated 'X-PAYPAL-AUTHORIZATION' fail. They give the following errors:
For NVP call I get:You do not have permissions to make this API call
对于GetBasicPersonalData调用,我得到: Authentication failed. API credentials are incorrect.
And for the GetBasicPersonalData call I get:Authentication failed. API credentials are incorrect.
有人在Ruby中经历过这个吗?什么是生成签名的最佳方法. Paypal刚刚在Java的Paypal中提供了一些SDK,但没有提供生成签名的算法.
Has anyone gone through this in Ruby? What is best way to generate signature. Paypal has just provided some SDK in Paypal, Java, but not the algorithm to generate signature.
谢谢,
尼罗什(Nilesh)
Thanks,
Nilesh
推荐答案
看看PayPal Permissions宝石.
Take a look at the PayPal Permissions gem.
https://github.com/moshbit/paypal_permissions
特别是lib/paypal_permissions/x_pp_authorization.rb需要'cgi'需要'openssl'需要'base64'
Specifically lib/paypal_permissions/x_pp_authorization.rbrequire 'cgi'require 'openssl'require 'base64'
class Hash
def to_paypal_permissions_query
collect do |key, value|
"#{key}=#{value}"
end.sort * '&'
end
end
module ActiveMerchant #:nodoc:
module Billing #:nodoc:
module XPPAuthorization
public
def x_pp_authorization_header url, api_user_id, api_password, access_token, access_token_verifier
timestamp = Time.now.to_i.to_s
signature = x_pp_authorization_signature url, api_user_id, api_password, timestamp, access_token, access_token_verifier
{ 'X-PAYPAL-AUTHORIZATION' => "token=#{access_token},signature=#{signature},timestamp=#{timestamp}" }
end
public
def x_pp_authorization_signature url, api_user_id, api_password, timestamp, access_token, access_token_verifier
# no query params, but if there were, this is where they'd go
query_params = {}
key = [
paypal_encode(api_password),
paypal_encode(access_token_verifier),
].join("&")
params = query_params.dup.merge({
"oauth_consumer_key" => api_user_id,
"oauth_version" => "1.0",
"oauth_signature_method" => "HMAC-SHA1",
"oauth_token" => access_token,
"oauth_timestamp" => timestamp,
})
sorted_query_string = params.to_paypal_permissions_query
base = [
"POST",
paypal_encode(url),
paypal_encode(sorted_query_string)
].join("&")
base = base.gsub /%([0-9A-F])([0-9A-F])/ do
"%#{$1.downcase}#{$2.downcase}" # hack to match PayPal Java SDK bit for bit
end
digest = OpenSSL::HMAC.digest('sha1', key, base)
Base64.encode64(digest).chomp
end
# The PayPalURLEncoder java class percent encodes everything other than 'a-zA-Z0-9 _'.
# Then it converts ' ' to '+'.
# Ruby's CGI.encode takes care of the ' ' and '*' to satisfy PayPal
# (but beware, URI.encode percent encodes spaces, and does nothing with '*').
# Finally, CGI.encode does not encode '.-', which we need to do here.
def paypal_encode str
s = str.dup
CGI.escape(s).gsub('.', '%2E').gsub('-', '%2D')
end
end
end
end
样本参数:
url = 'https://svcs.sandbox.paypal.com/Permissions/GetBasicPersonalData'
api_user_id = 'caller_1234567890_biz_api1.yourdomain.com'
api_password = '1234567890'
access_token = 'YJGjMOmTUqVPlKOd1234567890-jdQV3eWCOLuCQOyDK1234567890'
access_token_verifier = 'PgUjnwsMhuuUuZlPU1234567890'
这篇关于在Ruby中生成Paypal签名,"X-PAYPAL-AUTHORIZATION"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!