某些背景:
我正在尝试使用Drupal的webform模块使用我帐户的沙箱设置中的凭据向https://connect.squareup.com/v2/payments发出REST API请求。该模块通过instantiating a new Client object然后调用方法post(string $request_url, array $request_options)
来使用GuzzleHttp。 $request_options
数组包括一个'headers'数组和一个'json'数组(后者的键被替换为'body')。
我在做什么:
我正在尝试从Square的“付款API”文档的'Create payment'部分重新创建示例请求。我已经根据需要修改了一些内容,包括:
ACCESS_TOKEN
和我的“沙箱访问令牌”source_id
,带有测试卡随机数“ cnon:card-nonce-ok”]customer_id
,因为我没有引用客户location_id
,带有“默认测试帐户”的位置ID curl https://connect.squareup.com/v2/payments \
-X POST \
-H 'Square-Version: 2020-04-22' \
-H 'Authorization: Bearer {{ SANDBOX_ACCESS_TOKEN }}' \
-H 'Content-Type: application/json' \
-d '{
"idempotency_key": "4935a656-a929-4792-b97c-8848be85c27c",
"amount_money": {
"amount": 200,
"currency": "USD"
},
"source_id": "cnon:card-nonce-ok",
"autocomplete": true,
"location_id": "{{ DEFAULT_TEST_ACCOUNT_LOCATION_ID }}",
"reference_id": "123456",
"note": "Brief description",
"app_fee_money": {
"amount": 10,
"currency": "USD"
}
}'
但是我在调试时只得到以下响应:401 Response status code:
Date:
- 'Mon, 27 Apr 2020 23:58:54 GMT'
Frame-Options:
- DENY
X-Frame-Options:
- DENY
X-Content-Type-Options:
- nosniff
X-Xss-Protection:
- '1; mode=block'
Content-Type:
- application/json
Content-Length:
- '124'
Strict-Transport-Security:
- 'max-age=631152000; includeSubDomains; preload'
Response header:
{"errors": [{"code": "UNAUTHORIZED","detail": "This request could not be authorized.","category": "AUTHENTICATION_ERROR"}]}
Response body:
errors:
-
code: UNAUTHORIZED
detail: 'This request could not be authorized.'
category: AUTHENTICATION_ERROR
我被困的地方:
我已经在本地使用本地证书进行了尝试,现在已经投入生产(仍然具有沙盒凭据)。我怀疑可能是问题出在打印headers
的关联数组包含'Authorization' => "Bearer ACCESS_TOKEN"
,该数组在值周围产生了单引号。但是我对此不太确定。还有其他方法可以调试Square根据请求得到的内容吗?还有其他任何想法或问题吗?