using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Dfareporting.v3_0;
using Google.Apis.Dfareporting.v3_0.Data;
using Google.Apis.Json;
using Google.Apis.Services;
namespace ConsoleApp1
{
class Program
{
/// <summary>
/// The OAuth 2.0 scopes to request.
/// </summary>
private static readonly IEnumerable<string> OAuthScopes = new[] {
DfareportingService.Scope.Dfareporting,
DfareportingService.Scope.Dfatrafficking
};
/// <summary>
/// Main method, to run this code example as a standalone application.
/// </summary>
/// <param name="args">The command line arguments.</param>
public static void Main(string[] args)
{
Run();
}
/// <summary>
/// Run the code example.
/// </summary>
/// <param name="service">Unused</param>
public static void Run()
{
string pathToJsonFile = "JSONFILE";
// An optional Google account email to impersonate. Only applicable to service accounts which
// have enabled domain-wide delegation and wish to make API requests on behalf of an account
// within their domain. Setting this field will not allow you to impersonate a user from a
string emailToImpersonate = "";
// Build service account credential.
ServiceAccountCredential credential =
getServiceAccountCredential(pathToJsonFile, emailToImpersonate);
// Create a Dfareporting service object.
//
// Note: application name should be replaced with a value that identifies your application.
DfareportingService service = new DfareportingService(
new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = "C# service account sample"
}
);
CampaignsListResponse campaigns;
String nextPageToken = null;
String fields = "nextPageToken,campaigns(id,name)";
do
{
// Create and execute the campaigns list request.
CampaignsResource.ListRequest request = service.Campaigns.List(PROFILE_ID);
request.Fields = fields;
request.PageToken = nextPageToken;
campaigns = request.Execute();
foreach (Campaign campaign in campaigns.Campaigns)
{
Console.WriteLine("Campaign with ID {0} and name \"{1}\" was found.",
campaign.Id, campaign.Name);
}
// Update the next page token.
nextPageToken = campaigns.NextPageToken;
} while (campaigns.Campaigns.Any() && !String.IsNullOrEmpty(nextPageToken));
Console.ReadLine();
}
private static ServiceAccountCredential getServiceAccountCredential(String pathToJsonFile,
String emailToImpersonate)
{
// Load and deserialize credential parameters from the specified JSON file.
JsonCredentialParameters parameters =
NewtonsoftJsonSerializer.Instance.Deserialize<JsonCredentialParameters>(pathToJsonFile);
// Create a credential initializer with the correct scopes.
ServiceAccountCredential.Initializer initializer =
new ServiceAccountCredential.Initializer(parameters.ClientEmail)
{
Scopes = OAuthScopes
};
// Configure impersonation (if applicable).
if (!String.IsNullOrEmpty(emailToImpersonate))
{
initializer.User = emailToImpersonate;
}
// Create a service account credential object using the deserialized private key.
ServiceAccountCredential credential =
new ServiceAccountCredential(initializer.FromPrivateKey(parameters.PrivateKey));
return credential;
}
}
}