Posts tagged with c#

  UserCredential credential;       using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))       {         credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(             GoogleClientSecrets.Load(stream).Secrets,             // This OAuth 2.0 access scope allows for full read/write access to the             // authenticated user's account.             new[] { YouTubeService.Scope.Youtube },             "user",             CancellationToken.None,             new FileDataStore(this.GetType().ToString())         );       } 

Using the code I shared above, I can renew the duration of the refresh token and create a campaign through Google Ads SDK (for Net 5 application) without any problems, but when I launch the same application in Azure Containers, I get a "Token Expire" error.

I will be very happy if you can help me with this behavior difference. Thank you in advance.

Kind regards

I want to develop a console application that pulls all campaigns under adwords accounts using Google Ads Api. But I could not pass the authentication step.

I do not fully understand whether I should use the Service Account or Desktop Application Flow for this process.

GoogleAdsConfig config = new GoogleAdsConfig()             {                 DeveloperToken = "Dev_token",                 OAuth2Mode = Google.Ads.GoogleAds.Config.OAuth2Flow.APPLICATION,                 OAuth2ClientId = "client_Id",                 OAuth2ClientSecret = "secrret",                 OAuth2RefreshToken = " refresh_token",              }; GoogleAdsClient client = new GoogleAdsClient(config); GoogleAdsServiceClient googleAdsService = client.GetService(Google.Ads.GoogleAds.Services.V10.GoogleAdsService); googleAdsService.SearchStream(AdwordsClientId, query,                 delegate (SearchGoogleAdsStreamResponse resp)                 {                     foreach (GoogleAdsRow adsRow in resp.Results)                     {                      }                 }             ); 

When I try as above, I get the following error

Google.Apis.Auth.OAuth2.Responses.TokenResponseException: Error:"unauthorized_client", Description:"Unauthorized", Uri:""

What paths should i follow? Thank you.

I have a project whose Publishing status is Testing (I am using Google Ads Api).

            using FileStream stream = new("Credentials.json", FileMode.Open, FileAccess.Read);             // The file token.json stores the user's access and refresh tokens, and is created             // automatically when the authorization flow completes for the first time.             UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(              GoogleClientSecrets.FromStream(stream).Secrets,              scopes,              "user",              CancellationToken.None,              new FileDataStore("Token", true)             );             if (credential.Token.IsExpired(credential.Flow.Clock))                 await credential.RefreshTokenAsync(CancellationToken.None); 

When I use the code above, it expects me to log in via Gmail, but I want to automatically handle the Refresh token and continue uninterrupted requests without understanding anything to the user. I am using Desktop Application as Flow, but I am using Net 5 Api as Framework. I look forward to your help in this matter.

Thank you from now.

I am using .net client library to access google ads API using C# .NET.

I have configured 'Installed App' account in following order:

  1. Configured Manager Account for Developer Token.
  2. Created oAuth credentials using Manager Account and developer console
  3. Created test manager account (test Account isn't linked to Ads account).
  4. Enabled Ads API from Ads Console.
  5. Placed configuration in App.Config

App.Config configuration:

    <add key = 'OAuth2Mode' value = 'APPLICATION' />     <add key = 'OAuth2ClientId' value = 'OAUTH-CLIENT-ID' />     <add key = 'OAuth2ClientSecret' value = 'SECRET' />     <add key = 'OAuth2RefreshToken' value = 'REFRESH-TOKEN' />     <add key="DeveloperToken" value="DEV-TOKEN" /> 

I got refresh token from adwords client-library OAuthTokenGenerator.exe using Test account.

Here is my code to access Ads API:

 public static void GetCampaigns(GoogleAdsClient client, long customerId)  {       client.Config.LinkedCustomerId = customerId.ToString();       GoogleAdsServiceClient googleAdsService = client.GetService(Services.V9.GoogleAdsService);       string query = @"SELECT                             campaign.id,                             campaign.name,                             campaign.network_settings.target_content_network                         FROM campaign                         ORDER BY campaign.id";             try             {                 // Issue a search request.                 googleAdsService.SearchStream(customerId.ToString(), query,                     delegate (SearchGoogleAdsStreamResponse resp)                     {                         foreach (GoogleAdsRow googleAdsRow in resp.Results)                         {                             Console.WriteLine("Campaign with ID {0} and name '{1}' was found.",                                 googleAdsRow.Campaign.Id, googleAdsRow.Campaign.Name);                         }                     }                 );             }             catch (GoogleAdsException e)             {                 Console.WriteLine("Failure:");                 Console.WriteLine($"Message: {e.Message}");                 Console.WriteLine($"Failure: {e.Failure}");                 Console.WriteLine($"Request ID: {e.RequestId}");                 throw;             }         } 

However i am receiving this exception on calling API:

 Status(StatusCode="Unauthenticated", Detail="Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", DebugException="Grpc.Core.Internal.CoreErrorDetailException: {"created":"@1643028386.159000000","description":"Error received from peer ipv4:142.250.181.106:443","file":"..\..\..\src\core\lib\surface\call.cc","file_line":1070,"grpc_message":"Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.","grpc_status":16}") 

can anyone guide me what am i missing in this configuration?

I am trying to update the AmoutMicros of Budget but I don't receive error, just not update the budget:

        CampaignBudget budget = new CampaignBudget()         {             ResourceName = ResourceNames.CampaignBudget(customerId, budgetId),             AmountMicros = (price + amount) * 100000,             Id = budgetId         };         CampaignBudgetOperation budgetOperation = new CampaignBudgetOperation()         {             Create = budget,             UpdateMask = FieldMasks.AllSetFieldsOf(budget),         };         try         {             MutateCampaignBudgetsResponse responseBudget =              budgetService.MutateCampaignBudgets(             customerId.ToString(), new CampaignBudgetOperation[] { budgetOperation            });             foreach(MutateCampaignBudgetResult result in responseBudget.Results)             {                 updateResponse.CampignId = campaignId;                 updateResponse.Updated = true;                 Console.WriteLine(result);             }             return updateResponse;         }         catch (GoogleAdsException e)         {             Console.WriteLine("Failure:");             Console.WriteLine($"Message: {e.Message}");             Console.WriteLine($"Failure: {e.Failure}");             Console.WriteLine($"Request ID: {e.RequestId}");             return updateResponse;             throw;         } 

This code don't show me a exception, apparently it was update, but, when I go at Google Ads Dashboard, the amout still the same.

I read the documentation but I didn't find an update of a budget.