helloworld
helloworld

Reputation: 11

Smartsheet Webhook error

i am trying to create a webhook from one of my applications in php using curl

here is the piece of code i am using (my token is correct and i own the 6642490389358468 sheet)

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;
        }

but i get the following response

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

can you help me troubleshoot that? am i missing somehing?

Upvotes: 0

Views: 434

Answers (2)

helloworld
helloworld

Reputation: 11

Thank you all,

now it works, i did not include the ADMIN_WEBHOOKS access scope when asked for my $token, and of course i forgot the "name" property !!

Upvotes: 1

Taylor Krusen
Taylor Krusen

Reputation: 1033

There is one small error in the code—you need to pass a "name" property back with your jsonData array.

Regarding the authorization issue, I was able to use your exact code with my Smartsheet credentials to successfully create a webhook on a sheet that I created. Double-check that your Access Token is working properly (or you can issue a brand new one) by using it to do another API call like a getSheet. If the Access Token works, then the issue is with permissions on the sheet you are trying to add a webhook to. Make sure you have 'Owner' or 'Admin' status on the sheet and copy the sheet ID over again.

I can confirm that the code works with the addition of the 'name' property.

Upvotes: 1

Related Questions