bitpay / ruby-client

Powerful, flexible, lightweight SDK for the BitPay Bitcoin Payment Gateway API.
MIT License
78 stars 64 forks source link

BitPay::BitPayError: 500: Unauthorized sin #63

Open ayazahmadtarar-pikessoft opened 6 years ago

ayazahmadtarar-pikessoft commented 6 years ago

I have created a client client = BitPay::SDK::Client.new(tokens:'9qUWYMu', debug:true, facade: 'pos', api_uri: 'https://test.bitpay.com') and now requesting for invoice = client.create_invoice(price: '123', currency: 'USD')

2.4.0 :048 > invoice = client.create_invoice(price: '123', currency: 'USD') opening connection to test.bitpay.com:443... opened starting SSL for test.bitpay.com:443... SSL established <- "GET /tokens HTTP/1.1\r\nAccept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3\r\nAccept: */*\r\nUser-Agent: ruby-bitpay-sdk 2.4.4\r\nX-Signature: 3045022100c5715f088a26f5ee73fb840f29af26fcc6830f9aa3d89f45db5ce9d004015aeb02202285de31c06c5bea8e8a5ef004de6a9e29ce6be9b823e7eb188ec5c554cb318f\r\nX-Identity: 04cd63e7dd6738a081e7070fe56486bebbf7cc164b51a894d5fef4d12f648c99738cede694b319a005ac614f68755356d183e4b1d87355bf4e80e2ec6f14f13f32\r\nContent-Type: application/json\r\nX-Bitpay-Plugin-Info: Rubylib2.4.4\r\nConnection: close\r\nHost: test.bitpay.com\r\n\r\n" -> "HTTP/1.1 500 Internal Server Error\r\n" -> "Date: Sat, 16 Dec 2017 09:51:50 GMT\r\n" -> "Content-Type: application/json; charset=utf-8\r\n" -> "Content-Length: 28\r\n" -> "Connection: close\r\n" -> "Set-Cookie: __cfduid=d297bd272509bb8e49124e6c7e89e3be51513417910; expires=Sun, 16-Dec-18 09:51:50 GMT; path=/; domain=.bitpay.com; HttpOnly\r\n" -> "Strict-Transport-Security: max-age=31536000\r\n" -> "X-Download-Options: noopen\r\n" -> "X-Content-Type-Options: nosniff\r\n" -> "Access-Control-Allow-Origin: *\r\n" -> "Access-Control-Allow-Methods: HEAD,GET,POST,PUT,DELETE,OPTIONS\r\n" -> "Access-Control-Allow-Headers: Content-Type,Authorization,Content-Length,X-Requested-With,Cache-Control,X-Accept-Version,x-signature,x-pubkey,x-identity,cf-connecting-ip\r\n" -> "x-deprecated-in-favor-of: none\r\n" -> "ETag: W/\"1c-S8DKEc4R1f/Mx8wxpNKK4ranIaM\"\r\n" -> "set-cookie: connect.sid=s%3AI2-av_owCCNgeCSI5tzHDkDtaq_5vu9q.AOsphiJwJ7xQYMtDAS17tw%2FSr5mhuveUKr2V9%2F7%2FX5M; Path=/; HttpOnly; Secure\r\n" -> "Server: cloudflare-nginx\r\n" -> "CF-RAY: 3ce0bf138d583e7a-ZRH\r\n" -> "\r\n" reading 28 bytes... -> "{\"error\":\"Unauthorized sin\"" -> "}" read 28 bytes Conn close BitPay::BitPayError: 500: Unauthorized sin from /Users/ayaz/.rvm/gems/ruby-2.4.0/gems/bitpay-sdk-2.4.4/lib/bitpay/rest_connector.rb:73:inprocess_request' from /Users/ayaz/.rvm/gems/ruby-2.4.0/gems/bitpay-sdk-2.4.4/lib/bitpay/rest_connector.rb:28:in get' from /Users/ayaz/.rvm/gems/ruby-2.4.0/gems/bitpay-sdk-2.4.4/lib/bitpay/rest_connector.rb:84:inrefresh_tokens' from /Users/ayaz/.rvm/gems/ruby-2.4.0/gems/bitpay-sdk-2.4.4/lib/bitpay/rest_connector.rb:102:in get_token' from /Users/ayaz/.rvm/gems/ruby-2.4.0/gems/bitpay-sdk-2.4.4/lib/bitpay/client.rb:72:increate_invoice' from (irb):48 from /Users/ayaz/.rvm/rubies/ruby-2.4.0/bin/irb:11:in <main>' 2.4.0 :049 >

