Wednesday 5 March 2014

Google Adx Seller Rest API using .NET

Introduction

Hello everyone,
Recently i have worked with Adx Seller Api using .Net, i have also prepared the sample code for you guys.
Basically i will be focusing more on how to access the Api not on the Adx Seller Program, to know about what is it, just read it here Adx Seller Program.

To access the Google data via its API we need Access Token, to obtain the access token, we need to perform OAuth with Google which i have explained in my previous blog here http://sanjaybathre.blogspot.com/2014/01/sample-for-google-analytics-api-version.html.


Adx Seller


Under this title, i will explain the parameters used by the Adx Seller Api.
I am using the latest(till now) version (v 1.1) of the API, here is the details of the API resources, and here is the Rest Api playground provided by the Google - Playground.




See the List of Metrics and Dimensions required for Api.
Install the Adx Client Library in your solution, Open the Nuget Package Manager Console, and type in the following command: 
PM> Install-Package Google.Apis.AdExchangeSeller.v1_1 -Pre


Code

//Performing Google OAuth 2.0
            UserCredential OAuthcredential;
            using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
            {
                OAuthcredential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    new[] { AdExchangeSellerService.Scope.AdexchangeSellerReadonly },
                    "user", CancellationToken.None, new FileDataStore("AdexchangeSeller.Auth.Store")).Result;
            }
//AdExchange Seller Service object
            AdExchangeSellerService service_adex = new AdExchangeSellerService(new AdExchangeSellerService.Initializer()
            {
                HttpClientInitializer = OAuthcredential,
                ApplicationName = "AdExchange API sample",
            });

            ReportsResource.GenerateRequest gr = new ReportsResource.GenerateRequest(service_adex, "2014-02-27", "2014-02-27");
            List<string> Dimensions = new List<string>
            {
                "DATE",
                //"DOMAIN_NAME",
                "AD_TAG_NAME"
            };
            List<string> Metrices = new List<string>
            {
                "INDIVIDUAL_AD_IMPRESSIONS",
                "EARNINGS"
            };
            gr.Dimension = Dimensions;
            gr.Metric = Metrices;
            gr.Sort = "DATE";
            gr.Filter = "AD_TAG_NAME=@XYZ123";
            Report data = gr.Execute();

i have attached the code sample please find it and use it, replace 
client_secrets.json file with your file.