Mr.D Mr.D - 4 months ago 35
Ruby Question

Ruby PayPal SDK, getting token info from Authorize code throws 400 error

I have created PayPal business account and I have created applicaiton in

sandbox
mode.

By using
PayPal-Ruby-SDK
I have added my
client_id
,
client_secret
,
username
,
password
and
signature
to my
paypal.yml
file.

I also, use
PayPal-Android-SDK
, which has
client_id
of this app and when my Phone application logins via PayPal, it creates that kind of response:

"response": {
"code":"mock_code_EJhi9jOPswug9TDOv93qg4Y28xIlqPDpAoqd7biDLpeGCPvORHjP1Fh4CbFPgKMGCHejdDwe9w1uDWnjPCp1lkaFBjVmjvjpFtnr6z1YeBbmfZYqa9faQT_71dmgZhMIFVkbi4yO7hk0LBHXt_wtdsw"
},
"client": {
"platform": "Android",
"paypal_sdk_version": "2.14.4",
"product_name": "PayPal-Android-SDK",
"environment": "mock"
},
"response_type": "authorization_code"
}


Now I want to generate
refresh token
from this
code
field. I take this
code
and enter it in my ruby console:

tokeninfo = Tokeninfo.create('mock_code_EJhi9jOPswug9TDOv93qg4Y28xIlqPDpAoqd7biDLpeGCPvORHjP1Fh4CbFPgKMGCHejdDwe9w1uDWnjPCp1lkaFBjVmjvjpFtnr6z1YeBbmfZYqa9faQT_71dmgZhMIFVkbi4yO7hk0LBHXt_wtdsw')


However, this throws me that kind of error:

PayPal::SDK::Core::Exceptions::BadRequest: Failed. Response code = 400. Response message = Bad Request. Response debug ID = 93403fd3eb431, 1998e13f9d984.
from D:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/paypal-sdk-rest-1.4.8/lib/paypal-sdk/core/util/http_helper.rb:135:in `handle_response'
from D:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/paypal-sdk-rest-1.4.8/lib/paypal-sdk/core/util/http_helper.rb:78:in `http_call'
from D:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/paypal-sdk-rest-1.4.8/lib/paypal-sdk/core/api/base.rb:86:in `api_call'
from D:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/paypal-sdk-rest-1.4.8/lib/paypal-sdk/core/api/base.rb:98:in `post'
from D:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/paypal-sdk-rest-1.4.8/lib/paypal-sdk/core/openid_connect.rb:71:in `create_from_authorization_code'
from (irb):3
from D:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.2.7/lib/rails/commands/console.rb:110:in `start'
from D:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.2.7/lib/rails/commands/console.rb:9:in `start'
from D:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.2.7/lib/rails/commands/commands_tasks.rb:68:in `console'
from D:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.2.7/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from D:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.2.7/lib/rails/commands.rb:17:in `<top (required)>'


I have checked Login with PayPal option in my dashboard. Why this is happening?

Also, I have noticed that this
mock_code_
is same, whenever I login from different account.

Answer

I will post answer to my own question. This could help to other people in future.

The main reason of failing Tokeninfo.create method is that authorization code, which is received from Android application is in mock mode.

Every authorization code which has mock_code_ prefix will not work with sandbox(and live). This is just for testing in device.

In Android SDK you must set ENVIRONMENT_SANDBOX or ENVIRONMENT_PRODUCTION instead of ENVIRONMENT_NO_NETWORK.