简体   繁体   English

Gimbal API身份验证失败并显示401未经授权(PHP cURL请求)

[英]Gimbal API Authentication fails with 401 unauthorized (PHP cURL request)

I am trying to connect with the Gimbal manager Restful API. 我正在尝试与云台管理器Restful API连接。 I have an account and an API KEY for my organization. 我有一个帐户和我组织的API密钥。 My cURL request keeps failing with a 401 unauthorized response from the API endpoint. 我的cURL请求不断失败,并且来自API端点的401未经授权的响应。 Here is my cURL request in PHP: 这是我在PHP中的cURL请求:

    // from Gimbal Manager:
    define('ORG_API_KEY', 'XXXXXXXXXXXXXXXXX') ;

    // new beacon registration object:
    $post = array (
        "factory_id" => "XXXX-XXXXX",
        "name" => "NewBeacon",
        "latitude" => 12345
        "longitude" => 67890,
        "visibility" => "public"
    );

    $url = "https://manager.gimbal.com/api/beacons";
    $headers = array(
          'AUTHORIZATION: Token token=' . ORG_API_KEY, 
          'Content-type: application/json'
    ) ;
    $debug = 1 ;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers, TRUE);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    if ($debug) {
        curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, TRUE);
        curl_setopt($curl, CURLINFO_HEADER_OUT, true);
    }

    curl_setopt($crl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

    $result = curl_exec($ch);

(edit) and here the request header (from the $headerSent var above) (编辑),然后是请求标头(来自上面的$ headerSent var)

POST /api/beacons HTTP/1.1
Host: manager.gimbal.com
Accept: */*
Content-Length: 577
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------ce1a4e9dd55e

and here is the response from Gimbal: 这是Gimbal的回应:

HTTP/1.1 401 Unauthorized
Cache-Control: no-cache
Content-Type: text/html; charset=utf-8
Date: Sat, 29 Aug 2015 20:05:10 GMT
Server: Apache
Status: 401 Unauthorized
Vary: Accept-Encoding
WWW-Authenticate: Token realm="Application"
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Request-Id: 7abe53cd-d161-4886-bb61-8cfedf71743a
X-XSS-Protection: 1; mode=block
Content-Length: 27
Connection: keep-alive
HTTP Token: Access denied.

I have tried a few different ways of expressing the AUTH Token: 我尝试了几种表达AUTH令牌的方法:

AUTHORIZATION: <$token>
AUTHORIZATION: Token <$token>
AUTHORIZATION: Token token=<$token>

They all give the same 401 response. 它们都给出相同的401响应。 I have noticed other tickets here and elsewhere describing a similar issue, but none were marked as solved. 我注意到这里和其他地方的其他票证都描述了类似的问题,但是都没有标记为已解决。

Has anyone out there had luck connecting with the Gimbal Manager API? 有没有人能与Gimbal Manager API连接? If so, did your code look different? 如果是这样,您的代码看起来是否有所不同?

In the end I had 2 problems with my initial code above. 最后,我上面的初始代码遇到了两个问题。

The TRUE flag on the CURLOPT_HTTPHEADER was preventing the AUTH/Content-type headers from being sent (not sure why I had it in there and not sure if that flag has any value or meaning). CURLOPT_HTTPHEADER上的TRUE标志阻止了AUTH / Content-type标头的发送(不确定为什么要放在里面,也不确定该标志是否具有任何值或含义)。

The POST data I was sending along was not in the correct format. 我发送的POST数据格式不正确。 Since I was requesting a content-type of JSON it was expecting that POST data to be in that format. 由于我请求的是JSON的内容类型,因此期望POST数据采用该格式。 Once I added json_encode($post), everything worked fine. 添加json_encode($ post)后,一切正常。

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers, TRUE);
.. .became ....
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

and

curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
... became ....
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));

Sometimes it takes carefully typing out and formatting a big StackOverflow issue to see the flaw in the code and solve it yourself. 有时,需要仔细输入并格式化一个大的StackOverflow问题,才能查看代码中的缺陷并自己解决。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM