0

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?

helloworld
  • 11
  • 1
  • To troubleshoot, I'd suggest a couple of things. 1) Try issuing this same request using [Postman](https://www.getpostman.com/); if it doesn't work the first time, troubleshoot there until the request is successful. 2) Use [Fiddler](https://www.telerik.com/download/fiddler) or similar tool to produce a trace of the raw request that your app code is generating. Compare that request info with the successful request from Postman to identify any differences, and update your code such that it generates the same (successful) request as you created using Postman. – Kim Brandl Feb 15 '18 at 19:47

2 Answers2

1

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.

Taylor Krusen
  • 963
  • 6
  • 10
1

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 !!

helloworld
  • 11
  • 1