0

我正在尝试使用 curl 从我在 php 中的一个应用程序创建一个 webhook

这是我正在使用的一段代码(我的令牌是正确的,我拥有 6642490389358468 表)

function setWebHook($token){
            // API Url
            // BASE_API_URL = "https://api.smartsheet.com/2.0/";
            $url = self::BASE_API_URL. "webhooks";
            $headers = array(
              "Authorization: Bearer ". $token,
              "Content-Type: application/json"
             );
            //The JSON data.
            $jsonData = array(
                "callbackUrl"=>"https://www.example.com/myapp/test",
                "scope"=>"sheet",
                "scopeObjectId"=>6642490389358468,
                "version"=>1,
                "events"=>[ "*.*" ]
            );
            //Initiate cURL.
            $ch = curl_init($url);
            //Encode the array into JSON.
            $jsonDataEncoded = json_encode($jsonData);
            //Tell cURL that we want to send a POST request.
            curl_setopt($ch, CURLOPT_POST, 1);
            //Attach our encoded JSON string to the POST fields.
            curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
            //Set the content type to application/json
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            //Execute the request
            $result = curl_exec($ch);
            if (curl_errno($ch)) {
                $apiResponse = "Oh No! Error: " . curl_error($ch);
            } else {
                // Assign response to variable
                $apiResponse = json_decode($result);
                curl_close($ch);
            }
            return $apiResponse;
        }

但我得到以下回复

{
    "errorCode": 1004,
    "message": "You are not authorized to perform this action.",
    "refId": "x2kcvthuyfs8"
}

你能帮我解决这个问题吗?我错过了什么吗?

4

2 回答 2

1

谢谢你们,

现在它可以工作了,当我询问我的 $token 时,我没有包括 ADMIN_WEBHOOKS 访问范围,当然我忘记了“名称”属性!

于 2018-02-15T23:32:51.957 回答
1

代码中有一个小错误——你需要用你的 jsonData 数组传回一个“name”属性。

关于授权问题,我能够使用您的确切代码和我的 Smartsheet 凭据在我创建的工作表上成功创建一个 webhook。通过使用它执行另一个 API 调用(如 getSheet),仔细检查您的访问令牌是否正常工作(或者您可以发布一个全新的)。如果访问令牌有效,则问题在于您尝试添加 webhook 的工作表上的权限。确保您在工作表上具有“所有者”或“管理员”状态,然后再次复制工作表 ID。

我可以确认代码在添加“名称”属性的情况下有效。

于 2018-02-15T20:51:06.553 回答