Get Started | Ad Manager API - Google Developers

文章推薦指數: 80 %
投票人數:10人

Developers can use the Google Ad Manager API to build applications that manage inventory, create orders, pull reports, and more. The Ad ... Google AdManager API English BahasaIndonesia Deutsch Español Français Português–Brasil Русский 中文–简体 日本語 한국어 Signin Guides Reference Samples Support Google AdManager API Guides Reference Samples Support GetStarted Authentication PQLGuide ThirdParties BasicConcepts HowTo Reports Forecasts Native Programmatic BestPractices TestNetworksvs.Production Home Products AdManager API Sendfeedback GetStarted Stayorganizedwithcollections Saveandcategorizecontentbasedonyourpreferences. DeveloperscanusetheGoogleAdManagerAPItobuildapplicationsthat manageinventory,createorders,pullreports,andmore. TheAdManagerAPIusesSOAP;tohelpyougetstarted,weofferclientlibrariesforJava,.NET,Python,PHP,andRuby.To makeyourfirstAPIrequest,followthestepsbelow. GetaccesstoanAdManagernetwork Ifyoudon'talreadyhaveone,signupforanAdManager account.Youcanalsocreateatestnetworkif youwanttotesttheAPIinaseparateenvironment.Notethatyoudonotneed anAdSenseaccountfortestpurposes. Makeanoteofyournetworkcode.You'llfindthisintheURLwhenyouare loggedintoyournetwork.Forexample,intheURL https://admanager.google.com/1234#home,1234isyour networkcode. ByusingtheAdManagerAPIyouagreetotheTerms ofService. Createauthenticationcredentials YoumustauthenticateallAdManagerAPIrequestsusingOAuth2.Thesteps belowcoverthesimpleusecaseofaccessingyourownAdManagerdata. Formoredetailsandotheroptions,seeAuthentication. OpentheGoogleAPIConsoleCredentialspage. Fromtheprojectmenu,chooseCreateproject,enteranameforthe project,andoptionally,edittheprovidedProjectID.Click Create. OntheCredentialspage,selectCreatecredentials,then selectServiceaccountkey. SelectNew serviceaccount,andselectJSONasthekeytype. ClickCreatetodownloadafilecontainingaprivatekey. ConfigureyourAdManagernetwork Sign intoGoogleAdManager. Inthesidebar,clickAdmin>Globalsettings. UnderGeneralsettings>Apiaccessclickthesliderto Enabled. ClicktheSavebuttonatthebottomofthepage. Setupyourclient DownloadoneoftheAdManagerclient libraries.Thelibrariesofferwrapperfunctionsandfeaturesthatmakeit easierandfastertodevelopapplications. Thetabsbelowprovidequickstartsforcodingineachofthelanguagesfor whichthereisaclientlibrary. Java HereisabasicexamplethatshowshowtousetheJavaclient library.Formoredetailedusageinformation,referto theREADME fileintheclientlibrarydistribution. Setupyourcredentials Runthefollowingcommandinashell: curlhttps://raw.githubusercontent.com/googleads/googleads-java-lib/master/examples/admanager_axis/src/main/resources/ads.properties-o~/ads.properties Openthe~/ads.propertiesfileandpopulatethefollowingfields: [...] api.admanager.applicationName=INSERT_APPLICATION_NAME_HERE api.admanager.jsonKeyFilePath=INSERT_PATH_TO_JSON_KEY_FILE_HERE api.admanager.networkCode=INSERT_NETWORK_CODE_HERE [...] Specifydependencies Edityourpom.xmlfileandaddthefollowingtothe dependenciestag.Youcanfindthelatestversionnumberon Github. com.google.api-ads ads-lib RELEASE com.google.api-ads dfp-axis RELEASE Writesomecodeandmakearequest! importcom.google.api.ads.common.lib.auth.OfflineCredentials; importcom.google.api.ads.common.lib.auth.OfflineCredentials.Api; importcom.google.api.ads.admanager.axis.factory.AdManagerServices; importcom.google.api.ads.admanager.axis.v202208.Network; importcom.google.api.ads.admanager.axis.v202208.NetworkServiceInterface; importcom.google.api.ads.admanager.lib.client.AdManagerSession; importcom.google.api.client.auth.oauth2.Credential; publicclassApp{ publicstaticvoidmain(String[]args)throwsException{ CredentialoAuth2Credential=newOfflineCredentials.Builder() .forApi(Api.AD_MANAGER) .fromFile() .build() .generateCredential(); //ConstructanAdManagerSession. AdManagerSessionsession=newAdManagerSession.Builder() .fromFile() .withOAuth2Credential(oAuth2Credential) .build(); //ConstructaGoogleAdManagerservicefactory,whichcanonlybeusedonceper //thread,butshouldbereusedasmuchaspossible. AdManagerServicesadManagerServices=newAdManagerServices(); //Retrievetheappropriateservice NetworkServiceInterfacenetworkService=adManagerServices.get(session, NetworkServiceInterface.class); //Makearequest Networknetwork=networkService.getCurrentNetwork(); System.out.printf("Currentnetworkhasnetworkcode'%s'anddisplay"+ "name'%s'.%n",network.getNetworkCode(),network.getDisplayName()); } } ViewonGitHub Python HereisabasicexamplethatshowshowtousethePythonclientlibrary. ThePythonClientLibrarysupportsPythonv3.6+.Formoredetailedusage information,refertotheREADME fileintheclientlibrarydistribution. Installthelibraryandsetupyourcredentials. Runthefollowingcommandsinashell: pipinstallgoogleads curlhttps://raw.githubusercontent.com/googleads/googleads-python-lib/master/googleads.yaml\ -o~/googleads.yaml Setupyour~/googleads.yamlfile. Fillinthefollowingfields: ad_manager: application_name:INSERT_APPLICATION_NAME_HERE network_code:INSERT_NETWORK_CODE_HERE path_to_private_key_file:INSERT_PATH_TO_FILE_HERE Runsomecodeandmakearequest. #Importthelibrary. fromgoogleadsimportad_manager #Initializeaclientobject,bydefaultusesthecredentialsin~/googleads.yaml. client=ad_manager.AdManagerClient.LoadFromStorage() #Initializeaservice. network_service=client.GetService('NetworkService',version='v202208') #Makearequest. current_network=network_service.getCurrentNetwork() print("Currentnetworkhasnetworkcode'%s'anddisplayname'%s'."% (current_network['networkCode'],current_network['displayName'])) ViewonGitHub PHP Hereisabasicexamplethatshowshowtousethe PHPclientlibrary. Installthelibraryandsetupyourcredentials. Runthefollowingcommandsinashelltoinstalltheclientlibraryanddownloadthe adsapi_php.ini filetoyourhomedirectory: composerrequiregoogleads/googleads-php-lib curlhttps://raw.githubusercontent.com/googleads/googleads-php-lib/master/examples/AdManager/adsapi_php.ini-o~/adsapi_php.ini Setupyour~/adsapi_php.inifile. Fillinthefollowingfields: [AD_MANAGER] networkCode="INSERT_NETWORK_CODE_HERE" applicationName="INSERT_APPLICATION_NAME_HERE" [OAUTH2] jsonKeyFilePath="INSERT_ABSOLUTE_PATH_TO_OAUTH2_JSON_KEY_FILE_HERE" scopes="https://www.googleapis.com/auth/dfp" Runsomecodeandmakearequest! Thisexamplecodemustberunfromthecommandline,notabrowser. Thefilecontainingthiscodemustresideintherootoftheprojectdirectory, wherecomposerrequirewasrun. fromFile() ->build(); //ConstructanAPIsessionconfiguredfromapropertiesfileandtheOAuth2 //credentialsabove. $session=(newAdManagerSessionBuilder()) ->fromFile() ->withOAuth2Credential($oAuth2Credential) ->build(); //Getaservice. $serviceFactory=newServiceFactory(); $networkService=$serviceFactory->createNetworkService($session); //Makearequest $network=$networkService->getCurrentNetwork(); printf( "Networkwithcode%danddisplayname'%s'wasfound.\n", $network->getNetworkCode(), $network->getDisplayName() ); ViewonGitHub .NET Hereisabasicexamplethatshowshowtousethe.NETclient library Createanewproject OpenVisualStudioandcreateanewproject(i.e.ConsoleApplication). Addrequiredlibraryreferencestoyourproject AddanugetdependencyforGoogle.Dfp. SetupyourApp.config Copysrc\App.configtoyourprojectdirectoryandaddittoyourproject.IfyourapplicationhasitsownApp.config,thenyoucancopythefollowingnodesintoyourApp.config: configuration/AdManagerApi configuration/configSections/section[name="AdManagerApi"] configuration/system.net Setupcredentials OpenApp.configandeditthefollowingkeys: Makeacalltothelibrary YoucancallthelibraryasshowninthefollowingC#codesnippet ViewonGitHub AdManagerUseruser=newAdManagerUser(); using(InventoryServiceinventoryService=user.GetService()) { //Createastatementtoselectadunits. intpageSize=StatementBuilder.SUGGESTED_PAGE_LIMIT; StatementBuilderstatementBuilder= newStatementBuilder().OrderBy("idASC").Limit(pageSize); //Retrieveasmallamountofadunitsatatime,pagingthroughuntilall //adunitshavebeenretrieved. inttotalResultSetSize=0; do { AdUnitPagepage= inventoryService.getAdUnitsByStatement(statementBuilder.ToStatement()); //Printoutsomeinformationforeachadunit. if(page.results!=null) { totalResultSetSize=page.totalResultSetSize; inti=page.startIndex; foreach(AdUnitadUnitinpage.results) { Console.WriteLine( "{0})AdunitwithID\"{1}\"andname\"{2}\"wasfound.",i++, adUnit.id,adUnit.name); } } statementBuilder.IncreaseOffsetBy(pageSize); }while(statementBuilder.GetOffset()



請為這篇文章評分?