CINXE.COM
Authorize Requests | Campaign Manager 360 | Google for Developers
<!doctype html> <html lang="en" dir="ltr"> <head> <meta name="google-signin-client-id" content="721724668570-nbkv1cfusk7kk4eni4pjvepaus73b13t.apps.googleusercontent.com"> <meta name="google-signin-scope" content="profile email https://www.googleapis.com/auth/developerprofiles https://www.googleapis.com/auth/developerprofiles.award"> <meta property="og:site_name" content="Google for Developers"> <meta property="og:type" content="website"><meta name="theme-color" content="#1e8e3e"><meta charset="utf-8"> <meta content="IE=Edge" http-equiv="X-UA-Compatible"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="manifest" href="/_pwa/developers/manifest.json" crossorigin="use-credentials"> <link rel="preconnect" href="//www.gstatic.com" crossorigin> <link rel="preconnect" href="//fonts.gstatic.com" crossorigin> <link rel="preconnect" href="//fonts.googleapis.com" crossorigin> <link rel="preconnect" href="//apis.google.com" crossorigin> <link rel="preconnect" href="//www.google-analytics.com" crossorigin><link rel="stylesheet" href="//fonts.googleapis.com/css?family=Google+Sans:400,500|Roboto:400,400italic,500,500italic,700,700italic|Roboto+Mono:400,500,700&display=swap"> <link rel="stylesheet" href="//fonts.googleapis.com/css2?family=Material+Icons&family=Material+Symbols+Outlined&display=block"><link rel="stylesheet" href="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/developers/css/app.css"> <link rel="shortcut icon" href="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/developers/images/favicon-new.png"> <link rel="apple-touch-icon" href="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/developers/images/touchicon-180-new.png"><link rel="canonical" href="https://developers.google.com/doubleclick-advertisers/authorizing"><link rel="search" type="application/opensearchdescription+xml" title="Google for Developers" href="https://developers.google.com/s/opensearch.xml"> <link rel="alternate" hreflang="en" href="https://developers.google.com/doubleclick-advertisers/authorizing" /><link rel="alternate" hreflang="x-default" href="https://developers.google.com/doubleclick-advertisers/authorizing" /><link rel="alternate" hreflang="ar" href="https://developers.google.com/doubleclick-advertisers/authorizing?hl=ar" /><link rel="alternate" hreflang="bn" href="https://developers.google.com/doubleclick-advertisers/authorizing?hl=bn" /><link rel="alternate" hreflang="zh-Hans" href="https://developers.google.com/doubleclick-advertisers/authorizing?hl=zh-cn" /><link rel="alternate" hreflang="zh-Hant" href="https://developers.google.com/doubleclick-advertisers/authorizing?hl=zh-tw" /><link rel="alternate" hreflang="fa" href="https://developers.google.com/doubleclick-advertisers/authorizing?hl=fa" /><link rel="alternate" hreflang="fr" href="https://developers.google.com/doubleclick-advertisers/authorizing?hl=fr" /><link rel="alternate" hreflang="de" href="https://developers.google.com/doubleclick-advertisers/authorizing?hl=de" /><link rel="alternate" hreflang="he" href="https://developers.google.com/doubleclick-advertisers/authorizing?hl=he" /><link rel="alternate" hreflang="hi" href="https://developers.google.com/doubleclick-advertisers/authorizing?hl=hi" /><link rel="alternate" hreflang="id" href="https://developers.google.com/doubleclick-advertisers/authorizing?hl=id" /><link rel="alternate" hreflang="it" href="https://developers.google.com/doubleclick-advertisers/authorizing?hl=it" /><link rel="alternate" hreflang="ja" href="https://developers.google.com/doubleclick-advertisers/authorizing?hl=ja" /><link rel="alternate" hreflang="ko" href="https://developers.google.com/doubleclick-advertisers/authorizing?hl=ko" /><link rel="alternate" hreflang="pl" href="https://developers.google.com/doubleclick-advertisers/authorizing?hl=pl" /><link rel="alternate" hreflang="pt-BR" href="https://developers.google.com/doubleclick-advertisers/authorizing?hl=pt-br" /><link rel="alternate" hreflang="ru" href="https://developers.google.com/doubleclick-advertisers/authorizing?hl=ru" /><link rel="alternate" hreflang="es-419" href="https://developers.google.com/doubleclick-advertisers/authorizing?hl=es-419" /><link rel="alternate" hreflang="th" href="https://developers.google.com/doubleclick-advertisers/authorizing?hl=th" /><link rel="alternate" hreflang="tr" href="https://developers.google.com/doubleclick-advertisers/authorizing?hl=tr" /><link rel="alternate" hreflang="vi" href="https://developers.google.com/doubleclick-advertisers/authorizing?hl=vi" /><title>Authorize Requests | Campaign Manager 360 | Google for Developers</title> <meta property="og:title" content="Authorize Requests | Campaign Manager 360 | Google for Developers"><meta property="og:url" content="https://developers.google.com/doubleclick-advertisers/authorizing"><meta property="og:image" content="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/developers/images/opengraph/google-green.png"> <meta property="og:image:width" content="1200"> <meta property="og:image:height" content="675"><meta property="og:locale" content="en"><meta name="twitter:card" content="summary_large_image"><script type="application/ld+json"> { "@context": "https://schema.org", "@type": "Article", "headline": "Authorize Requests" } </script><script type="application/ld+json"> { "@context": "https://schema.org", "@type": "BreadcrumbList", "itemListElement": [{ "@type": "ListItem", "position": 1, "name": "Campaign Manager 360", "item": "https://developers.google.com/doubleclick-advertisers" },{ "@type": "ListItem", "position": 2, "name": "Authorize Requests", "item": "https://developers.google.com/doubleclick-advertisers/authorizing" }] } </script> <link rel="stylesheet" href="/extras.css"></head> <body class="" template="page" theme="google-green" type="article" layout="docs" display-toc pending> <devsite-progress type="indeterminate" id="app-progress"></devsite-progress> <section class="devsite-wrapper"> <devsite-cookie-notification-bar></devsite-cookie-notification-bar><devsite-header role="banner"> <div class="devsite-header--inner nocontent"> <div class="devsite-top-logo-row-wrapper-wrapper"> <div class="devsite-top-logo-row-wrapper"> <div class="devsite-top-logo-row"> <button type="button" id="devsite-hamburger-menu" class="devsite-header-icon-button button-flat material-icons gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Navigation menu button" visually-hidden aria-label="Open menu"> </button> <div class="devsite-product-name-wrapper"> <a href="https://developers.google.com/doubleclick-advertisers"> <div class="devsite-product-logo-container" size="medium" > <picture> <img class="devsite-product-logo" alt="Campaign Manager 360" src="https://developers.google.com/static/ads/images/logo_campaign_manager_192px.svg" srcset=" /static/ads/images/logo_campaign_manager_192px.svg" sizes="64px" loading="lazy" > </picture> </div> </a> <span class="devsite-product-name"> <ul class="devsite-breadcrumb-list" > <li class="devsite-breadcrumb-item "> <a href="https://developers.google.com/doubleclick-advertisers" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Upper Header" data-value="1" track-type="globalNav" track-name="breadcrumb" track-metadata-position="1" track-metadata-eventdetail="Campaign Manager 360" > Campaign Manager 360 </a> </li> </ul> </span> </div> <div class="devsite-top-logo-row-middle"> <div class="devsite-header-upper-tabs"> <devsite-tabs class="upper-tabs"> <nav class="devsite-tabs-wrapper" aria-label="Upper tabs"> <tab class="devsite-active"> <a href="https://developers.google.com/doubleclick-advertisers/getting_started" track-metadata-eventdetail="https://developers.google.com/doubleclick-advertisers/getting_started" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - api" track-metadata-module="primary nav" aria-label="API, selected" data-category="Site-Wide Custom Events" data-label="Tab: API" track-name="api" > API </a> </tab> <tab > <a href="https://developers.google.com/doubleclick-advertisers/dtv2/overview" track-metadata-eventdetail="https://developers.google.com/doubleclick-advertisers/dtv2/overview" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - data transfer" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Data Transfer" track-name="data transfer" > Data Transfer </a> </tab> <tab > <a href="https://developers.google.com/doubleclick-advertisers/sheet/overview" track-metadata-eventdetail="https://developers.google.com/doubleclick-advertisers/sheet/overview" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - bulk sheet" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Bulk Sheet" track-name="bulk sheet" > Bulk Sheet </a> </tab> </nav> </devsite-tabs> </div> <devsite-search enable-signin enable-search enable-suggestions enable-query-completion project-name="Campaign Manager 360" tenant-name="Google for Developers" project-scope="/doubleclick-advertisers" url-scoped="https://developers.google.com/s/results/doubleclick-advertisers" > <form class="devsite-search-form" action="https://developers.google.com/s/results" method="GET"> <div class="devsite-search-container"> <button type="button" search-open class="devsite-search-button devsite-header-icon-button button-flat material-icons" aria-label="Open search"></button> <div class="devsite-searchbox"> <input aria-activedescendant="" aria-autocomplete="list" aria-label="Search" aria-expanded="false" aria-haspopup="listbox" autocomplete="off" class="devsite-search-field devsite-search-query" name="q" placeholder="Search" role="combobox" type="text" value="" > <div class="devsite-search-image material-icons" aria-hidden="true"> </div> <div class="devsite-search-shortcut-icon-container" aria-hidden="true"> <kbd class="devsite-search-shortcut-icon">/</kbd> </div> </div> </div> </form> <button type="button" search-close class="devsite-search-button devsite-header-icon-button button-flat material-icons" aria-label="Close search"></button> </devsite-search> </div> <devsite-language-selector> <ul role="presentation"> <li role="presentation"> <a role="menuitem" lang="en" >English</a> </li> <li role="presentation"> <a role="menuitem" lang="de" >Deutsch</a> </li> <li role="presentation"> <a role="menuitem" lang="es" >Español</a> </li> <li role="presentation"> <a role="menuitem" lang="es_419" >Español – América Latina</a> </li> <li role="presentation"> <a role="menuitem" lang="fr" >Français</a> </li> <li role="presentation"> <a role="menuitem" lang="id" >Indonesia</a> </li> <li role="presentation"> <a role="menuitem" lang="it" >Italiano</a> </li> <li role="presentation"> <a role="menuitem" lang="pl" >Polski</a> </li> <li role="presentation"> <a role="menuitem" lang="pt_br" >Português – Brasil</a> </li> <li role="presentation"> <a role="menuitem" lang="vi" >Tiếng Việt</a> </li> <li role="presentation"> <a role="menuitem" lang="tr" >Türkçe</a> </li> <li role="presentation"> <a role="menuitem" lang="ru" >Русский</a> </li> <li role="presentation"> <a role="menuitem" lang="he" >עברית</a> </li> <li role="presentation"> <a role="menuitem" lang="ar" >العربيّة</a> </li> <li role="presentation"> <a role="menuitem" lang="fa" >فارسی</a> </li> <li role="presentation"> <a role="menuitem" lang="hi" >हिंदी</a> </li> <li role="presentation"> <a role="menuitem" lang="bn" >বাংলা</a> </li> <li role="presentation"> <a role="menuitem" lang="th" >ภาษาไทย</a> </li> <li role="presentation"> <a role="menuitem" lang="zh_cn" >中文 – 简体</a> </li> <li role="presentation"> <a role="menuitem" lang="zh_tw" >中文 – 繁體</a> </li> <li role="presentation"> <a role="menuitem" lang="ja" >日本語</a> </li> <li role="presentation"> <a role="menuitem" lang="ko" >한국어</a> </li> </ul> </devsite-language-selector> <devsite-user enable-profiles fp-auth id="devsite-user"> <span class="button devsite-top-button" aria-hidden="true" visually-hidden>Sign in</span> </devsite-user> </div> </div> </div> <div class="devsite-collapsible-section "> <div class="devsite-header-background"> <div class="devsite-product-id-row" > <div class="devsite-product-description-row"> <ul class="devsite-breadcrumb-list" > <li class="devsite-breadcrumb-item "> <a href="https://developers.google.com/doubleclick-advertisers/getting_started" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Lower Header" data-value="1" track-type="globalNav" track-name="breadcrumb" track-metadata-position="1" track-metadata-eventdetail="" > Campaign Manager 360 API </a> </li> </ul> </div> </div> <div class="devsite-doc-set-nav-row"> <devsite-tabs class="lower-tabs"> <nav class="devsite-tabs-wrapper" aria-label="Lower tabs"> <tab class="devsite-active"> <a href="https://developers.google.com/doubleclick-advertisers/getting_started" track-metadata-eventdetail="https://developers.google.com/doubleclick-advertisers/getting_started" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - guides" track-metadata-module="primary nav" aria-label="Guides, selected" data-category="Site-Wide Custom Events" data-label="Tab: Guides" track-name="guides" > Guides </a> </tab> <tab > <a href="https://developers.google.com/doubleclick-advertisers/rel_notes" track-metadata-eventdetail="https://developers.google.com/doubleclick-advertisers/rel_notes" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - reference" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Reference" track-name="reference" > Reference </a> </tab> <tab > <a href="https://developers.google.com/doubleclick-advertisers/libraries" track-metadata-eventdetail="https://developers.google.com/doubleclick-advertisers/libraries" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - samples" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Samples" track-name="samples" > Samples </a> </tab> <tab > <a href="https://developers.google.com/doubleclick-advertisers/get-support" track-metadata-eventdetail="https://developers.google.com/doubleclick-advertisers/get-support" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - support" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Support" track-name="support" > Support </a> </tab> </nav> </devsite-tabs> </div> </div> </div> </div> </devsite-header> <devsite-book-nav scrollbars > <div class="devsite-book-nav-filter" > <span class="filter-list-icon material-icons" aria-hidden="true"></span> <input type="text" placeholder="Filter" aria-label="Type to filter" role="searchbox"> <span class="filter-clear-button hidden" data-title="Clear filter" aria-label="Clear filter" role="button" tabindex="0"></span> </div> <nav class="devsite-book-nav devsite-nav nocontent" aria-label="Side menu"> <div class="devsite-mobile-header"> <button type="button" id="devsite-close-nav" class="devsite-header-icon-button button-flat material-icons gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Close navigation" aria-label="Close navigation"> </button> <div class="devsite-product-name-wrapper"> <a href="https://developers.google.com/doubleclick-advertisers"> <div class="devsite-product-logo-container" size="medium" > <picture> <img class="devsite-product-logo" alt="Campaign Manager 360" src="https://developers.google.com/static/ads/images/logo_campaign_manager_192px.svg" srcset=" /static/ads/images/logo_campaign_manager_192px.svg" sizes="64px" loading="lazy" > </picture> </div> </a> <span class="devsite-product-name"> <ul class="devsite-breadcrumb-list" > <li class="devsite-breadcrumb-item "> <a href="https://developers.google.com/doubleclick-advertisers" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Upper Header" data-value="1" track-type="globalNav" track-name="breadcrumb" track-metadata-position="1" track-metadata-eventdetail="Campaign Manager 360" > Campaign Manager 360 </a> </li> </ul> </span> </div> </div> <div class="devsite-book-nav-wrapper"> <div class="devsite-mobile-nav-top"> <ul class="devsite-nav-list"> <li class="devsite-nav-item"> <a href="/doubleclick-advertisers/getting_started" class="devsite-nav-title gc-analytics-event devsite-nav-active" data-category="Site-Wide Custom Events" data-label="Tab: API" track-name="api" data-category="Site-Wide Custom Events" data-label="Responsive Tab: API" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > API </span> </a> <ul class="devsite-nav-responsive-tabs"> <li class="devsite-nav-item"> <a href="/doubleclick-advertisers/getting_started" class="devsite-nav-title gc-analytics-event devsite-nav-has-children devsite-nav-active" data-category="Site-Wide Custom Events" data-label="Tab: Guides" track-name="guides" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Guides" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip menu="_book"> Guides </span> <span class="devsite-nav-icon material-icons" data-icon="forward" menu="_book"> </span> </a> </li> <li class="devsite-nav-item"> <a href="/doubleclick-advertisers/rel_notes" class="devsite-nav-title gc-analytics-event devsite-nav-has-children " data-category="Site-Wide Custom Events" data-label="Tab: Reference" track-name="reference" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Reference" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Reference </span> <span class="devsite-nav-icon material-icons" data-icon="forward" > </span> </a> </li> <li class="devsite-nav-item"> <a href="/doubleclick-advertisers/libraries" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Tab: Samples" track-name="samples" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Samples" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Samples </span> </a> </li> <li class="devsite-nav-item"> <a href="/doubleclick-advertisers/get-support" class="devsite-nav-title gc-analytics-event devsite-nav-has-children " data-category="Site-Wide Custom Events" data-label="Tab: Support" track-name="support" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Support" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Support </span> <span class="devsite-nav-icon material-icons" data-icon="forward" > </span> </a> </li> </ul> </li> <li class="devsite-nav-item"> <a href="/doubleclick-advertisers/dtv2/overview" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Tab: Data Transfer" track-name="data transfer" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Data Transfer" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Data Transfer </span> </a> </li> <li class="devsite-nav-item"> <a href="/doubleclick-advertisers/sheet/overview" class="devsite-nav-title gc-analytics-event devsite-nav-has-children " data-category="Site-Wide Custom Events" data-label="Tab: Bulk Sheet" track-name="bulk sheet" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Bulk Sheet" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Bulk Sheet </span> <span class="devsite-nav-icon material-icons" data-icon="forward" > </span> </a> </li> </ul> </div> <div class="devsite-mobile-nav-bottom"> <ul class="devsite-nav-list" menu="_book"> <li class="devsite-nav-item"><a href="/doubleclick-advertisers/getting_started" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /doubleclick-advertisers/getting_started" track-type="bookNav" track-name="click" track-metadata-eventdetail="/doubleclick-advertisers/getting_started" ><span class="devsite-nav-text" tooltip>Get Started</span></a></li> <li class="devsite-nav-item"><a href="/doubleclick-advertisers/libraries" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /doubleclick-advertisers/libraries" track-type="bookNav" track-name="click" track-metadata-eventdetail="/doubleclick-advertisers/libraries" ><span class="devsite-nav-text" tooltip>Client Libraries and Samples</span></a></li> <li class="devsite-nav-item devsite-nav-expandable devsite-nav-accordion"><div class="devsite-expandable-nav"> <a class="devsite-nav-toggle" aria-hidden="true"></a><div class="devsite-nav-title devsite-nav-title-no-path" tabindex="0" role="button"> <span class="devsite-nav-text" tooltip>Basic Concepts</span> </div><ul class="devsite-nav-section"><li class="devsite-nav-item"><a href="/doubleclick-advertisers/authorizing" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /doubleclick-advertisers/authorizing" track-type="bookNav" track-name="click" track-metadata-eventdetail="/doubleclick-advertisers/authorizing" ><span class="devsite-nav-text" tooltip>Authorize Requests</span></a></li><li class="devsite-nav-item"><a href="/doubleclick-advertisers/performance" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /doubleclick-advertisers/performance" track-type="bookNav" track-name="click" track-metadata-eventdetail="/doubleclick-advertisers/performance" ><span class="devsite-nav-text" tooltip>Improve Performance</span></a></li><li class="devsite-nav-item"><a href="/doubleclick-advertisers/guides/migration" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /doubleclick-advertisers/guides/migration" track-type="bookNav" track-name="click" track-metadata-eventdetail="/doubleclick-advertisers/guides/migration" ><span class="devsite-nav-text" tooltip>Migrate to a New Version</span></a></li><li class="devsite-nav-item"><a href="/doubleclick-advertisers/service_accounts" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /doubleclick-advertisers/service_accounts" track-type="bookNav" track-name="click" track-metadata-eventdetail="/doubleclick-advertisers/service_accounts" ><span class="devsite-nav-text" tooltip>Service Accounts</span></a></li></ul></div></li> <li class="devsite-nav-item devsite-nav-expandable devsite-nav-accordion"><div class="devsite-expandable-nav"> <a class="devsite-nav-toggle" aria-hidden="true"></a><div class="devsite-nav-title devsite-nav-title-no-path" tabindex="0" role="button"> <span class="devsite-nav-text" tooltip>Conversions</span> </div><ul class="devsite-nav-section"><li class="devsite-nav-item"><a href="/doubleclick-advertisers/guides/conversions_overview" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /doubleclick-advertisers/guides/conversions_overview" track-type="bookNav" track-name="click" track-metadata-eventdetail="/doubleclick-advertisers/guides/conversions_overview" ><span class="devsite-nav-text" tooltip>Overview</span></a></li><li class="devsite-nav-item"><a href="/doubleclick-advertisers/guides/conversions_upload" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /doubleclick-advertisers/guides/conversions_upload" track-type="bookNav" track-name="click" track-metadata-eventdetail="/doubleclick-advertisers/guides/conversions_upload" ><span class="devsite-nav-text" tooltip>Upload Conversions</span></a></li><li class="devsite-nav-item"><a href="/doubleclick-advertisers/guides/conversions_update" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /doubleclick-advertisers/guides/conversions_update" track-type="bookNav" track-name="click" track-metadata-eventdetail="/doubleclick-advertisers/guides/conversions_update" ><span class="devsite-nav-text" tooltip>Edit Conversions</span></a></li><li class="devsite-nav-item"><a href="/doubleclick-advertisers/guides/conversions_ec" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /doubleclick-advertisers/guides/conversions_ec" track-type="bookNav" track-name="click" track-metadata-eventdetail="/doubleclick-advertisers/guides/conversions_ec" ><span class="devsite-nav-text" tooltip>Enhanced Conversions</span></a></li><li class="devsite-nav-item"><a href="/doubleclick-advertisers/guides/conversions_faq" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /doubleclick-advertisers/guides/conversions_faq" track-type="bookNav" track-name="click" track-metadata-eventdetail="/doubleclick-advertisers/guides/conversions_faq" ><span class="devsite-nav-text" tooltip>FAQ</span></a></li></ul></div></li> <li class="devsite-nav-item devsite-nav-expandable devsite-nav-accordion"><div class="devsite-expandable-nav"> <a class="devsite-nav-toggle" aria-hidden="true"></a><div class="devsite-nav-title devsite-nav-title-no-path" tabindex="0" role="button"> <span class="devsite-nav-text" tooltip>Reporting</span> </div><ul class="devsite-nav-section"><li class="devsite-nav-item"><a href="/doubleclick-advertisers/guides/reporting_best_practices" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /doubleclick-advertisers/guides/reporting_best_practices" track-type="bookNav" track-name="click" track-metadata-eventdetail="/doubleclick-advertisers/guides/reporting_best_practices" ><span class="devsite-nav-text" tooltip>Reporting Best Practices</span></a></li><li class="devsite-nav-item"><a href="/doubleclick-advertisers/guides/create_reports" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /doubleclick-advertisers/guides/create_reports" track-type="bookNav" track-name="click" track-metadata-eventdetail="/doubleclick-advertisers/guides/create_reports" ><span class="devsite-nav-text" tooltip>Create and Update Reports</span></a></li><li class="devsite-nav-item"><a href="/doubleclick-advertisers/guides/run_reports" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /doubleclick-advertisers/guides/run_reports" track-type="bookNav" track-name="click" track-metadata-eventdetail="/doubleclick-advertisers/guides/run_reports" ><span class="devsite-nav-text" tooltip>Find and Run Reports</span></a></li><li class="devsite-nav-item"><a href="/doubleclick-advertisers/guides/download_reports" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /doubleclick-advertisers/guides/download_reports" track-type="bookNav" track-name="click" track-metadata-eventdetail="/doubleclick-advertisers/guides/download_reports" ><span class="devsite-nav-text" tooltip>Download Report Files</span></a></li><li class="devsite-nav-item"><a href="/doubleclick-advertisers/guides/synchronousreports" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /doubleclick-advertisers/guides/synchronousreports" track-type="bookNav" track-name="click" track-metadata-eventdetail="/doubleclick-advertisers/guides/synchronousreports" ><span class="devsite-nav-text" tooltip>Synchronous Reports</span></a></li></ul></div></li> <li class="devsite-nav-item devsite-nav-expandable devsite-nav-accordion"><div class="devsite-expandable-nav"> <a class="devsite-nav-toggle" aria-hidden="true"></a><div class="devsite-nav-title devsite-nav-title-no-path" tabindex="0" role="button"> <span class="devsite-nav-text" tooltip>Trafficking</span> </div><ul class="devsite-nav-section"><li class="devsite-nav-item"><a href="/doubleclick-advertisers/guides/trafficking_overview" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /doubleclick-advertisers/guides/trafficking_overview" track-type="bookNav" track-name="click" track-metadata-eventdetail="/doubleclick-advertisers/guides/trafficking_overview" ><span class="devsite-nav-text" tooltip>Overview</span></a></li><li class="devsite-nav-item"><a href="/doubleclick-advertisers/trafficking_tasks" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /doubleclick-advertisers/trafficking_tasks" track-type="bookNav" track-name="click" track-metadata-eventdetail="/doubleclick-advertisers/trafficking_tasks" ><span class="devsite-nav-text" tooltip>Common Trafficking Tasks</span></a></li></ul></div></li> </ul> </div> </div> </nav> </devsite-book-nav> <section id="gc-wrapper"> <main role="main" class="devsite-main-content" has-book-nav has-sidebar > <div class="devsite-sidebar"> <div class="devsite-sidebar-content"> <devsite-toc class="devsite-nav" role="navigation" aria-label="On this page" depth="2" scrollbars ></devsite-toc> <devsite-recommendations-sidebar class="nocontent devsite-nav"> </devsite-recommendations-sidebar> </div> </div> <devsite-content> <article class="devsite-article"> <div class="devsite-article-meta nocontent" role="navigation"> <ul class="devsite-breadcrumb-list" aria-label="Breadcrumb"> <li class="devsite-breadcrumb-item "> <a href="https://developers.google.com/" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Breadcrumbs" data-value="1" track-type="globalNav" track-name="breadcrumb" track-metadata-position="1" track-metadata-eventdetail="" > Home </a> </li> <li class="devsite-breadcrumb-item "> <div class="devsite-breadcrumb-guillemet material-icons" aria-hidden="true"></div> <a href="https://developers.google.com/products" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Breadcrumbs" data-value="2" track-type="globalNav" track-name="breadcrumb" track-metadata-position="2" track-metadata-eventdetail="" > Products </a> </li> <li class="devsite-breadcrumb-item "> <div class="devsite-breadcrumb-guillemet material-icons" aria-hidden="true"></div> <a href="https://developers.google.com/ads" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Breadcrumbs" data-value="3" track-type="globalNav" track-name="breadcrumb" track-metadata-position="3" track-metadata-eventdetail="Google Advertising Platforms" > Ads </a> </li> <li class="devsite-breadcrumb-item "> <div class="devsite-breadcrumb-guillemet material-icons" aria-hidden="true"></div> <a href="https://developers.google.com/doubleclick-advertisers" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Breadcrumbs" data-value="4" track-type="globalNav" track-name="breadcrumb" track-metadata-position="4" track-metadata-eventdetail="Campaign Manager 360" > Campaign Manager 360 </a> </li> <li class="devsite-breadcrumb-item "> <div class="devsite-breadcrumb-guillemet material-icons" aria-hidden="true"></div> <a href="https://developers.google.com/doubleclick-advertisers/getting_started" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Breadcrumbs" data-value="5" track-type="globalNav" track-name="breadcrumb" track-metadata-position="5" track-metadata-eventdetail="" > API </a> </li> </ul> <devsite-thumb-rating position="header"> </devsite-thumb-rating> </div> <devsite-feedback position="header" project-name="Campaign Manager 360" product-id="82128" bucket="doubleclick-advertisers-reporting" context="" version="t-devsite-webserver-20241114-r00-rc02.464922260396498922" data-label="Send Feedback Button" track-type="feedback" track-name="sendFeedbackLink" track-metadata-position="header" class="nocontent" project-icon="https://developers.google.com/static/ads/images/logo_campaign_manager_192px.svg" > <button> Send feedback </button> </devsite-feedback> <h1 class="devsite-page-title" tabindex="-1"> Authorize Requests </h1> <devsite-feature-tooltip ack-key="AckCollectionsBookmarkTooltipDismiss" analytics-category="Site-Wide Custom Events" analytics-action-show="Callout Profile displayed" analytics-action-close="Callout Profile dismissed" analytics-label="Create Collection Callout" class="devsite-page-bookmark-tooltip nocontent" dismiss-button="true" id="devsite-collections-dropdown" dismiss-button-text="Dismiss" close-button-text="Got it"> <devsite-bookmark></devsite-bookmark> <span slot="popout-heading"> Stay organized with collections </span> <span slot="popout-contents"> Save and categorize content based on your preferences. </span> </devsite-feature-tooltip> <div class="devsite-page-title-meta"><devsite-view-release-notes></devsite-view-release-notes></div> <devsite-toc class="devsite-nav" depth="2" devsite-toc-embedded > </devsite-toc> <devsite-recommendations-dropdown class="nocontent"></devsite-recommendations-dropdown> <div class="devsite-article-body clearfix "> <p>Every request your application sends to the Campaign Manager 360 API must include an authorization token. The token also identifies your application to Google.</p> <h2 id="AboutAuthorization" data-text="About authorization protocols" tabindex="-1">About authorization protocols</h2> <p>Your application must use <a href="https://developers.google.com/identity/protocols/OAuth2">OAuth 2.0</a> to authorize requests. No other authorization protocols are supported. If your application uses <a href="https://developers.google.com/identity/gsi/web">Sign In With Google</a>, some aspects of authorization are handled for you.</p> <h2 id="OAuth2Authorizing" data-text="Authorizing requests with OAuth 2.0" tabindex="-1">Authorizing requests with OAuth 2.0</h2> <p>All requests to the Campaign Manager 360 API must be authorized by an authenticated user.</p> <p>The details of the authorization process, or "flow," for OAuth 2.0 vary somewhat depending on what kind of application you're writing. The following general process applies to all application types:</p> <ol> <li>When you create your application, you register it using the <a href="https://console.cloud.google.com/">Google API Console</a>. Google then provides information you'll need later, such as a client ID and a client secret.</li> <li>Activate the Campaign Manager 360 API in the Google API Console. (If the API isn't listed in the API Console, then skip this step.)</li> <li>When your application needs access to user data, it asks Google for a particular <strong>scope</strong> of access.</li> <li>Google displays a <strong>consent screen</strong> to the user, asking them to authorize your application to request some of their data.</li> <li>If the user approves, then Google gives your application a short-lived <strong>access token</strong>.</li> <li>Your application requests user data, attaching the access token to the request.</li> <li>If Google determines that your request and the token are valid, it returns the requested data.</li> </ol> <p>Some flows include additional steps, such as using <strong>refresh tokens</strong> to acquire new access tokens. For detailed information about flows for various types of applications, see Google's <a href="https://developers.google.com/identity/protocols/OAuth2">OAuth 2.0 documentation</a>.</p> <p id="OAuth2Scope">Here's the OAuth 2.0 scope information for the Campaign Manager 360 API:</p> <table> <tr> <th>Scope</th> <th>Meaning</th> </tr> <tr> <td><code translate="no" dir="ltr">https://www.googleapis.com/auth/dfatrafficking</code></td> <td>Read/write access to Campaign Manager 360 trafficking.</td> </tr> <tr> <td><code translate="no" dir="ltr">https://www.googleapis.com/auth/dfareporting</code></td> <td>Read/write access to Campaign Manager 360 reporting.</td> </tr> <tr> <td><code translate="no" dir="ltr">https://www.googleapis.com/auth/ddmconversions</code></td> <td>Read/write access to Campaign Manager 360 offline conversions.</td> </tr> </table> <p>To request access using OAuth 2.0, your application needs the scope information, as well as information that Google supplies when you register your application (such as the client ID and the client secret).</p> <p class="note"><strong>Tip:</strong> The Google APIs client libraries can handle some of the authorization process for you. They are available for a variety of programming languages; check the <a href="/doubleclick-advertisers/libraries">page with libraries and samples</a> for more details.</p> <h2 id="examples" data-text="Examples" tabindex="-1">Examples</h2> <div class="ds-selector-tabs" data-ds-scope="code-sample"> <section><h3 id="c" data-text="C#" tabindex="-1">C#</h3><p>Below is an excerpt from our <a href="/doubleclick-advertisers/libraries">.NET sample project</a>, which uses the <a href="/api-client-library/dotnet/get_started">Google API Client Library for .NET</a> to implement the <a href="//developers.google.com/identity/protocols/OAuth2#installed">installed application flow</a>. <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="C#"><span class="devsite-syntax-cm">/*</span> <span class="devsite-syntax-cm"> * Copyright 2015 Google Inc</span> <span class="devsite-syntax-cm"> *</span> <span class="devsite-syntax-cm"> * Licensed under the Apache License, Version 2.0(the "License");</span> <span class="devsite-syntax-cm"> * you may not use this file except in compliance with the License.</span> <span class="devsite-syntax-cm"> * You may obtain a copy of the License at</span> <span class="devsite-syntax-cm"> *</span> <span class="devsite-syntax-cm"> * http://www.apache.org/licenses/LICENSE-2.0</span> <span class="devsite-syntax-cm"> *</span> <span class="devsite-syntax-cm"> * Unless required by applicable law or agreed to in writing, software</span> <span class="devsite-syntax-cm"> * distributed under the License is distributed on an "AS IS" BASIS,</span> <span class="devsite-syntax-cm"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span> <span class="devsite-syntax-cm"> * See the License for the specific language governing permissions and</span> <span class="devsite-syntax-cm"> * limitations under the License.</span> <span class="devsite-syntax-cm">*/</span> <span class="devsite-syntax-k">using</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">System</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">using</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">System.Collections.Generic</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">using</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">System.Threading</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">using</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">Google.Apis.Auth.OAuth2</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">using</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">Google.Apis.Dfareporting.v4</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">using</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">Google.Apis.Services</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">using</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">Google.Apis.Util.Store</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">namespace</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">DfaReporting.Samples</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">/// <summary></span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">/// Factory for generating DFA Reporting and Trafficking API service objects.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">/// </summary></span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">class</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nc">DfaReportingFactory</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">/// <summary></span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">/// The scopes used to make reporting and trafficking requests.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">/// </summary></span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">private</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">readonly</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">IEnumerable<string></span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">scopes</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-p">[]</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">DfareportingService</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Scope</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Dfareporting</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">DfareportingService</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Scope</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Dfatrafficking</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">DfareportingService</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Scope</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Ddmconversions</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">};</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">/// <summary></span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">/// Authorizes the application to access users' protected data.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">/// </summary></span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">private</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">ICredential</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">Authorize</span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Load application default credentials if they're available.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">ICredential</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">credential</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">LoadApplicationDefaultCredentials</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Otherwise, load credentials from the provided client secrets file.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">credential</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">==</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">null</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">credential</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">LoadUserCredentials</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"client_secrets.json"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">FileDataStore</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"DfaReporting.Samples"</span><span class="devsite-syntax-p">));</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">credential</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">/// <summary></span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">/// Attempts to load the application default credentials</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">/// </summary></span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">/// <returns>The application default credentials, or null if none were found.</returns></span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">private</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">ICredential</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">LoadApplicationDefaultCredentials</span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">try</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GoogleCredential</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">credential</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GoogleCredential</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">GetApplicationDefaultAsync</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-n">Result</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">credential</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">CreateScoped</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">scopes</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">catch</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Exception</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// No application default credentials, continue to try other options.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">null</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">/// <summary></span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">/// Attempts to load user credentials from the provided client secrets file and persists data to</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">/// the provided data store.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">/// </summary></span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">/// <returns>The user credentials.</returns></span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">/// <param name="clientSecretsFile">Path to the file containing client secrets.</param></span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">/// <param name="dataStore">The data store to use for caching credential information.</param></span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">private</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">ICredential</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">LoadUserCredentials</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">clientSecretsFile</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">IDataStore</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">dataStore</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">using</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-kt">var</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">stream</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">IO</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">FileStream</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">clientSecretsFile</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">IO</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">FileMode</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Open</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">IO</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">FileAccess</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Read</span><span class="devsite-syntax-p">))</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GoogleWebAuthorizationBroker</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">AuthorizeAsync</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GoogleClientSecrets</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Load</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">stream</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-n">Secrets</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">scopes</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"dfa-user"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">CancellationToken</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">None</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">dataStore</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-n">Result</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">/// <summary></span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">/// Initializes a <code>DfaReportingService</code> instance.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">/// </summary></span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">/// <returns>An initialized <code>DfaReportingService</code> object.</returns></span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">DfareportingService</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">getInstance</span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">ICredential</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">credential</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Authorize</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Create and return the service.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">DfareportingService</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BaseClientService</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Initializer</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">HttpClientInitializer</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">credential</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">ApplicationName</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"DFA/DCM Reporting and Trafficking API Samples"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">});</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-p">}</span></pre></devsite-code></p></section> <section><h3 id="java" data-text="Java" tabindex="-1">Java</h3><p>Below is an excerpt from our <a href="/doubleclick-advertisers/libraries">Java sample project</a>, which uses the <a href="https://developers.google.com/api-client-library/java/">Google API Client Library for Java</a> to implement the <a href="//developers.google.com/identity/protocols/OAuth2#installed">installed application flow</a>. <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Java"><span class="devsite-syntax-c1">// Copyright 2014 Google Inc. All Rights Reserved.</span> <span class="devsite-syntax-c1">//</span> <span class="devsite-syntax-c1">// Licensed under the Apache License, Version 2.0 (the "License");</span> <span class="devsite-syntax-c1">// you may not use this file except in compliance with the License.</span> <span class="devsite-syntax-c1">// You may obtain a copy of the License at</span> <span class="devsite-syntax-c1">//</span> <span class="devsite-syntax-c1">// http://www.apache.org/licenses/LICENSE-2.0</span> <span class="devsite-syntax-c1">//</span> <span class="devsite-syntax-c1">// Unless required by applicable law or agreed to in writing, software</span> <span class="devsite-syntax-c1">// distributed under the License is distributed on an "AS IS" BASIS,</span> <span class="devsite-syntax-c1">// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span> <span class="devsite-syntax-c1">// See the License for the specific language governing permissions and</span> <span class="devsite-syntax-c1">// limitations under the License.</span> <span class="devsite-syntax-kn">package</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.services.samples.dfareporting</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">java.nio.charset.StandardCharsets.UTF_8</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.client.auth.oauth2.Credential</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.client.googleapis.auth.oauth2.GoogleCredential</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.client.googleapis.util.Utils</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.client.http.HttpTransport</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.client.json.JsonFactory</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.client.util.store.DataStoreFactory</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.client.util.store.FileDataStoreFactory</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.services.dfareporting.Dfareporting</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.services.dfareporting.DfareportingScopes</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">java.io.IOException</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">java.io.Reader</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">java.nio.file.Files</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">java.nio.file.Paths</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-cm">/**</span> <span class="devsite-syntax-cm"> * Utility methods used by all DFA Reporting and Trafficking API samples.</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">class</span> <span class="devsite-syntax-nc">DfaReportingFactory</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/** Directory to store user credentials. */</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">private</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">java</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">io</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">File</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">DATA_STORE_DIR</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">java</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">io</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">File</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getProperty</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"user.home"</span><span class="devsite-syntax-p">),</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">".store/dfareporting_sample"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">private</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">HttpTransport</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">HTTP_TRANSPORT</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Utils</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getDefaultTransport</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">private</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">final</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">JsonFactory</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">JSON_FACTORY</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Utils</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getDefaultJsonFactory</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/**</span> <span class="devsite-syntax-cm"> * Authorizes the application to access users' protected data.</span> <span class="devsite-syntax-cm"> *</span> <span class="devsite-syntax-cm"> * @return An initialized {@link Credential} object.</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">private</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Credential</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">authorize</span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">throws</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Exception</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Load application default credentials if they're available.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Credential</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">credential</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">loadApplicationDefaultCredentials</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Otherwise, load credentials from the provided client secrets file.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">credential</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">==</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">clientSecretsFile</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">DfaReportingFactory</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">class</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getResource</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"/client_secrets.json"</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-na">getFile</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">credential</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">loadUserCredentials</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">clientSecretsFile</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">FileDataStoreFactory</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">DATA_STORE_DIR</span><span class="devsite-syntax-p">));</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">credential</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/**</span> <span class="devsite-syntax-cm"> * Attempts to load application default credentials.</span> <span class="devsite-syntax-cm"> *</span> <span class="devsite-syntax-cm"> * @return A {@link Credential} object initialized with application default credentials, or</span> <span class="devsite-syntax-cm"> * {@code null} if none were found.</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">private</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Credential</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">loadApplicationDefaultCredentials</span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">try</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GoogleCredential</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">credential</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GoogleCredential</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getApplicationDefault</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">credential</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">createScoped</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">DfareportingScopes</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">all</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">catch</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">IOException</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">ignored</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// No application default credentials, continue to try other options.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/**</span> <span class="devsite-syntax-cm"> * Attempts to load user credentials from the provided client secrets file and persists data to</span> <span class="devsite-syntax-cm"> * the provided data store.</span> <span class="devsite-syntax-cm"> *</span> <span class="devsite-syntax-cm"> * @param clientSecretsFile The path to the file containing client secrets.</span> <span class="devsite-syntax-cm"> * @param dataStoreFactory he data store to use for caching credential information.</span> <span class="devsite-syntax-cm"> * @return A {@link Credential} object initialized with user account credentials.</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">private</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Credential</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">loadUserCredentials</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">clientSecretsFile</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">DataStoreFactory</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">dataStoreFactory</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">throws</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Exception</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Load client secrets JSON file.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GoogleClientSecrets</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">clientSecrets</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">try</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Reader</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">reader</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Files</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">newBufferedReader</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Paths</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">get</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">clientSecretsFile</span><span class="devsite-syntax-p">),</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">UTF_8</span><span class="devsite-syntax-p">))</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">clientSecrets</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GoogleClientSecrets</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">load</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">JSON_FACTORY</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">reader</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Set up the authorization code flow.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GoogleAuthorizationCodeFlow</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">flow</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GoogleAuthorizationCodeFlow</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">HTTP_TRANSPORT</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">JSON_FACTORY</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">clientSecrets</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">DfareportingScopes</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">all</span><span class="devsite-syntax-p">())</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">setDataStoreFactory</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">dataStoreFactory</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Authorize and persist credential information to the data store.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">AuthorizationCodeInstalledApp</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">flow</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">LocalServerReceiver</span><span class="devsite-syntax-p">()).</span><span class="devsite-syntax-na">authorize</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"user"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-cm">/**</span> <span class="devsite-syntax-cm"> * Performs all necessary setup steps for running requests against the API.</span> <span class="devsite-syntax-cm"> * </span> <span class="devsite-syntax-cm"> * @return An initialized {@link Dfareporting} service object.</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Dfareporting</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">getInstance</span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">throws</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Exception</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Credential</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">credential</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">authorize</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Create Dfareporting client.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Dfareporting</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Builder</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">HTTP_TRANSPORT</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">JSON_FACTORY</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">credential</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-na">setApplicationName</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"dfareporting-java-samples"</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-p">}</span></pre></devsite-code></p></section> <section><h3 id="php" data-text="PHP" tabindex="-1">PHP</h3><p>Below is an excerpt from our <a href="/doubleclick-advertisers/libraries">PHP sample project</a>, which uses the <a href="/api-client-library/php">Google API Client Library for PHP</a> to implement the <a href="//developers.google.com/identity/protocols/OAuth2#webserver">web application flow</a>. <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="PHP"><<span class="devsite-syntax-x">?php</span> <span class="devsite-syntax-x">/*</span> <span class="devsite-syntax-x"> * Copyright 2015 Google Inc.</span> <span class="devsite-syntax-x"> *</span> <span class="devsite-syntax-x"> * Licensed under the Apache License, Version 2.0 (the "License");</span> <span class="devsite-syntax-x"> * you may not use this file except in compliance with the License.</span> <span class="devsite-syntax-x"> * You may obtain a copy of the License at</span> <span class="devsite-syntax-x"> *</span> <span class="devsite-syntax-x"> * http://www.apache.org/licenses/LICENSE-2.0</span> <span class="devsite-syntax-x"> *</span> <span class="devsite-syntax-x"> * Unless required by applicable law or agreed to in writing, software</span> <span class="devsite-syntax-x"> * distributed under the License is distributed on an "AS IS" BASIS,</span> <span class="devsite-syntax-x"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span> <span class="devsite-syntax-x"> * See the License for the specific language governing permissions and</span> <span class="devsite-syntax-x"> * limitations under the License.</span> <span class="devsite-syntax-x"> */</span> <span class="devsite-syntax-x">/**</span> <span class="devsite-syntax-x"> * Implements the examples execution flow.</span> <span class="devsite-syntax-x"> * Load this file with no parameters to get the list of available examples.</span> <span class="devsite-syntax-x"> */</span> <span class="devsite-syntax-x">require_once __DIR__ . '/vendor/autoload.php';</span> <span class="devsite-syntax-x">require_once 'htmlHelper.php';</span> <span class="devsite-syntax-x">session_start();</span> <span class="devsite-syntax-x">// Configure token storage on disk.</span> <span class="devsite-syntax-x">// If you want to store refresh tokens in a local disk file, set this to true.</span> <span class="devsite-syntax-x">define('STORE_ON_DISK', false, true);</span> <span class="devsite-syntax-x">define('TOKEN_FILENAME', 'tokens.dat', true);</span> <span class="devsite-syntax-x">// Set up authentication</span> <span class="devsite-syntax-x">$client = new Google_Client();</span> <span class="devsite-syntax-x">$client->setApplicationName(</span> <span class="devsite-syntax-x"> 'DCM/DFA Reporting and Trafficking API PHP Samples'</span> <span class="devsite-syntax-x">);</span> <span class="devsite-syntax-x">$client->addScope(Google_Service_Dfareporting::DFAREPORTING);</span> <span class="devsite-syntax-x">$client->addScope(Google_Service_Dfareporting::DFATRAFFICKING);</span> <span class="devsite-syntax-x">$client->addScope(Google_Service_Dfareporting::DDMCONVERSIONS);</span> <span class="devsite-syntax-x">$client->setAccessType('offline');</span> <span class="devsite-syntax-x">if (getenv('GOOGLE_APPLICATION_CREDENTIALS')) {</span> <span class="devsite-syntax-x"> $client->useApplicationDefaultCredentials();</span> <span class="devsite-syntax-x">} else {</span> <span class="devsite-syntax-x"> // Be sure to replace the contents of client_secrets.json with your developer</span> <span class="devsite-syntax-x"> // credentials.</span> <span class="devsite-syntax-x"> $client->setAuthConfigFile('client_secrets.json');</span> <span class="devsite-syntax-x">}</span> <span class="devsite-syntax-x">// Create service.</span> <span class="devsite-syntax-x">$service = new Google_Service_Dfareporting($client);</span> <span class="devsite-syntax-x">// If we're logging out we just need to clear our local access token.</span> <span class="devsite-syntax-x">// Note that this only logs you out of the session. If STORE_ON_DISK is</span> <span class="devsite-syntax-x">// enabled and you want to remove stored data, delete the file.</span> <span class="devsite-syntax-x">if (isset($_REQUEST['logout'])) {</span> <span class="devsite-syntax-x"> unset($_SESSION['access_token']);</span> <span class="devsite-syntax-x">}</span> <span class="devsite-syntax-x">// If we have a code back from the OAuth 2.0 flow, we need to exchange that</span> <span class="devsite-syntax-x">// with the authenticate() function. We store the resultant access token</span> <span class="devsite-syntax-x">// bundle in the session (and disk, if enabled), and redirect to this page.</span> <span class="devsite-syntax-x">if (isset($_GET['code'])) {</span> <span class="devsite-syntax-x"> $client->authenticate($_GET['code']);</span> <span class="devsite-syntax-x"> // Note that "getAccessToken" actually retrieves both the access and refresh</span> <span class="devsite-syntax-x"> // tokens, assuming both are available.</span> <span class="devsite-syntax-x"> $_SESSION['access_token'] = $client->getAccessToken();</span> <span class="devsite-syntax-x"> if (STORE_ON_DISK) {</span> <span class="devsite-syntax-x"> file_put_contents(TOKEN_FILENAME, json_encode($_SESSION['access_token']));</span> <span class="devsite-syntax-x"> }</span> <span class="devsite-syntax-x"> $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];</span> <span class="devsite-syntax-x"> header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));</span> <span class="devsite-syntax-x"> exit;</span> <span class="devsite-syntax-x">}</span> <span class="devsite-syntax-x">// If we have an access token, we can make requests, else we generate an</span> <span class="devsite-syntax-x">// authentication URL.</span> <span class="devsite-syntax-x">if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {</span> <span class="devsite-syntax-x"> $client->setAccessToken($_SESSION['access_token']);</span> <span class="devsite-syntax-x">} elseif (STORE_ON_DISK && file_exists(TOKEN_FILENAME) </span>&& <span class="devsite-syntax-x"> filesize(TOKEN_FILENAME) > 0) {</span> <span class="devsite-syntax-x"> // Note that "setAccessToken" actually sets both the access and refresh token,</span> <span class="devsite-syntax-x"> // assuming both were saved.</span> <span class="devsite-syntax-x"> $client->setAccessToken(file_get_contents(TOKEN_FILENAME));</span> <span class="devsite-syntax-x"> $_SESSION['access_token'] = $client->getAccessToken();</span> <span class="devsite-syntax-x">} else {</span> <span class="devsite-syntax-x"> // If we're doing disk storage, generate a URL that forces user approval.</span> <span class="devsite-syntax-x"> // This is the only way to guarantee we get back a refresh token.</span> <span class="devsite-syntax-x"> if (STORE_ON_DISK) {</span> <span class="devsite-syntax-x"> $client->setApprovalPrompt('force');</span> <span class="devsite-syntax-x"> }</span> <span class="devsite-syntax-x"> $authUrl = $client->createAuthUrl();</span> <span class="devsite-syntax-x">}</span> <span class="devsite-syntax-x">$pageTitle = sprintf(</span> <span class="devsite-syntax-x"> 'DCM/DFA Reporting and Trafficking API %s PHP usage samples',</span> <span class="devsite-syntax-x"> $service->version</span> <span class="devsite-syntax-x">);</span> <span class="devsite-syntax-x">printHtmlHeader($pageTitle);</span> <span class="devsite-syntax-x">if (isset($authUrl)) {</span> <span class="devsite-syntax-x"> // No access token found, show the link to generate one</span> <span class="devsite-syntax-x"> printf("<a class='login' href='%s'>Login!</a>", $authUrl);</span> <span class="devsite-syntax-x">} else {</span> <span class="devsite-syntax-x"> print "<a class='logout' href='?logout'>Logout</a>";</span> <span class="devsite-syntax-x">}</span> <span class="devsite-syntax-x">if ($client->getAccessToken()) {</span> <span class="devsite-syntax-x"> // If the action is set, dispatch the action if supported</span> <span class="devsite-syntax-x"> if (isset($_GET['action'])) {</span> <span class="devsite-syntax-x"> $action = decodeActionString($_GET['action']);</span> <span class="devsite-syntax-x"> if (!isValidAction($action)) {</span> <span class="devsite-syntax-x"> die('Unsupported action: ' . $_GET['action'] . "\n");</span> <span class="devsite-syntax-x"> }</span> <span class="devsite-syntax-x"> displayAction($action);</span> <span class="devsite-syntax-x"> } else {</span> <span class="devsite-syntax-x"> // Show the list of links to supported actions.</span> <span class="devsite-syntax-x"> printExamplesIndex(getSupportedActions());</span> <span class="devsite-syntax-x"> printHtmlFooter();</span> <span class="devsite-syntax-x"> }</span> <span class="devsite-syntax-x"> // Note that we re-store the access_token bundle, just in case anything</span> <span class="devsite-syntax-x"> // changed during the request - the main thing that might happen here is the</span> <span class="devsite-syntax-x"> // access token itself is refreshed if the application has offline access.</span> <span class="devsite-syntax-x"> $_SESSION['access_token'] = $client->getAccessToken();</span> <span class="devsite-syntax-x">}</span> <span class="devsite-syntax-x">/**</span> <span class="devsite-syntax-x"> * Displays the requested action.</span> <span class="devsite-syntax-x"> */</span> <span class="devsite-syntax-x">function displayAction($action)</span> <span class="devsite-syntax-x">{</span> <span class="devsite-syntax-x"> global $service;</span> <span class="devsite-syntax-x"> // Render the required action.</span> <span class="devsite-syntax-x"> include_once 'examples/' . $action[0] . '/' . $action[1] . '.php';</span> <span class="devsite-syntax-x"> $class = $action[1];</span> <span class="devsite-syntax-x"> $example = new $class($service);</span> <span class="devsite-syntax-x"> printHtmlHeader($example->getName());</span> <span class="devsite-syntax-x"> try {</span> <span class="devsite-syntax-x"> $example->execute();</span> <span class="devsite-syntax-x"> } catch (Google_Exception $ex) {</span> <span class="devsite-syntax-x"> print_r($ex);</span> <span class="devsite-syntax-x"> print 'An error as occurred while calling the example:<br/>';</span> <span class="devsite-syntax-x"> print $ex->getMessage();</span> <span class="devsite-syntax-x"> }</span> <span class="devsite-syntax-x"> printSampleHtmlFooter();</span> <span class="devsite-syntax-x">}</span> <span class="devsite-syntax-x">/**</span> <span class="devsite-syntax-x"> * Determines whether the requested action is in our list of supported actions.</span> <span class="devsite-syntax-x"> */</span> <span class="devsite-syntax-x">function isValidAction($action)</span> <span class="devsite-syntax-x">{</span> <span class="devsite-syntax-x"> $actions = getSupportedActions();</span> <span class="devsite-syntax-x"> if (array_key_exists($action[0], $actions)) {</span> <span class="devsite-syntax-x"> $section = $actions[$action[0]];</span> <span class="devsite-syntax-x"> if (in_array($action[1], $section)) {</span> <span class="devsite-syntax-x"> return true;</span> <span class="devsite-syntax-x"> }</span> <span class="devsite-syntax-x"> }</span> <span class="devsite-syntax-x"> return false;</span> <span class="devsite-syntax-x">}</span> <span class="devsite-syntax-x">/**</span> <span class="devsite-syntax-x"> * Decodes an action string passed as a URL parameter into a section and action</span> <span class="devsite-syntax-x"> * pair.</span> <span class="devsite-syntax-x"> */</span> <span class="devsite-syntax-x">function decodeActionString($actionString)</span> <span class="devsite-syntax-x">{</span> <span class="devsite-syntax-x"> $parts = explode(':', $actionString);</span> <span class="devsite-syntax-x"> if (count($parts) != 2) {</span> <span class="devsite-syntax-x"> die('Invalid action specified.');</span> <span class="devsite-syntax-x"> }</span> <span class="devsite-syntax-x"> return $parts;</span> <span class="devsite-syntax-x">}</span> <span class="devsite-syntax-x">/**</span> <span class="devsite-syntax-x"> * Builds an array containing the supported actions, separated into sections.</span> <span class="devsite-syntax-x"> */</span> <span class="devsite-syntax-x">function getSupportedActions()</span> <span class="devsite-syntax-x">{</span> <span class="devsite-syntax-x"> $actions = [];</span> <span class="devsite-syntax-x"> foreach (glob('examples/*/*.php') as $file) {</span> <span class="devsite-syntax-x"> $dir = dirname($file);</span> <span class="devsite-syntax-x"> $section = substr($dir, strrpos($dir, '/') + 1);</span> <span class="devsite-syntax-x"> if (!array_key_exists($section, $actions)) {</span> <span class="devsite-syntax-x"> $actions[$section] = [];</span> <span class="devsite-syntax-x"> }</span> <span class="devsite-syntax-x"> $actions[$section][] = basename($file, '.php');</span> <span class="devsite-syntax-x"> }</span> <span class="devsite-syntax-x"> return $actions;</span> <span class="devsite-syntax-x">}</span></pre></devsite-code></p></section> <section><h3 id="python" data-text="Python" tabindex="-1">Python</h3><p>Below is an excerpt from our <a href="/doubleclick-advertisers/libraries">Python sample project</a>, which uses the <a href="/api-client-library/python">Google API Client Library for Python</a> to implement the <a href="//developers.google.com/identity/protocols/OAuth2#installed">installed application flow</a>. <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Python"><span class="devsite-syntax-ch">#!/usr/bin/python</span> <span class="devsite-syntax-c1">#</span> <span class="devsite-syntax-c1"># Copyright 2015 Google Inc. All Rights Reserved.</span> <span class="devsite-syntax-c1">#</span> <span class="devsite-syntax-c1"># Licensed under the Apache License, Version 2.0 (the "License");</span> <span class="devsite-syntax-c1"># you may not use this file except in compliance with the License.</span> <span class="devsite-syntax-c1"># You may obtain a copy of the License at</span> <span class="devsite-syntax-c1">#</span> <span class="devsite-syntax-c1"># http://www.apache.org/licenses/LICENSE-2.0</span> <span class="devsite-syntax-c1">#</span> <span class="devsite-syntax-c1"># Unless required by applicable law or agreed to in writing, software</span> <span class="devsite-syntax-c1"># distributed under the License is distributed on an "AS IS" BASIS,</span> <span class="devsite-syntax-c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span> <span class="devsite-syntax-c1"># See the License for the specific language governing permissions and</span> <span class="devsite-syntax-c1"># limitations under the License.</span> <span class="devsite-syntax-sd">"""Handles common tasks across all API samples."""</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-nn">argparse</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-nn">os</span> <span class="devsite-syntax-kn">from</span> <span class="devsite-syntax-nn">googleapiclient</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-n">discovery</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-nn">httplib2</span> <span class="devsite-syntax-kn">from</span> <span class="devsite-syntax-nn">oauth2client</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-n">client</span> <span class="devsite-syntax-kn">from</span> <span class="devsite-syntax-nn">oauth2client</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-n">file</span> <span class="devsite-syntax-k">as</span> <span class="devsite-syntax-n">oauthFile</span> <span class="devsite-syntax-kn">from</span> <span class="devsite-syntax-nn">oauth2client</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-n">tools</span> <span class="devsite-syntax-n">API_NAME</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-s1">'dfareporting'</span> <span class="devsite-syntax-n">API_VERSION</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-s1">'v4'</span> <span class="devsite-syntax-n">API_SCOPES</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-p">[</span><span class="devsite-syntax-s1">'https://www.googleapis.com/auth/dfareporting'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-s1">'https://www.googleapis.com/auth/dfatrafficking'</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-s1">'https://www.googleapis.com/auth/ddmconversions'</span><span class="devsite-syntax-p">]</span> <span class="devsite-syntax-c1"># Filename used for the credential store.</span> <span class="devsite-syntax-n">CREDENTIAL_STORE_FILE</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">API_NAME</span> <span class="devsite-syntax-o">+</span> <span class="devsite-syntax-s1">'.dat'</span> <span class="devsite-syntax-k">def</span> <span class="devsite-syntax-nf">get_arguments</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">argv</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">desc</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">parents</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-kc">None</span><span class="devsite-syntax-p">):</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-sd">"""Validates and parses command line arguments.</span> <span class="devsite-syntax-sd"> Args:</span> <span class="devsite-syntax-sd"> argv: list of strings, the command-line parameters of the application.</span> <span class="devsite-syntax-sd"> desc: string, a description of the sample being executed.</span> <span class="devsite-syntax-sd"> parents: list of argparse.ArgumentParser, additional command-line parsers.</span> <span class="devsite-syntax-sd"> Returns:</span> <span class="devsite-syntax-sd"> The parsed command-line arguments.</span> <span class="devsite-syntax-sd"> """</span> <span class="devsite-syntax-c1"># Include the default oauth2client argparser</span> <span class="devsite-syntax-n">parent_parsers</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-p">[</span><span class="devsite-syntax-n">tools</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">argparser</span><span class="devsite-syntax-p">]</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-n">parents</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">parent_parsers</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">extend</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">parents</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">parser</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">argparse</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">ArgumentParser</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-n">description</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-n">desc</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">formatter_class</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-n">argparse</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">RawDescriptionHelpFormatter</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">parents</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-n">parent_parsers</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">return</span> <span class="devsite-syntax-n">parser</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">parse_args</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">argv</span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-mi">1</span><span class="devsite-syntax-p">:])</span> <span class="devsite-syntax-k">def</span> <span class="devsite-syntax-nf">load_application_default_credentials</span><span class="devsite-syntax-p">():</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-sd">"""Atempts to load application default credentials.</span> <span class="devsite-syntax-sd"> Returns:</span> <span class="devsite-syntax-sd"> A credential object initialized with application default credentials or None</span> <span class="devsite-syntax-sd"> if none were found.</span> <span class="devsite-syntax-sd"> """</span> <span class="devsite-syntax-k">try</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">credentials</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">client</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">GoogleCredentials</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">get_application_default</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-k">return</span> <span class="devsite-syntax-n">credentials</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">create_scoped</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">API_SCOPES</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">except</span> <span class="devsite-syntax-n">client</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">ApplicationDefaultCredentialsError</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-c1"># No application default credentials, continue to try other options.</span> <span class="devsite-syntax-k">pass</span> <span class="devsite-syntax-k">def</span> <span class="devsite-syntax-nf">load_user_credentials</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">client_secrets</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">storage</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">flags</span><span class="devsite-syntax-p">):</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-sd">"""Attempts to load user credentials from the provided client secrets file.</span> <span class="devsite-syntax-sd"> Args:</span> <span class="devsite-syntax-sd"> client_secrets: path to the file containing client secrets.</span> <span class="devsite-syntax-sd"> storage: the data store to use for caching credential information.</span> <span class="devsite-syntax-sd"> flags: command-line flags.</span> <span class="devsite-syntax-sd"> Returns:</span> <span class="devsite-syntax-sd"> A credential object initialized with user account credentials.</span> <span class="devsite-syntax-sd"> """</span> <span class="devsite-syntax-c1"># Set up a Flow object to be used if we need to authenticate.</span> <span class="devsite-syntax-n">flow</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">client</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">flow_from_clientsecrets</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-n">client_secrets</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">scope</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-n">API_SCOPES</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">message</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-n">tools</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">message_if_missing</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">client_secrets</span><span class="devsite-syntax-p">))</span> <span class="devsite-syntax-c1"># Retrieve credentials from storage.</span> <span class="devsite-syntax-c1"># If the credentials don't exist or are invalid run through the installed</span> <span class="devsite-syntax-c1"># client flow. The storage object will ensure that if successful the good</span> <span class="devsite-syntax-c1"># credentials will get written back to file.</span> <span class="devsite-syntax-n">credentials</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">storage</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">get</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-n">credentials</span> <span class="devsite-syntax-ow">is</span> <span class="devsite-syntax-kc">None</span> <span class="devsite-syntax-ow">or</span> <span class="devsite-syntax-n">credentials</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">invalid</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">credentials</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">tools</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">run_flow</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">flow</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">storage</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">flags</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">return</span> <span class="devsite-syntax-n">credentials</span> <span class="devsite-syntax-k">def</span> <span class="devsite-syntax-nf">setup</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">flags</span><span class="devsite-syntax-p">):</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-sd">"""Handles authentication and loading of the API.</span> <span class="devsite-syntax-sd"> Args:</span> <span class="devsite-syntax-sd"> flags: command-line flags obtained by calling ''get_arguments()''.</span> <span class="devsite-syntax-sd"> Returns:</span> <span class="devsite-syntax-sd"> An initialized service object.</span> <span class="devsite-syntax-sd"> """</span> <span class="devsite-syntax-c1"># Load application default credentials if they're available.</span> <span class="devsite-syntax-n">credentials</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">load_application_default_credentials</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-c1"># Otherwise, load credentials from the provided client secrets file.</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-n">credentials</span> <span class="devsite-syntax-ow">is</span> <span class="devsite-syntax-kc">None</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-c1"># Name of a file containing the OAuth 2.0 information for this</span> <span class="devsite-syntax-c1"># application, including client_id and client_secret, which are found</span> <span class="devsite-syntax-c1"># on the Credentials tab on the Google Developers Console.</span> <span class="devsite-syntax-n">client_secrets</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">os</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">path</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">join</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">os</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">path</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">dirname</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-vm">__file__</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-s1">'client_secrets.json'</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">storage</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">oauthFile</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">Storage</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">CREDENTIAL_STORE_FILE</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">credentials</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">load_user_credentials</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">client_secrets</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">storage</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">flags</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-c1"># Authorize HTTP object with the prepared credentials.</span> <span class="devsite-syntax-n">http</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">credentials</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">authorize</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">http</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-n">httplib2</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">Http</span><span class="devsite-syntax-p">())</span> <span class="devsite-syntax-c1"># Construct and return a service object via the discovery service.</span> <span class="devsite-syntax-k">return</span> <span class="devsite-syntax-n">discovery</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">build</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">API_NAME</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">API_VERSION</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">http</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-n">http</span><span class="devsite-syntax-p">)</span></pre></devsite-code></p></section> <section><h3 id="ruby" data-text="Ruby" tabindex="-1">Ruby</h3><p>Below is an excerpt from our <a href="/doubleclick-advertisers/libraries">Ruby sample project</a>, which uses the <a href="/api-client-library/ruby/start/get_started">Google API Client Library for Ruby</a> to implement the <a href="//developers.google.com/identity/protocols/OAuth2#installed">installed application flow</a>. <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Ruby"><span class="devsite-syntax-ch">#!/usr/bin/env ruby</span> <span class="devsite-syntax-c1">#</span> <span class="devsite-syntax-c1"># Copyright:: Copyright 2016, Google Inc. All Rights Reserved.</span> <span class="devsite-syntax-c1">#</span> <span class="devsite-syntax-c1"># License:: Licensed under the Apache License, Version 2.0 (the "License");</span> <span class="devsite-syntax-c1"># you may not use this file except in compliance with the License.</span> <span class="devsite-syntax-c1"># You may obtain a copy of the License at</span> <span class="devsite-syntax-c1">#</span> <span class="devsite-syntax-c1"># http://www.apache.org/licenses/LICENSE-2.0</span> <span class="devsite-syntax-c1">#</span> <span class="devsite-syntax-c1"># Unless required by applicable law or agreed to in writing, software</span> <span class="devsite-syntax-c1"># distributed under the License is distributed on an "AS IS" BASIS,</span> <span class="devsite-syntax-c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or</span> <span class="devsite-syntax-c1"># implied.</span> <span class="devsite-syntax-c1"># See the License for the specific language governing permissions and</span> <span class="devsite-syntax-c1"># limitations under the License.</span> <span class="devsite-syntax-c1">#</span> <span class="devsite-syntax-c1"># Handles common tasks across all DFA Reporting API samples.</span> <span class="devsite-syntax-nb">require</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'google/apis/dfareporting_v4'</span> <span class="devsite-syntax-nb">require</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'googleauth'</span> <span class="devsite-syntax-nb">require</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'googleauth/stores/file_token_store'</span> <span class="devsite-syntax-c1"># Utility methods used by all DFA Reporting and Trafficking API samples.</span> <span class="devsite-syntax-k">module</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">DfareportingUtils</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">API_NAME</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'dfareporting'</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">freeze</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">API_NAMESPACE</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">Google</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-no">Apis</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-no">DfareportingV4</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">API_SCOPES</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">[</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">API_NAMESPACE</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-no">AUTH_DDMCONVERSIONS</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">API_NAMESPACE</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-no">AUTH_DFAREPORTING</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">API_NAMESPACE</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-no">AUTH_DFATRAFFICKING</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">].</span><span class="devsite-syntax-n">freeze</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">CLIENT_SECRETS_FILE</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'client_secrets.json'</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">freeze</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">CREDENTIAL_STORE_FILE</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">#{</span><span class="devsite-syntax-no">API_NAME</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">-oauth2.yaml"</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">freeze</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">CREDENTIAL_STORE_PATH</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">File</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">dirname</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-bp">__FILE__</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># This redirect URI allows you to copy the token from the success screen.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">OAUTH_REDIRECT_URI</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'urn:ietf:wg:oauth:2.0:oob'</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">freeze</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># Handles validating command line arguments and returning them as a Hash</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">def</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nc">self</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-nf">parse_arguments</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">argument_values</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">*</span><span class="devsite-syntax-n">argument_names</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">validate_arguments</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">argument_values</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">*</span><span class="devsite-syntax-n">argument_names</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">generate_argument_map</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">argument_values</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">*</span><span class="devsite-syntax-n">argument_names</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">end</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># Validates the number of command line arguments matches what was expected</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">def</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nc">self</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-nf">validate_arguments</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">argument_values</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">*</span><span class="devsite-syntax-n">argument_names</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">argument_values</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">length</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">==</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">argument_names</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">length</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># Format the arguments for display (ie, '<profile_id>')</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">formatted_arguments</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">argument_names</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">map</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">|</span><span class="devsite-syntax-n">a</span><span class="devsite-syntax-o">|</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'<'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">a</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">to_s</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'>'</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># Display a message to the user and exit</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">puts</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">format</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'Usage: %s %s'</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-vg">$PROGRAM_NAME</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">formatted_arguments</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">join</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">' '</span><span class="devsite-syntax-p">))</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">exit</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">end</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">private_class_method</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ss">:validate_arguments</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># Converts parallel arrays of argument names and values into a single map</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">def</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nc">self</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-nf">generate_argument_map</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">argument_values</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">*</span><span class="devsite-syntax-n">argument_names</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">ret</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">argument_names</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">each_with_index</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">do</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">|</span><span class="devsite-syntax-n">arg</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">index</span><span class="devsite-syntax-o">|</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">ret</span><span class="devsite-syntax-o">[</span><span class="devsite-syntax-n">arg</span><span class="devsite-syntax-o">]</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">argument_values</span><span class="devsite-syntax-o">[</span><span class="devsite-syntax-n">index</span><span class="devsite-syntax-o">]</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">end</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">ret</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">end</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">private_class_method</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ss">:generate_argument_map</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># Handles authentication and loading of the API.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">def</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nc">self</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-nf">initialize_service</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># Uncomment the following lines to enable logging.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># log_file = File.open("#{$0}.log", 'a+')</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># log_file.sync = true</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># logger = Logger.new(log_file)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># logger.level = Logger::DEBUG</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># Google::Apis.logger = logger # Logging is set globally</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># Create an API Service object.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">service</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">create_service_object</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># Load application default credentials if they're available.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">authorization</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">authorize_application_default_credentials</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># Otherwise, load credentials from the provided client secrets file.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">authorization</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">authorize_installed_application</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">authorization</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">nil?</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># If no credentials could be loaded, return an error.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">authorization</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">nil?</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">puts</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'Could not load credentials. Enter client ID and secret from '</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">\</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'https://console.developers.google.com/ into client_secrets.json.'</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">exit</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">end</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">service</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">authorization</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">authorization</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">service</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">end</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># Returns an instance of the Dfareporting service without authentication.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">def</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nc">self</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-nf">create_service_object</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">service</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">API_NAMESPACE</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-no">DfareportingService</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">new</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">service</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">client_options</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">application_name</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Ruby </span><span class="devsite-syntax-si">#{</span><span class="devsite-syntax-no">API_NAME</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2"> samples"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">service</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">client_options</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">application_version</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'1.0.0'</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">service</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">end</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">private_class_method</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ss">:create_service_object</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># Attempts to load application default credentials and return an</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># authorization object that can be used to make requests.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">def</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nc">self</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-nf">authorize_application_default_credentials</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">Google</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-no">Auth</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">get_application_default</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-no">API_SCOPES</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">rescue</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">StandardError</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># No application default credentials, continue to try other options.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kp">nil</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">end</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">private_class_method</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ss">:authorize_application_default_credentials</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># Handles authorizing a user via the OAuth installed application flow and</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># returns an authorization object that can be used to make requests.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">def</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nc">self</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-nf">authorize_installed_application</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># Load the client secrets.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">client_id</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">load_client_secrets</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kp">nil</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">client_id</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">nil?</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># FileTokenStore stores auth credentials in a file, so they survive</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># multiple runs of the application. This avoids prompting the user for</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># authorization every time the access token expires, by remembering the</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># refresh token.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">#</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># Note: FileTokenStore is not suitable for multi-user applications.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">token_store</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">Google</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-no">Auth</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-no">Stores</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-no">FileTokenStore</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">new</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-ss">file</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">File</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">join</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-no">CREDENTIAL_STORE_PATH</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">CREDENTIAL_STORE_FILE</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">authorizer</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">Google</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-no">Auth</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-no">UserAuthorizer</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">new</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">client_id</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">API_SCOPES</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">token_store</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">authorization</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">authorizer</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">get_credentials</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'default'</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">authorization</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">nil?</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">puts</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">format</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Open this URL in your browser and authorize the application.</span><span class="devsite-syntax-se">\n\n</span><span class="devsite-syntax-s2">%s"</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">\</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-se">\n\n</span><span class="devsite-syntax-s2">Enter the authorization code:"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">authorizer</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">get_authorization_url</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-ss">base_url</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">OAUTH_REDIRECT_URI</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">code</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">STDIN</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">gets</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">chomp</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">authorization</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">authorizer</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">get_and_store_credentials_from_code</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-ss">base_url</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">OAUTH_REDIRECT_URI</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ss">code</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">code</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ss">user_id</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'default'</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">end</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">authorization</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">end</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">private_class_method</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ss">:authorize_installed_application</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">def</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nc">self</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-nf">load_client_secrets</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># Load client ID from the specified file.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">client_id</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">Google</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-no">Auth</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-no">ClientId</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">from_file</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">File</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">join</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-no">CREDENTIAL_STORE_PATH</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">CLIENT_SECRETS_FILE</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">client_id</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">id</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">start_with?</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'[[INSERT'</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">||</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">client_id</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">secret</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">start_with?</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'[[INSERT'</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kp">nil</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">end</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">client_id</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">rescue</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">StandardError</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># Unable to load client_secrets.json.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kp">nil</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">end</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">private_class_method</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ss">:load_client_secrets</span> <span class="devsite-syntax-k">end</span></pre></devsite-code></p></section> </div> </div> <devsite-recommendations display="in-page" hidden yield> </devsite-recommendations> <devsite-thumb-rating position="footer"> </devsite-thumb-rating> <devsite-feedback position="footer" project-name="Campaign Manager 360" product-id="82128" bucket="doubleclick-advertisers-reporting" context="" version="t-devsite-webserver-20241114-r00-rc02.464922260396498922" data-label="Send Feedback Button" track-type="feedback" track-name="sendFeedbackLink" track-metadata-position="footer" class="nocontent" project-icon="https://developers.google.com/static/ads/images/logo_campaign_manager_192px.svg" > <button> Send feedback </button> </devsite-feedback> <devsite-recommendations id="recommendations-link" yield></devsite-recommendations> <div class="devsite-floating-action-buttons"> </div> </article> <devsite-content-footer class="nocontent"> <p>Except as otherwise noted, the content of this page is licensed under the <a href="https://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 License</a>, and code samples are licensed under the <a href="https://www.apache.org/licenses/LICENSE-2.0">Apache 2.0 License</a>. For details, see the <a href="https://developers.google.com/site-policies">Google Developers Site Policies</a>. Java is a registered trademark of Oracle and/or its affiliates.</p> <p>Last updated 2024-09-19 UTC.</p> </devsite-content-footer> <devsite-notification > </devsite-notification> <div class="devsite-content-data"> <template class="devsite-thumb-rating-feedback"> <devsite-feedback position="thumb-rating" project-name="Campaign Manager 360" product-id="82128" bucket="doubleclick-advertisers-reporting" context="" version="t-devsite-webserver-20241114-r00-rc02.464922260396498922" data-label="Send Feedback Button" track-type="feedback" track-name="sendFeedbackLink" track-metadata-position="thumb-rating" class="nocontent" project-icon="https://developers.google.com/static/ads/images/logo_campaign_manager_192px.svg" > <button> Need to tell us more? </button> </devsite-feedback> </template> <template class="devsite-content-data-template"> [[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-09-19 UTC."],[[["All Campaign Manager 360 API requests require authorization via OAuth 2.0 using an access token."],["The authorization process involves application registration, API activation, scope definition, user consent, and token retrieval."],["Client libraries simplify authorization and are available for various programming languages (C#, Java, PHP, Python, Ruby)."],["Common scopes grant access to trafficking, reporting, and offline conversions within Campaign Manager 360."],["Refresh tokens can extend the authorization period by obtaining new access tokens when needed."]]],[]] </template> </div> </devsite-content> </main> <devsite-footer-promos class="devsite-footer"> <nav class="devsite-footer-promos nocontent" aria-label="Promotions"> <ul class="devsite-footer-promos-list"> <li class="devsite-footer-promo"> <a href="//ads-developers.googleblog.com/search/label/dfa_api" class="devsite-footer-promo-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Promo Link (index 1)" > <picture> <img class="devsite-footer-promo-icon" src="https://www.gstatic.com/images/icons/material/product/2x/blogger_64dp.png" loading="lazy" alt="Blog"> </picture> <span class="devsite-footer-promo-label"> Blog </span> </a> <div class="devsite-footer-promo-description">The latest Google Ads API Developer Blogs</div> </li> </ul> </nav> </devsite-footer-promos> <devsite-footer-linkboxes class="devsite-footer"> <nav class="devsite-footer-linkboxes nocontent" aria-label="Footer links"> <ul class="devsite-footer-linkboxes-list"> <li class="devsite-footer-linkbox "> <h3 class="devsite-footer-linkbox-heading no-link">Tools</h3> <ul class="devsite-footer-linkbox-list"> <li class="devsite-footer-linkbox-item"> <a href="/doubleclick-advertisers/libraries" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 1)" > Libraries </a> </li> <li class="devsite-footer-linkbox-item"> <a href="https://developers.google.com/apis-explorer/#p/dfareporting/v2.1/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 2)" > APIs Explorer </a> </li> </ul> </li> <li class="devsite-footer-linkbox "> <h3 class="devsite-footer-linkbox-heading no-link">Product Info</h3> <ul class="devsite-footer-linkbox-list"> <li class="devsite-footer-linkbox-item"> <a href="/doubleclick-advertisers/dfaTermsOfService" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 1)" > Terms of Service </a> </li> </ul> </li> <li class="devsite-footer-linkbox "> <h3 class="devsite-footer-linkbox-heading no-link">Developer consoles</h3> <ul class="devsite-footer-linkbox-list"> <li class="devsite-footer-linkbox-item"> <a href="//console.developers.google.com" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 1)" > Google API Console </a> </li> <li class="devsite-footer-linkbox-item"> <a href="//console.cloud.google.com" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 2)" > Google Cloud Platform Console </a> </li> <li class="devsite-footer-linkbox-item"> <a href="//play.google.com/apps/publish" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 3)" > Google Play Console </a> </li> <li class="devsite-footer-linkbox-item"> <a href="//console.firebase.google.com" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 4)" > Firebase Console </a> </li> <li class="devsite-footer-linkbox-item"> <a href="//console.actions.google.com" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 5)" > Actions on Google Console </a> </li> <li class="devsite-footer-linkbox-item"> <a href="//cast.google.com/publish" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 6)" > Cast SDK Developer Console </a> </li> <li class="devsite-footer-linkbox-item"> <a href="//chrome.google.com/webstore/developer/dashboard" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 7)" > Chrome Web Store Dashboard </a> </li> <li class="devsite-footer-linkbox-item"> <a href="//console.home.google.com" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 8)" > Google Home Developer Console </a> </li> </ul> </li> </ul> </nav> </devsite-footer-linkboxes> <devsite-footer-utility class="devsite-footer"> <div class="devsite-footer-utility nocontent"> <nav class="devsite-footer-sites" aria-label="Other Google Developers websites"> <a href="https://developers.google.com/" class="devsite-footer-sites-logo-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Google Developers Link"> <picture> <img class="devsite-footer-sites-logo" src="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/developers/images/lockup-google-for-developers.svg" loading="lazy" alt="Google Developers"> </picture> </a> <ul class="devsite-footer-sites-list"> <li class="devsite-footer-sites-item"> <a href="//developer.android.com" class="devsite-footer-sites-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Android Link" > Android </a> </li> <li class="devsite-footer-sites-item"> <a href="//developer.chrome.com/home" class="devsite-footer-sites-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Chrome Link" > Chrome </a> </li> <li class="devsite-footer-sites-item"> <a href="//firebase.google.com" class="devsite-footer-sites-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Firebase Link" > Firebase </a> </li> <li class="devsite-footer-sites-item"> <a href="//cloud.google.com" class="devsite-footer-sites-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Google Cloud Platform Link" > Google Cloud Platform </a> </li> <li class="devsite-footer-sites-item"> <a href="//ai.google.dev/" class="devsite-footer-sites-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Google AI Link" > Google AI </a> </li> <li class="devsite-footer-sites-item"> <a href="/products" class="devsite-footer-sites-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer All products Link" > All products </a> </li> </ul> </nav> <nav class="devsite-footer-utility-links" aria-label="Utility links"> <ul class="devsite-footer-utility-list"> <li class="devsite-footer-utility-item "> <a class="devsite-footer-utility-link gc-analytics-event" href="/terms/site-terms" data-category="Site-Wide Custom Events" data-label="Footer Terms link" > Terms </a> </li> <li class="devsite-footer-utility-item "> <a class="devsite-footer-utility-link gc-analytics-event" href="//policies.google.com/privacy" data-category="Site-Wide Custom Events" data-label="Footer Privacy link" > Privacy </a> </li> <li class="devsite-footer-utility-item glue-cookie-notification-bar-control"> <a class="devsite-footer-utility-link gc-analytics-event" href="#" data-category="Site-Wide Custom Events" data-label="Footer Manage cookies link" aria-hidden="true" > Manage cookies </a> </li> <li class="devsite-footer-utility-item devsite-footer-utility-button"> <span class="devsite-footer-utility-description">Sign up for the Google for Developers newsletter</span> <a class="devsite-footer-utility-link gc-analytics-event" href="/newsletter/subscribe" data-category="Site-Wide Custom Events" data-label="Footer Subscribe link" > Subscribe </a> </li> </ul> <devsite-language-selector> <ul role="presentation"> <li role="presentation"> <a role="menuitem" lang="en" >English</a> </li> <li role="presentation"> <a role="menuitem" lang="de" >Deutsch</a> </li> <li role="presentation"> <a role="menuitem" lang="es" >Español</a> </li> <li role="presentation"> <a role="menuitem" lang="es_419" >Español – América Latina</a> </li> <li role="presentation"> <a role="menuitem" lang="fr" >Français</a> </li> <li role="presentation"> <a role="menuitem" lang="id" >Indonesia</a> </li> <li role="presentation"> <a role="menuitem" lang="it" >Italiano</a> </li> <li role="presentation"> <a role="menuitem" lang="pl" >Polski</a> </li> <li role="presentation"> <a role="menuitem" lang="pt_br" >Português – Brasil</a> </li> <li role="presentation"> <a role="menuitem" lang="vi" >Tiếng Việt</a> </li> <li role="presentation"> <a role="menuitem" lang="tr" >Türkçe</a> </li> <li role="presentation"> <a role="menuitem" lang="ru" >Русский</a> </li> <li role="presentation"> <a role="menuitem" lang="he" >עברית</a> </li> <li role="presentation"> <a role="menuitem" lang="ar" >العربيّة</a> </li> <li role="presentation"> <a role="menuitem" lang="fa" >فارسی</a> </li> <li role="presentation"> <a role="menuitem" lang="hi" >हिंदी</a> </li> <li role="presentation"> <a role="menuitem" lang="bn" >বাংলা</a> </li> <li role="presentation"> <a role="menuitem" lang="th" >ภาษาไทย</a> </li> <li role="presentation"> <a role="menuitem" lang="zh_cn" >中文 – 简体</a> </li> <li role="presentation"> <a role="menuitem" lang="zh_tw" >中文 – 繁體</a> </li> <li role="presentation"> <a role="menuitem" lang="ja" >日本語</a> </li> <li role="presentation"> <a role="menuitem" lang="ko" >한국어</a> </li> </ul> </devsite-language-selector> </nav> </div> </devsite-footer-utility> <devsite-panel></devsite-panel> </section></section> <devsite-sitemask></devsite-sitemask> <devsite-snackbar></devsite-snackbar> <devsite-tooltip ></devsite-tooltip> <devsite-heading-link></devsite-heading-link> <devsite-analytics> <script type="application/json" analytics>[{"dimensions": {"dimension11": false, "dimension6": "en", "dimension1": "Signed out", "dimension5": "en", "dimension4": "Campaign Manager 360", "dimension3": false}, "gaid": "UA-24532603-1", "metrics": {"ratings_count": "metric2", "ratings_value": "metric1"}, "purpose": 1}, {"dimensions": {"dimension11": false, "dimension6": "en", "dimension1": "Signed out", "dimension5": "en", "dimension4": "Campaign Manager 360", "dimension3": false}, "gaid": "UA-54828107-12", "metrics": {"ratings_count": "metric2", "ratings_value": "metric1"}, "purpose": 0}]</script> <script type="application/json" tag-management>{"at": "True", "ga4": [{"id": "G-272J68FCRF", "purpose": 1}], "ga4p": [{"id": "G-272J68FCRF", "purpose": 1}], "gtm": [], "parameters": {"internalUser": "False", "language": {"machineTranslated": "False", "requested": "en", "served": "en"}, "pageType": "article", "projectName": "Campaign Manager 360", "signedIn": "False", "tenant": "developers", "recommendations": {"sourcePage": "", "sourceType": 0, "sourceRank": 0, "sourceIdenticalDescriptions": 0, "sourceTitleWords": 0, "sourceDescriptionWords": 0, "experiment": ""}, "experiment": {"ids": ""}}}</script> </devsite-analytics> <devsite-badger></devsite-badger> <script nonce="fiWsZyIvQXesS30crDcivLkMHYyYFR"> (function(d,e,v,s,i,t,E){d['GoogleDevelopersObject']=i; t=e.createElement(v);t.async=1;t.src=s;E=e.getElementsByTagName(v)[0]; E.parentNode.insertBefore(t,E);})(window, document, 'script', 'https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/developers/js/app_loader.js', '[1,"en",null,"/js/devsite_app_module.js","https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625","https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/developers","https://developers-dot-devsite-v2-prod.appspot.com",null,null,["/_pwa/developers/manifest.json","https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/images/video-placeholder.svg","https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/developers/images/favicon-new.png","https://fonts.googleapis.com/css?family=Google+Sans:400,500|Roboto:400,400italic,500,500italic,700,700italic|Roboto+Mono:400,500,700&display=swap"],1,null,[1,6,8,12,14,17,21,25,50,52,63,70,75,76,80,87,91,92,93,97,98,100,101,102,103,104,105,107,108,109,110,112,113,117,118,120,122,124,125,126,127,129,130,131,132,133,134,135,136,138,140,141,147,148,149,151,152,156,157,158,159,161,163,164,168,169,170,179,180,182,183,186,191,193,196],"AIzaSyAP-jjEJBzmIyKR4F-3XITp8yM9T1gEEI8","AIzaSyB6xiKGDR5O3Ak2okS4rLkauxGUG7XP0hg","developers.google.com","AIzaSyAQk0fBONSGUqCNznf6Krs82Ap1-NV6J4o","AIzaSyCCxcqdrZ_7QMeLCRY20bh_SXdAYqy70KY",null,null,null,["Profiles__enable_page_saving","MiscFeatureFlags__enable_view_transitions","MiscFeatureFlags__enable_variable_operator","Search__enable_ai_eligibility_checks","Profiles__enable_developer_profiles_callout","Search__enable_ai_search_summaries","Profiles__require_profile_eligibility_for_signin","TpcFeatures__enable_required_headers","Significatio__enable_by_tenant","Profiles__enable_profile_collections","EngEduTelemetry__enable_engedu_telemetry","Cloud__enable_cloud_shell_fte_user_flow","MiscFeatureFlags__emergency_css","MiscFeatureFlags__developers_footer_image","Profiles__enable_dashboard_curated_recommendations","MiscFeatureFlags__enable_project_variables","Search__enable_page_map","DevPro__enable_cloud_innovators_plus","Search__enable_dynamic_content_confidential_banner","Profiles__enable_awarding_url","CloudShell__cloud_code_overflow_menu","Cloud__enable_cloud_shell","Cloud__enable_cloudx_ping","BookNav__enable_tenant_cache_key","Analytics__enable_clearcut_logging","Concierge__enable_pushui","MiscFeatureFlags__developers_footer_dark_image","Concierge__enable_concierge","CloudShell__cloud_shell_button","DevPro__enable_developer_subscriptions","Search__enable_suggestions_from_borg","Experiments__reqs_query_experiments","Cloud__enable_legacy_calculator_redirect","Profiles__enable_complete_playlist_endpoint","Cloud__enable_cloud_dlp_service","Profiles__enable_public_developer_profiles","Cloud__enable_cloud_facet_chat","Cloud__enable_cloudx_experiment_ids","Cloud__enable_free_trial_server_call","Search__enable_ai_search_summaries_restricted","Cloud__enable_llm_concierge_chat","Concierge__enable_concierge_restricted","Profiles__enable_release_notes_notifications","MiscFeatureFlags__enable_firebase_utm","Profiles__enable_completecodelab_endpoint","MiscFeatureFlags__enable_explain_this_code","TpcFeatures__enable_mirror_tenant_redirects","Profiles__enable_recognition_badges"],null,null,"AIzaSyBLEMok-5suZ67qRPzx0qUtbnLmyT_kCVE","https://developerscontentserving-pa.clients6.google.com","AIzaSyCM4QpTRSqP5qI4Dvjt4OAScIN8sOUlO-k","https://developerscontentsearch-pa.clients6.google.com",1,4,null,"https://developerprofiles-pa.clients6.google.com",[1,"developers","Google for Developers","developers.google.com",null,"developers-dot-devsite-v2-prod.appspot.com",null,null,[1,1,[1],null,null,null,null,null,null,null,null,[1],null,null,null,null,null,null,[1],[1,null,null,[1,20],"/recommendations/information"],null,null,null,[1,1,1],[1,1,null,1,1]],null,[null,null,null,null,null,null,"/images/lockup-new.svg","/images/touchicon-180-new.png",null,null,null,null,1,null,null,null,null,null,null,null,null,1,null,null,null,"/images/lockup-dark-theme-new.svg",[]],[],null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,[6,1,14,15,20,22,23,29,32,36],null,[[null,null,null,[3,7,10,2,39,17,4,32,24,11,12,13,34,15,25],null,null,[1,[["docType","Choose a content type",[["Tutorial",null,null,null,null,null,null,null,null,"Tutorial"],["Guide",null,null,null,null,null,null,null,null,"Guide"],["Sample",null,null,null,null,null,null,null,null,"Sample"]]],["product","Choose a product",[["Android",null,null,null,null,null,null,null,null,"Android"],["ARCore",null,null,null,null,null,null,null,null,"ARCore"],["ChromeOS",null,null,null,null,null,null,null,null,"ChromeOS"],["Firebase",null,null,null,null,null,null,null,null,"Firebase"],["Flutter",null,null,null,null,null,null,null,null,"Flutter"],["Assistant",null,null,null,null,null,null,null,null,"Google Assistant"],["GoogleCloud",null,null,null,null,null,null,null,null,"Google Cloud"],["GoogleMapsPlatform",null,null,null,null,null,null,null,null,"Google Maps Platform"],["GooglePay",null,null,null,null,null,null,null,null,"Google Pay & Google Wallet"],["GooglePlay",null,null,null,null,null,null,null,null,"Google Play"],["Tensorflow",null,null,null,null,null,null,null,null,"TensorFlow"]]],["category","Choose a topic",[["AiAndMachineLearning",null,null,null,null,null,null,null,null,"AI and Machine Learning"],["Data",null,null,null,null,null,null,null,null,"Data"],["Enterprise",null,null,null,null,null,null,null,null,"Enterprise"],["Gaming",null,null,null,null,null,null,null,null,"Gaming"],["Mobile",null,null,null,null,null,null,null,null,"Mobile"],["Web",null,null,null,null,null,null,null,null,"Web"]]]]]],[1,1],null,1],[[["UA-24532603-1"],["UA-22084204-5"],null,null,["UA-24532603-5"],null,null,[["G-272J68FCRF"],null,null,[["G-272J68FCRF",2]]],[["UA-24532603-1",2]],null,[["UA-24532603-5",2]],null,1],[[3,2],[15,12],[1,1],[6,5],[14,11],[11,8],[5,4],[4,3],[13,10],[16,13],[12,9]],[[2,2],[1,1]]],null,4,null,null,null,null,null,null,null,null,null,null,null,null,null,"developers.devsite.google"],null,"pk_live_5170syrHvgGVmSx9sBrnWtA5luvk9BwnVcvIi7HizpwauFG96WedXsuXh790rtij9AmGllqPtMLfhe2RSwD6Pn38V00uBCydV4m"]') </script> <devsite-a11y-announce></devsite-a11y-announce> </body> </html>