【发布时间】:2020-10-22 09:46:08
【问题描述】:
我正在处理这个not-so-great ASAP Connected 文档。它是为 .NET 编写的,但我正在用 PHP 为客户端创建一个 WordPress 插件。
为了获得授权,它提供了这个 sn-p:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://stagingapi.asapconnected.com/api/login");
request.Headers.Add(HttpRequestHeader.Authorization, "user=username&organizationId=id&password=password&apiKey=apikey");
request.Accept = "application/json";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string accessToken = response.Headers["asap_accesstoken"];
在我的插件中,我正在尝试这样做:
$request_url = 'https://stagingapi.asapconnected.com/api/login';
$headers = array(
//Accept or Content type
'Accept: application/json',
//Authorisation
'Authorization: ' . http_build_query(array(
'user' => ASAPCONNECTED_USERNAME,
'password' => ASAPCONNECTED_PASSWORD,
'organizationId' => ASAPCONNECTED_ORGANIZATION_ID,
'apiKey' => ASAPCONNECTED_API_KEY
)));
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //Return response as string instead of display
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //Ignore any SSL errors
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); //Specify our header
curl_setopt($curl, CURLOPT_HTTPGET, true); //Specify that this is a GET request
$response = curl_exec($curl); //Execute cURL and collect the response
$curl_info = curl_getinfo($curl); //Retrieve it's info
$curl_error = curl_error($curl); //Retrieve it's error
curl_close($curl); //Close the cURL
if ($curl_error || !$response) {
if ($curl_error) {
//An error occurred requesting authorisation from the API!
}
if (!$response) {
//The response was empty when requesting authorisation from the API!
}
} else {
//The API authorisation request was a success! Do stuff...
}
所以我认为“GetResponse”就像一个 GET 请求,并且文档和它的 sn-p 非常清楚地将 API 凭据放在“授权”标头中。但是,我得到的响应是空的!它不返回任何错误。当我记录$curl_info 变量时,它会输出这个(省略了一些数据):
[01-Jul-2020 21:03:36 UTC] Array
(
[url] => https://stagingapi.asapconnected.com/api/login
[content_type] =>
[http_code] => 401
[header_size] => 765
[request_size] => 222
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.183425
[namelookup_time] => 0.00508
[connect_time] => 0.006237
[pretransfer_time] => 0.047479
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => 0
[upload_content_length] => -1
[starttransfer_time] => 0.183389
[redirect_time] => 0
[redirect_url] =>
[certinfo] => Array
(
)
[http_version] => 2
[protocol] => 2
[ssl_verifyresult] => 0
[scheme] => HTTPS
[appconnect_time_us] => 47412
[connect_time_us] => 6237
[namelookup_time_us] => 5080
[pretransfer_time_us] => 47479
[redirect_time_us] => 0
[starttransfer_time_us] => 183389
[total_time_us] => 183425
)
有什么想法吗?
【问题讨论】:
-
访问令牌在响应头中,而不是数据中。您需要使用
CURLOPT_HEADER将其包含在响应中,然后您必须解析返回的标头。我不认为curl提供任何自动标头解析。 -
啊哈!好吧,这让我更接近@Barmar!谢谢!所以在添加
curl_setopt($curl, CURLOPT_HEADER, true);之后,它就达到了成功功能。虽然在回复中,它现在告诉我asap_accesstoken: There's no authorization parameters。我没有正确发送标头中的凭据吗? -
看起来应该是对的
-
好的,谢谢@Barmar 让我走到这一步。我想我会再次联系他们的支持团队以获取凭据。他们文档中的句子
Note: the username and password is unique to your integration application, and is not related to your regular ASAP application credentials.让我相信我的客户向我提供了他们的登录凭据,而不是集成应用程序凭据,而且这两个可能不相同。
标签: php curl authorization httpwebrequest