使用「試用它!」從GTM API文檔的功能: https://developers.google.com/tag-manager/api/v1/reference/accounts/containers/variables/list?authuser=2 我能夠從GTM得到一個清單,我的變量無法使用Google API更新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 = "[email protected]";
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)
{
}
失敗每次給我的錯誤請求的時間。我嘗試了不同的參數,不同的變量類型,沒有任何幫助。還嘗試了不同的範圍來初始化服務 - 沒有任何幫助。
這方面的信息很差,尤其是對於c#。
我在這裏發佈了我的代碼 - 希望可以節省一些人的日子,因爲它不是直接從後端驗證服務而無需用戶交互。 (以及它到底竟然是容易)
您是否設法解決這個問題?我有一個與awct標籤類似的問題,而每一個設置和參數是完全正確的,它仍然返回400錯誤的請求,在自定義的HTML它工程很好,我已經完全複製了字典中建議的設置,但似乎沒有任何工作。 –