googleapis / google-api-php-client

A PHP client library for accessing Google APIs
Apache License 2.0
9.22k stars 3.52k forks source link

GTM v2 -"Returned an error response for your request" - What error?... #2470

Closed psociety closed 10 months ago

psociety commented 1 year ago

When i try to create a template in GTM v2 i get the following error:

Google\Service\Exception Object
(
    [errors:protected] => Array
        (
            [0] => Array
                (
                    [message] => Returned an error response for your request.
                    [domain] => global
                    [reason] => badRequest
                )

        )

    [message:protected] => {
  "error": {
    "code": 400,
    "message": "Returned an error response for your request.",
    "errors": [
      {
        "message": "Returned an error response for your request.",
        "domain": "global",
        "reason": "badRequest"
      }
    ],
    "status": "INVALID_ARGUMENT"
  }
}

    [string:Exception:private] => Google\Service\Exception: {
  "error": {
    "code": 400,
    "message": "Returned an error response for your request.",
    "errors": [
      {
        "message": "Returned an error response for your request.",
        "domain": "global",
        "reason": "badRequest"
      }
    ],
    "status": "INVALID_ARGUMENT"
  }
}

How I am expected to fix the error if no proper error message is returned?

Used code:

$template = new CustomTemplate();
$template->setName("google_api_sucks");
$template->setTemplateData(file_get_contents('google_template.tpl'));

return $this->tagManager->accounts_containers_workspaces_templates->create($this->getWorkspacePath(), $template);

Template data:

___INFO___

{
  "type": "TAG",
  "id": "cvt_temp_public_id",
  "version": 1,
  "securityGroups": [],
  "displayName": "google_api_sucks",
  "brand": {
    "id": "brand_dummy",
    "displayName": ""
  },
  "description": "",
  "containerContexts": [
    "WEB"
  ]
}

___TEMPLATE_PARAMETERS___

[]

___SANDBOXED_JS_FOR_WEB_TEMPLATE___

// Enter your template code here.
const log = require('logToConsole');
log('data =', data);

// Call data.gtmOnSuccess when the tag is finished.
data.gtmOnSuccess();

___WEB_PERMISSIONS___

[
  {
    "instance": {
      "key": {
        "publicId": "logging",
        "versionId": "1"
      },
      "param": [
        {
          "key": "environments",
          "value": {
            "type": 1,
            "string": "debug"
          }
        }
      ]
    },
    "isRequired": true
  }
]

___TESTS___

scenarios: []

___NOTES___
bshaffer commented 10 months ago

I copied your code exactly, and was able to run the API without any issues. After creating a Workspace in the Tag Manager UI and copying the workspace path into the $path variable below (and copying your template data into template.tpl), I ran the following script:

require 'vendor/autoload.php';

$client = new Google\Client();
$client->setScopes([
  'https://www.googleapis.com/auth/tagmanager.edit.containers',
]);
$client->useApplicationDefaultCredentials();

$service = new Google\Service\TagManager($client);
$path = 'accounts/MY_ACCOUNT_ID/containers/MY_CONTAINER_ID/workspaces/2';
$template = new Google\Service\TagManager\CustomTemplate();
$template->setName("google_api_rocks");
$template->setTemplateData(file_get_contents(__DIR__ . '/template.tpl'));
$results = $service->accounts_containers_workspaces_templates->create($path, $template);

This resulted in the template being created in the workspace as expected:

Screenshot 2023-09-08 at 10 01 42 AM

I agree the error message from the API is not very helpful, but that is not the fault of this client library, but rather the API itself, so there's nothing I can do to help you. Good luck!