คุณต้องตั้งค่าการให้สิทธิ์ก่อนจึงจะส่งคําขอไปยัง API ได้ หากกําลังใช้ไลบรารีของไคลเอ็นต์ คุณจะต้องสร้างออบเจ็กต์ Service
ด้วย
โค้ดต่อไปนี้จะแสดงวิธีกําหนดค่าไคลเอ็นต์และให้สิทธิ์คําขอโดยใช้คีย์ API
Java
import com.google.api.client.http.HttpTransport; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.jackson.JacksonFactory; import com.google.api.services.adexperiencereport.v1.AdExperienceReport; import com.google.api.services.adexperiencereport.v1.AdExperienceReportRequestInitializer; import com.google.api.services.adexperiencereport.v1.model.SiteSummaryResponse; import com.google.api.services.adexperiencereport.v1.model.ViolatingSitesResponse; ... public static void main(String[] args) { HttpTransport httpTransport = new NetHttpTransport(); JacksonFactory jsonFactory = new JacksonFactory(); AdExperienceReportRequestInitializer reqInitializer = new AdExperienceReportRequestInitializer("YOUR_API_KEY"); AdExperienceReport service = new AdExperienceReport.Builder(httpTransport, jsonFactory, null) .setAdExperienceReportRequestInitializer(reqInitializer) .setApplicationName("YOUR_APPLICATION_NAME") .build(); ViolatingSitesResponse response = service.violatingSites().list().execute(); ... } ...
Python
from apiclient.discovery import build api_key = 'YOUR_API_KEY' service = build('adexperiencereport', 'v1', developerKey=api_key) response = service.violatingSites().list().execute() ...
PHP
$client = new Google_Client(); $client->setApplicationName("YOUR_APPLICATION_NAME"); $client->setDeveloperKey("YOUR_API_KEY"); $service = new Google_Service_AdExperienceReport($client); $response = $service->violatingSites; ...
.NET
using Google.Apis.AdExperienceReport.v1.AdExperienceReportService; using Google.Apis.Services.BaseClientService.Initializer; ... public static void Main(string[] args) { var service = new AdExperienceReportService(new BaseClientService.Initializer { ApplicationName = "YOUR_APPLICATION_NAME", ApiKey = "YOUR_API_KEY", }); var response = await service.ViolatingSites.List().ExecuteAsync(); ... }