gpt4 book ai didi

c# - 无法使用 Google API 更新 GTM 变量

转载 作者:太空宇宙 更新时间:2023-11-03 15:08:41 25 4
gpt4 key购买 nike

使用“试一试!” GTM API 文档中的功能: https://developers.google.com/tag-manager/api/v1/reference/accounts/containers/variables/list?authuser=2我能够从 GTM 获得包含我的变量的列表

但是,当我尝试更新变量 ( https://developers.google.com/tag-manager/api/v1/reference/accounts/containers/variables/update?authuser=2) 时,再次使用“尝试一下!”,我得到:

{
"error": {
"errors": [
{
"domain": "global",
"reason": "invalidArgument",
"message": "Bad Request"
}
],
"code": 400,
"message": "Bad Request"
}
}

我很确定我正确地插入了所有参数,并且在尝试了一天之后我假设 GTM 的更新变量 API 有问题。

有人可以确认更新 GTM 变量对他们有效吗?

我也尝试从后端执行此操作并弹出相同的响应。

这是我的代码:

   public void Authenticate()
{


string serviceAccountEmail = "xxxxxxxxxxxx@local-proj.iam.gserviceaccount.com";

string folder = System.Web.HttpContext.Current.Server.MapPath("/App_Data/MyGoogleStorage/key.p12");
var certificate = new X509Certificate2(folder, "notasecret", X509KeyStorageFlags.Exportable);

ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { TagManagerService.Scope.TagmanagerEditContainers}
}.FromCertificate(certificate));

// Create the service.
var service = new TagManagerService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "My Local Proj"
});

此时服务已通过身份验证,因此我可以简单地执行以下操作:

var myGTMVars = service.Accounts.Containers.Variables.List("AccountID", "ContainerID").Execute();

从后端从 GTM 获取我的所有变量。一切都按预期进行。

但是当我尝试更新一个时:

    Variable gTMVar = new Variable();
gTMVar.Name = "IsAfterSignUpSuccess";
gTMVar.Type = "c";
var param = new Parameter()
{
Type = "template",
Value = "true"
};
gTMVar.Parameter = new List<Parameter>();
gTMVar.Parameter.Add(param);
try
{
var newVar = service.Accounts.Containers.Variables.Update(body: gTMVar, accountId: "xxx", containerId: "xxx", variableId: "x").Execute();
}
catch (Exception ex)
{

}

每次给我 BadRequest 都会失败。我尝试了不同的参数,不同类型的变量,但没有任何帮助。还尝试了不同的范围来初始化服务 - 没有任何帮助。

关于这个主题的信息很少,尤其是关于 c# 的信息。

我在这里发布了我的代码 - 希望可以节省一些人的时间,因为在没有用户交互的情况下从后端验证服务并不是直接的。 (最后证明很简单)

最佳答案

如果您尝试更新值,请求中的参数必须具有 3 个属性:
类型:您要编辑的类型,
键:值,
值(value):你的值(value)

网址:https://www.googleapis.com/tagmanager/v1/accounts/accountId/containers/containerId/variables/variableId
参数:

{
"name":"YOUR_VARIABLE_NAME",
"type":"YOUR_VARIABLE_TYPE",
"parameter":[{
"type":"YOUR_TYPE",
"key":"value",
"value":"YOUR_VALUE"
}]
}

跟踪代码管理器 API 的文档有遗漏,而且几乎没有可用的帮助。大多数指南都侧重于 GUI,而 Simo Ahava 在他的示例中主要使用 Python。

最好的,西卡

关于c# - 无法使用 Google API 更新 GTM 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42167370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com