2016-03-11 108 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正確無誤 - 您可以在開發人員儀表板中找到這些信息。