使用 Google Ad Manager API (Beta 版) 讀取 Ad Manager 資料並執行報表。
如要提出第一個 API 要求,請完成下列步驟:
取得 Ad Manager 聯播網存取權
如果您還沒有 Ad Manager 帳戶,請註冊 Ad Manager 帳戶。
啟用 Ad Manager API
在 Google API 控制台專案中啟用 Ad Manager API。
驗證
所有 API 要求都必須使用 OAuth2 進行驗證。
Ad Manager API 用戶端程式庫會從應用程式預設憑證讀取憑證。您可以使用環境變數或 gcloud 設定這些值。
服務帳戶
Linux 或 macOS
export GOOGLE_APPLICATION_CREDENTIALS=KEY_FILE_PATH
Windows
set GOOGLE_APPLICATION_CREDENTIALS=KEY_FILE_PATH
使用者憑證
gcloud auth application-default login --scopes="https://www.googleapis.com/auth/admanager"
# End user credentials must specify the cloud project where the API is enabled.
gcloud auth application-default set-quota-project PROJECT_ID
如要進一步瞭解如何選擇憑證類型及建立憑證,請參閱驗證指南。
設定用戶端程式庫
Java
Maven:
<!-- pom.xml -->
<dependency>
<groupId>com.google.api-ads</groupId>
<artifactId>ad-manager</artifactId>
<version>0.1.0</version>
</dependency>
如為 Gradle:
implementation 'com.google.api-ads:ad-manager:0.1.0'
Python
從 PyPi 安裝用戶端程式庫。
pip install google-ads-admanager
.NET
從 NuGet 安裝用戶端程式庫。
透過 .NET CLI:
dotnet add package Google.Ads.AdManager.V1 --version 1.0.0-beta01
做為套件參考資料:
<PackageReference Include="Google.Ads.AdManager.V1" Version="1.0.0-beta01" />
提出第一個要求
Java
import com.google.ads.admanager.v1.GetNetworkRequest;
import com.google.ads.admanager.v1.Network;
import com.google.ads.admanager.v1.NetworkName;
import com.google.ads.admanager.v1.NetworkServiceClient;
public class SyncGetNetwork {
public static void main(String[] args) throws Exception {
syncGetNetwork();
}
public static void syncGetNetwork() throws Exception {
try (NetworkServiceClient networkServiceClient = NetworkServiceClient.create()) {
GetNetworkRequest request =
GetNetworkRequest.newBuilder()
.setName(NetworkName.of("[NETWORK_CODE]").toString())
.build();
Network response = networkServiceClient.getNetwork(request);
}
}
}
Python
from google.ads import admanager_v1
def sample_get_network():
# Create a client
client = admanager_v1.NetworkServiceClient()
# Initialize request argument(s)
request = admanager_v1.GetNetworkRequest(
name="name_value",
)
# Make the request
response = client.get_network(request=request)
# Handle the response
print(response)
.NET
using Google.Ads.AdManager.V1;
public sealed partial class GeneratedNetworkServiceClientSnippets
{
public void GetNetwork()
{
// Create client
NetworkServiceClient networkServiceClient = NetworkServiceClient.Create();
// Initialize request argument(s)
string name = "networks/[NETWORK_CODE]";
// Make the request
Network response = networkServiceClient.GetNetwork(name);
}
}
cURL
curl -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
https://admanager.googleapis.com/v1/networks/NETWORK_CODE