我正在尝试使用PayPal的红宝石来完成这样的简单任务:
1-用户使用PayPal OAuth登录到我的应用程序。
2-用户授权我的应用访问他的帐户。
3-我的应用发出了一个简单请求,要求列出该用户的10笔交易。
我正在遵循官方PayPal的github存储库(https://github.com/paypal/PayPal-Ruby-SDK)中的指示
require 'paypal-sdk-rest'
include PayPal::SDK::REST
include PayPal::SDK::OpenIDConnect
# Update client_id, client_secret and redirect_uri
PayPal::SDK.configure({
:openid_client_id => "client_id",
:openid_client_secret => "client_secret",
:openid_redirect_uri => "http://google.com"
})
# Generate URL to Get Authorize code
puts Tokeninfo.authorize_url( :scope => "openid profile" )
# Create tokeninfo by using AuthorizeCode from redirect_uri
tokeninfo = Tokeninfo.create("Replace with Authorize Code received on redirect_uri")
puts tokeninfo.to_hash
# Refresh tokeninfo object
tokeninfo = tokeninfo.refresh
puts tokeninfo.to_hash
经过这个简单的设置代码,我试图获取用户的交易,但是我不能仅使用如下代码的Payment实体来做到这一点:
Payment.all(count: 10)
它返回错误:
PayPal::SDK::Core::Exceptions::UnauthorizedAccess: Failed. Response code = 401. Response message = Unauthorized. Response debug ID = 409003e14dd9a, 409003e14dd9a.
因此,我该如何验证用户的身份,然后使用其令牌执行API操作?
我不确定您是否需要这一切。
这是我所做的:
include PayPal::SDK::REST
PayPal::SDK.configure({
openid_client_id: "client_id",
openid_client_secret: "client_secret",
openid_redirect_uri: "http://google.com"
})
Payment.all
=>
Request[get]: https://api.sandbox.paypal.com/v1/payments/payment
Request.body=null request.header={"X-PAYPAL-SANDBOX-EMAIL-ADDRESS"=>"[email protected]", "Authorization"=>"Bearer A101.CLf7NBF8CWMt0Lt4tc9OzrKs9RmGB8wuDBw11n1Ucn8DWTyws8A-nM5b68NYpn8F.qiud2q4fOpca2RSfhZElDV4fzLm", "Content-Type"=>"application/json", "User-Agent"=>"PayPalSDK/PayPal-Ruby-SDK 1.4.4 (paypal-sdk-core 1.4.4; ruby 2.3.0p0-x86_64-linux;OpenSSL 1.0.1f 6 Jan 2014)"}
Response[200]: OK, Duration: 30.002s
Response.body={"payments":[{"id":"PAY-
...etcetc
值得注意的是,目前它仅显示通过REST API创建的付款。
尽管401是未经授权的,所以很可能您得到的信息不正确。确保您的client_id和client_secret是正确的-您可以在开发人员信息中心下找到此信息。
它现在使用Transactions API对此提供支持。