在标签管理器中创建新容器时请求错误


Bad Request when creating new container in Tag-Manager

我使用php sdk与Google标签管理器进行交互。当我运行这段代码

    $client = new 'Google_Client();
    $client->setClientId('xxxx');
    $client->setClientSecret('xxxx');
    $client->setAccessToken($token);
    $container = new 'Google_Service_TagManager_Container();
    $container->setName('a-name');
    $container->setTimeZoneCountryId('America/Los_Angeles');
    $container->setTimeZoneId('US');
    $container->setUsageContext(array('web', 'android', 'ios'));
    $tagManager = new 'Google_Service_TagManager($client);
    $tagManager->accounts_containers->create('xxx', $container);

则抛出异常:Error calling POST https://www.googleapis.com/tagmanager/v1/accounts/xxx/containers: (400) Bad Request .

容器数据与Google Developer控制台中相同。在Google Developer Console中的示例请求也会产生相同的Bad Request错误。

任何想法?是API bug吗?

这篇文章很老了,我的答案与标签管理器api v2有关,但我也在努力创建一个容器(在v2中),刚刚完成了任务。我在这里分享我的代码,因为当我搜索我的问题时,我在这里结束,没有找到答案…也许能帮到别人。

基本上在做了所有验证的东西之后,这里是如何使用标签管理器v2 api创建容器:

    $container = new Google_Service_TagManager_Container();
    $container->setUsageContext(['web']);
    $container->setName('yourContainerName');
    $container->setNotes('yourContainerNotes');
    $accountId = 'yourAccountId';
    try {
        $accountContainer = $tagManager->accounts_containers->create('accounts/'.$accountId, $container);
    } catch(Exception $e) {
        $this->printH($e->getMessage());
        $this->printH($e->getCode());
    }