Not able to connect to Google Tag Manager API via C# API using Service Account

353 Views Asked by At

I'm trying to access Google Tag Manager API to get the list of my Tags via my C# Web API As mentioned in the documentation, authorization is only possible via OAuh 2.0 hence I have created a service account so that my web API can interact with google cloud services without any user interaction. I have downloaded the JSON/P12 Key and stored it in my solution and was able to extract the key But when I pass that key to Google Tag Manager API in the Header in HTTPClient Request I'm getting an error as not found

Following is the code snippet I'm using

        string token = "";
        using (FileStream stream = new FileStream(jsonKeyFilePath, FileMode.Open, FileAccess.Read))
        {
            token = await GoogleCredential
                .FromStream(stream)
                .CreateScoped(new []{ TagManagerService.Scope.TagmanagerReadonly})
                .UnderlyingCredential
                .GetAccessTokenForRequestAsync();
        }
        using HttpClient client = new HttpClient();
        string url = "https://tagmanager.googleapis.com/tagmanager/v2/accounts/{accountid}/containers/{containerid}/workspaces/{workspaceid}/tags";
        client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
        HttpResponseMessage response = await client.GetAsync(url);
        if (response.IsSuccessStatusCode) // this is always returning false
        {
            log(response);
        }

I tried finding some resources online but no help there is very little documentation related to .Net framework support Also, the token extracted via GoogleCredential contains a lot of dots at the end something like this

ta27.c.dzj7GOL8nadWmrqM74......................................................................................................................................................................................................................................................................................................................................................................................................

In response I'm getting, status as NotFound

enter image description here

1

There are 1 best solutions below

0
Akhil RJ On BEST ANSWER

I was able to figure out the solution to the problem was with container access I had to add my service account email as a user to my google tag manager container And it worked