2016-03-11 195 views
0

我试图使用PayPal的红宝石宝石做一个简单的任务类似这样的用户的PayPal付款:如何:获取通过PayPal红宝石宝石

1 - 用户登录到使用贝宝的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.all(count: 10) 

它返回一个错误:

PayPal::SDK::Core::Exceptions::UnauthorizedAccess: Failed. Response code = 401. Response message = Unauthorized. Response debug ID = 409003e14dd9a, 409003e14dd9a. 

那么,我该如何验证用户进入我的应用程序,然后使用他的令牌来执行API操作?

+0

你确定paypal API支持吗?可能会试着问他们。 –

回答

0

我不确定你是否需要这一切。

这里是我做了什么:

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正确无误 - 您可以在开发人员仪表板中找到这些信息。