guih commented 6 years ago

I'm having this too

pieterpoorthuis commented 6 years ago

BitPay has reset its test environment this week. If you try to use an old test account, please create a new test account at test.bitpay.com

About the pairing: did you follow the support article from https://support.bitpay.com/hc/en-us/articles/115003025666-How-do-I-create-private-keys-and-public-keys-for-the-BitPay-API- , section 'Ruby' ? Best is to do the pairing without passing a pairingcode in the parameters. BitPay will return a token + pairingcode. The pairingcode you need to approve in your merchant dashboard (Payment Tools -> Manage API tokens). After approval of the pairingcode, you can use the token forever to create invoices.

guih commented 6 years ago

Thanks, @pieterpoorthuis I'll do as instructed.

Tatsuhiko-Nakagawa commented 5 years ago

I followed the instruction from https://support.bitpay.com/hc/en-us/articles/115003025666-How-do-I-create-private-keys-and-public-keys-for-the-BitPay-API- , section 'Ruby' and tried

client = BitPay::SDK::Client.new(tokens:'MY-TOKEN-SHOWED-IN-DASHBOARD', debug:true, facade: 'merchant', api_uri: 'https://test.bitpay.com') invoice = client.create_invoice(price: "0.00001", currency: "BTC")

and I got 500: Unauthorized sin. What was wrong?

danescsvn commented 5 years ago

We are looking into this issue. If you can please email us your system specs to sales-engineering@bitpay.com and any other logs you may have would be helpful. Thank you.

Tatsuhiko-Nakagawa commented 5 years ago

@danescsvn I emailed you the recreation procedure. Thanks!

cjr-roland commented 5 years ago

Hi Tatsuhiko,

In your tests, you were passing the client_id (TfL...) as the API token. That's actually a hash of the pub_key created - not the API token.

To authenticate your client:

  1. use the generate_pem function to create a private/public key pair and store the .pem file.
  2. initialize the client based on this pem file, BitPay::SDK::Client.new(api_uri: 'https://test.bitpay.com', pem: pem)
  3. use the pair_client() function to request a pairing code from the BitPay server. This function will return a token object (containing the token string and the pairing code).
  4. enter the pairing code on https://test.bitpay.com/api-tokens and select the appropriate facade (use merchant). Once this is done, your client is paired, and the token generated on step 3 is approved and can be used to create invoices (in combination with the keys from the pem file).

Check the following example https://github.com/bitpay/ruby-client/blob/master/GUIDE.md#pairing-programatically.

Once you have a paired client, you can use the create_invoice() function and create test invoices - see https://github.com/bitpay/ruby-client/blob/019140f04959589e7137c9b81cc1b848e15ebbe6/GUIDE.md#create-a-new-bitcoin-invoice. You would need to explicitely set the facade you are using in when calling create_invoice function:

create_invoice(price: '10', currency: 'USD', facade: 'merchant')

pydelllover commented 4 years ago

@cjr-roland sir can you give this example in python. i am having too much issues from last 7 days. Totally stuck please help

dteh commented 4 years ago

@pydelllover you should post in the bitpay-python repo

  1. create a token at https://test.bitpay.com/api-tokens
  2. with the pairing token:
    
    from bitpay import key_utils
    from bitpay.client import Client
    pem = key_utils.generate_pem()
    with open('./bitpay.pem', 'w') as pem_file:
    pem_file.write(pem)

client = Client(api_uri="https://test.bitpay.com", pem=pem) tokens = client.pair_pos_client(pairing_token)

3. When instantiating the client in the future:

with open('./bitpay.pem', 'r') as pemfile: pem = pemfile.read() client = Client(api_uri=settings.BITPAY_API_BASE, pem=pem, tokens=tokens) invoice = client.create_invoice( { "price":price, "currency":"USD", "token":client.tokens['pos'], } )