Bắt đầu

Campaign Manager 360 API cho phép bạn truy cập theo phương pháp có lập trình vào thông tin từ Tài khoản Campaign Manager 360. Tài khoản này được dùng để quản lý và tạo chiến dịch và báo cáo, giống như cách bạn làm thông qua Campaign Manager 360 và các dịch vụ web Trình tạo báo cáo.

Hướng dẫn này mô tả cách bắt đầu sử dụng Campaign Manager 360 API.

Điều kiện tiên quyết

Trước khi sử dụng API Campaign Manager 360, bạn cần thực hiện một số bước điều kiện tiên quyết đáp ứng:

  1. Bạn phải có tài khoản Campaign Manager 360. Xem Nhà quảng cáo/Đại lý để biết thông tin đăng ký.

  2. Bạn phải bật Quyền truy cập API cho tài khoản Campaign Manager 360 của mình. Thường gặp nhất các tài khoản đã bật tính năng này theo mặc định; nếu bạn không chắc chắn, hãy liên hệ nhân viên phụ trách khách hàng hoặc nhóm hỗ trợ Campaign Manager 360 để được trợ giúp.

  3. Bạn phải có hồ sơ người dùng có quyền truy cập vào tài khoản này. Có Quản trị viên tài khoản Campaign Manager 360 tạo hồ sơ người dùng được liên kết với tài khoản này.

  4. Kiểm tra quyền đối với hồ sơ người dùng trong giao diện người dùng Campaign Manager 360. Các chế độ kiểm soát này những nội dung mà hồ sơ người dùng có thể truy cập từ API. Không có API riêng biệt quyền truy cập.

Tạo một dự án

Để bắt đầu sử dụng Campaign Manager 360 API, trước tiên, bạn cần tạo hoặc chọn một dự án trong Google API Console rồi bật API đó. Đang dùng đường liên kết hướng dẫn bạn thực hiện quy trình và kích hoạt Campaign Manager 360 API tự động.

Tạo thông tin xác thực

Tất cả các yêu cầu mà bạn đưa ra đối với Campaign Manager 360 API đều phải được cho phép. Tóm tắt tổng quan về việc uỷ quyền, hãy đọc về cách uỷ quyền và nhận dạng cho Google.

Các hướng dẫn sau sẽ giúp bạn thực hiện quy trình tạo OAuth 2.0 ID ứng dụng khách để sử dụng với ứng dụng đã cài đặt luồng. Để xem hướng dẫn về cách tạo thông tin đăng nhập để sử dụng với quy trình tài khoản dịch vụ, hãy tham khảo Tài khoản dịch vụ của chúng tôi.

  1. Làm theo các bước để định cấu hình dự án Google API Console.

  2. Mở trang Thông tin đăng nhập trong Bảng điều khiển API.
  3. Nhấp vào TẠO XÁC NHẬN > Mã ứng dụng OAuth.

    1. Nếu bạn chưa định cấu hình màn hình xin phép bằng OAuth cho dự án này trước đây, bạn sẽ được hướng dẫn để thực hiện việc này ngay bây giờ. Nhấp vào CẤU HÌNH ĐỒNG Ý MÀN HÌNH.

    2. Chọn loại người dùng rồi nhấp vào TẠO.

    3. Điền vào biểu mẫu ban đầu. Bạn có thể chỉnh sửa tên này sau, nếu cần. Nhấp chuột Lưu khi hoàn tất.

    4. Quay lại phần Thông tin xác thực > TẠO NỘI DUNG uy tín > Mã ứng dụng OAuth sang tiếp tục.

  4. Chọn loại ứng dụng là Desktop app (Ứng dụng dành cho máy tính), đặt tên cho ứng dụng đó, rồi nhấp vào Tạo.

Khi hoàn tất, bạn sẽ thấy mã ứng dụng khách và ứng dụng OAuth 2.0 khóa bí mật mà bạn có thể tải xuống ở định dạng JSON và lưu để sử dụng sau này.

Cài đặt thư viện ứng dụng

Campaign Manager 360 API được xây dựng dựa trên HTTP và JSON, vì vậy, mọi ứng dụng HTTP tiêu chuẩn đều có thể gửi yêu cầu vào thư viện và phân tích cú pháp phản hồi.

Tuy nhiên, thư viện ứng dụng Google API cung cấp khả năng tích hợp ngôn ngữ tốt hơn, tăng cường bảo mật và hỗ trợ thực hiện các yêu cầu được uỷ quyền. Khách hàng các thư viện có sẵn bằng một số ngôn ngữ lập trình; bằng cách sử dụng chúng, bạn có thể giúp tránh việc phải thiết lập yêu cầu HTTP và phân tích cú pháp phản hồi theo cách thủ công.

Để bắt đầu, hãy chọn ngôn ngữ lập trình mà bạn đang sử dụng phát triển ứng dụng.

C#

Cài đặt thư viện ứng dụng Campaign Manager 360 API cho .NET mới nhất. Sử dụng Bạn nên sử dụng NuGet để quản lý việc cài đặt của mình.

Mở Bảng điều khiển Trình quản lý gói NuGet và chạy lệnh sau:

Install-Package Google.Apis.Dfareporting.v3_4

Tìm hiểu thêm

Java

Cài đặt thư viện ứng dụng Campaign Manager 360 API cho Java mới nhất. Sử dụng Bạn nên sử dụng Maven để quản lý việc cài đặt của mình.

Hãy thêm phần phụ thuộc sau đây vào tệp pom.xml:

<dependency>
  <groupId>com.google.apis</groupId>
  <artifactId>google-api-services-dfareporting</artifactId>
  <version>v4-rev20220611-1.32.1</version>
  <exclusions>
    <exclusion>
      <groupId>com.google.guava</groupId>
      <artifactId>guava-jdk5</artifactId>
    </exclusion>
  </exclusions>
</dependency>

Tìm hiểu thêm

PHP

Cài đặt thư viện ứng dụng Campaign Manager 360 API cho PHP mới nhất. Sử dụng Bạn nên sử dụng Composer để quản lý việc cài đặt.

Mở cửa sổ dòng lệnh và chạy lệnh sau:

composer require google/apiclient

Nếu bạn đã cài đặt thư viện và chỉ muốn cập nhật lên phiên bản mới nhất:

composer update google/apiclient

Tuỳ thuộc vào hệ thống của bạn, bạn có thể cần phải thêm các lệnh này vào đầu sudo.

Tìm hiểu thêm

Python

Cài đặt thư viện ứng dụng Campaign Manager 360 API cho Python mới nhất. Sử dụng Bạn nên dùng pip để quản lý việc cài đặt của mình.

Mở cửa sổ dòng lệnh và chạy lệnh sau:

pip install --upgrade google-api-python-client

Tuỳ thuộc vào hệ thống của bạn, bạn có thể cần phải thêm các lệnh này vào đầu sudo.

Tìm hiểu thêm

Ruby

Cài đặt thư viện ứng dụng Campaign Manager 360 API mới nhất cho Ruby. Sử dụng Bạn nên dùng RubyGems để quản lý chế độ cài đặt của mình.

Mở cửa sổ dòng lệnh và chạy lệnh sau:

gem install google-api-client

Nếu bạn đã cài đặt thư viện và chỉ muốn cập nhật lên phiên bản mới nhất:

gem update -y google-api-client

Tuỳ thuộc vào hệ thống của bạn, bạn có thể cần phải thêm các lệnh này vào đầu sudo.

Tìm hiểu thêm

Bạn có thể xem thêm các ngôn ngữ được hỗ trợ trên trang Ứng dụng Thư viện.

Tạo yêu cầu

Khi tạo thông tin xác thực OAuth 2.0thư viện ứng dụng , bạn đã có thể bắt đầu sử dụng Campaign Manager 360 API. Tìm hiểu cách thức để cấp phép, định cấu hình ứng dụng khách và đưa ra yêu cầu đầu tiên của bạn bằng cách làm theo hướng dẫn bắt đầu nhanh bên dưới.

C#

  1. Tải tệp bí mật ứng dụng khách và tạo thông tin xác thực uỷ quyền.

    Vào lần đầu tiên thực hiện bước này, bạn sẽ được yêu cầu chấp nhận lời nhắc cấp phép trong trình duyệt. Trước khi chấp nhận, hãy đảm bảo bạn đã đăng nhập bằng Tài khoản Google có quyền truy cập vào Campaign Manager 360. Ứng dụng của bạn sẽ được phép truy cập vào dữ liệu thay mặt cho bất kỳ tài khoản nào hiện đang đăng nhập.

    // Load client secrets from the specified JSON file.
    GoogleClientSecrets clientSecrets;
    using(Stream json = new FileStream(pathToJsonFile, FileMode.Open, FileAccess.Read)) {
      clientSecrets = GoogleClientSecrets.Load(json);
    }
    
    // Create an asynchronous authorization task.
    //
    // Note: providing a data store allows auth credentials to be cached, so they survive multiple
    // runs of the application. This avoids prompting the user for authorization every time the
    // access token expires, by remembering the refresh token. The "user" value is used to
    // identify a specific set of credentials within the data store. You may provide different
    // values here to persist credentials for multiple users to the same data store.
    Task<UserCredential> authorizationTask = GoogleWebAuthorizationBroker.AuthorizeAsync(
        clientSecrets.Secrets,
        OAuthScopes,
        "user",
        CancellationToken.None,
        dataStore);
    
    // Authorize and persist credentials to the data store.
    UserCredential credential = authorizationTask.Result;
    
  2. Tạo một ứng dụng Dfareporting được uỷ quyền.

    // Create a Dfareporting service object.
    //
    // Note: application name should be replaced with a value that identifies your application.
    service = new DfareportingService(
        new BaseClientService.Initializer {
          HttpClientInitializer = credential,
          ApplicationName = "C# installed app sample"
        }
    );
    
  3. Thực hiện thao tác.

    // Retrieve and print all user profiles for the current authorized user.
    UserProfileList profiles = service.UserProfiles.List().Execute();
    
    foreach (UserProfile profile in profiles.Items) {
      Console.WriteLine("Found user profile with ID {0} and name \"{1}\".",
          profile.ProfileId, profile.UserName);
    }
    

Java

  1. Tải tệp bí mật ứng dụng khách và tạo thông tin xác thực uỷ quyền.

    Vào lần đầu tiên thực hiện bước này, bạn sẽ được yêu cầu chấp nhận lời nhắc cấp phép trong trình duyệt. Trước khi chấp nhận, hãy đảm bảo bạn đã đăng nhập bằng Tài khoản Google có quyền truy cập vào Campaign Manager 360. Ứng dụng của bạn sẽ được phép truy cập vào dữ liệu thay mặt cho bất kỳ tài khoản nào hiện đang đăng nhập.

    // Load the client secrets JSON file.
    GoogleClientSecrets clientSecrets =
        GoogleClientSecrets.load(
            jsonFactory, Files.newBufferedReader(Paths.get(pathToClientSecretsFile), UTF_8));
    
    // Set up the authorization code flow.
    //
    // Note: providing a DataStoreFactory allows auth credentials to be cached, so they survive
    // multiple runs of the program. This avoids prompting the user for authorization every time the
    // access token expires, by remembering the refresh token.
    GoogleAuthorizationCodeFlow flow =
        new GoogleAuthorizationCodeFlow.Builder(
                httpTransport, jsonFactory, clientSecrets, OAUTH_SCOPES)
            .setDataStoreFactory(dataStoreFactory)
            .build();
    
    // Authorize and persist credentials to the data store.
    //
    // Note: the "user" value below is used to identify a specific set of credentials in the data
    // store. You may provide different values here to persist credentials for multiple users to
    // the same data store.
    Credential credential =
        new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
    
  2. Tạo một ứng dụng Dfareporting được uỷ quyền.

    // Create a Dfareporting client instance.
    //
    // Note: application name below should be replaced with a value that identifies your
    // application. Suggested format is "MyCompany-ProductName/Version.MinorVersion".
    Dfareporting reporting =
        new Dfareporting.Builder(credential.getTransport(), credential.getJsonFactory(), credential)
            .setApplicationName("dfareporting-java-installed-app-sample")
            .build();
    
  3. Thực hiện thao tác.

    // Retrieve and print all user profiles for the current authorized user.
    UserProfileList profiles = reporting.userProfiles().list().execute();
    for (int i = 0; i < profiles.getItems().size(); i++) {
      System.out.printf("%d) %s%n", i + 1, profiles.getItems().get(i).getUserName());
    }
    

PHP

  1. Tải tệp bí mật ứng dụng khách và tạo thông tin xác thực uỷ quyền.

    Vào lần đầu tiên thực hiện bước này, bạn sẽ được yêu cầu chấp nhận lời nhắc cấp phép trong trình duyệt. Trước khi chấp nhận, hãy đảm bảo bạn đã đăng nhập bằng Tài khoản Google có quyền truy cập vào Campaign Manager 360. Ứng dụng của bạn sẽ được phép truy cập vào dữ liệu thay mặt cho bất kỳ tài khoản nào hiện đang đăng nhập.

    // Create a Google_Client instance.
    //
    // Note: application name should be replaced with a value that identifies
    // your application. Suggested format is "MyCompany-ProductName".
    $client = new Google_Client();
    $client->setAccessType('offline');
    $client->setApplicationName('PHP installed app sample');
    $client->setRedirectUri(self::OAUTH_REDIRECT_URI);
    $client->setScopes(self::$OAUTH_SCOPES);
    
    // Load the client secrets file.
    $client->setAuthConfig($pathToJsonFile);
    
    // Try to load cached credentials from the token store. Using a token store
    // allows auth credentials to be cached, so they survive multiple runs of
    // the application. This avoids prompting the user for authorization every
    // time the access token expires, by remembering the refresh token.
    if (file_exists($tokenStore) && filesize($tokenStore) > 0) {
        $client->setAccessToken(file_get_contents($tokenStore));
    } else {
        // If no cached credentials were found, authorize and persist
        // credentials to the token store.
        print 'Open this URL in your browser and authorize the application.';
        printf("\n\n%s\n\n", $client->createAuthUrl());
        print 'Enter the authorization code: ';
        $code = trim(fgets(STDIN));
        $client->authenticate($code);
    
        file_put_contents($tokenStore, json_encode($client->getAccessToken()));
    }
    
  2. Tạo một ứng dụng Dfareporting được uỷ quyền.

    // Create a Dfareporting service object.
    $service = new Google_Service_Dfareporting($client);
    
  3. Thực hiện thao tác.

    // Retrieve and print all user profiles for the current authorized user.
    $result = $service->userProfiles->listUserProfiles();
    foreach ($result['items'] as $userProfile) {
        printf(
            "User profile \"%s\" (ID: %d) found for account %d.\n",
            $userProfile->getUserName(),
            $userProfile->getProfileId(),
            $userProfile->getAccountId()
        );
    }
    

Python

  1. Tải tệp bí mật ứng dụng khách và tạo thông tin xác thực uỷ quyền.

    Vào lần đầu tiên thực hiện bước này, bạn sẽ được yêu cầu chấp nhận lời nhắc cấp phép trong trình duyệt. Trước khi chấp nhận, hãy đảm bảo bạn đã đăng nhập bằng Tài khoản Google có quyền truy cập vào Campaign Manager 360. Ứng dụng của bạn sẽ được phép truy cập vào dữ liệu thay mặt cho bất kỳ tài khoản nào hiện đang đăng nhập.

    # Set up a Flow object to be used if we need to authenticate.
    flow = client.flow_from_clientsecrets(
        path_to_client_secrets_file, scope=OAUTH_SCOPES)
    
    # Check whether credentials exist in the credential store. Using a credential
    # store allows auth credentials to be cached, so they survive multiple runs
    # of the application. This avoids prompting the user for authorization every
    # time the access token expires, by remembering the refresh token.
    storage = Storage(CREDENTIAL_STORE_FILE)
    credentials = storage.get()
    
    # If no credentials were found, go through the authorization process and
    # persist credentials to the credential store.
    if credentials is None or credentials.invalid:
      credentials = tools.run_flow(flow, storage,
                                   tools.argparser.parse_known_args()[0])
    
    # Use the credentials to authorize an httplib2.Http instance.
    http = credentials.authorize(httplib2.Http())
    
  2. Tạo một ứng dụng Dfareporting được uỷ quyền.

    # Construct a service object via the discovery service.
    service = discovery.build('dfareporting', 'v4', http=http)
    
  3. Thực hiện thao tác.

    # Construct the request.
    request = service.userProfiles().list()
    
    # Execute request and print response.
    response = request.execute()
    
    for profile in response['items']:
      print('Found user profile with ID %s and user name "%s".' %
            (profile['profileId'], profile['userName']))
    

Ruby

  1. Tải tệp bí mật ứng dụng khách và tạo thông tin xác thực uỷ quyền.

    Vào lần đầu tiên thực hiện bước này, bạn sẽ được yêu cầu chấp nhận lời nhắc cấp phép trong trình duyệt. Trước khi chấp nhận, hãy đảm bảo bạn đã đăng nhập bằng Tài khoản Google có quyền truy cập vào Campaign Manager 360. Ứng dụng của bạn sẽ được phép truy cập vào dữ liệu thay mặt cho bất kỳ tài khoản nào hiện đang đăng nhập.

    # Load client ID from the specified file.
    client_id = Google::Auth::ClientId.from_file(path_to_json_file)
    
    # Set up the user authorizer.
    #
    # Note: providing a token store allows auth credentials to be cached, so they
    # survive multiple runs of the application. This avoids prompting the user for
    # authorization every time the access token expires, by remembering the
    # refresh token.
    authorizer = Google::Auth::UserAuthorizer.new(
      client_id, [API_NAMESPACE::AUTH_DFAREPORTING], token_store
    )
    
    # Authorize and persist credentials to the data store.
    #
    # Note: the 'user' value below is used to identify a specific set of
    # credentials in the token store. You may provide different values here to
    # persist credentials for multiple users to the same token store.
    authorization = authorizer.get_credentials('user')
    if authorization.nil?
      puts format(
        "Open this URL in your browser and authorize the application.\n\n%s" \
        "\n\nEnter the authorization code:",
        authorizer.get_authorization_url(base_url: OAUTH_REDIRECT_URI)
      )
      code = STDIN.gets.chomp
      authorization = authorizer.get_and_store_credentials_from_code(
        base_url: OAUTH_REDIRECT_URI, code: code, user_id: 'user'
      )
    end
    
  2. Tạo một ứng dụng Dfareporting được uỷ quyền.

    # Create a Dfareporting service object.
    #
    # Note: application name should be replaced with a value that identifies
    # your application. Suggested format is "MyCompany-ProductName".
    service = API_NAMESPACE::DfareportingService.new
    service.authorization = authorization
    service.client_options.application_name = 'Ruby installed app sample'
    service.client_options.application_version = '1.0.0'
    
  3. Thực hiện thao tác.

    // Retrieve and print all user profiles for the current authorized user.
    UserProfileList profiles = service.UserProfiles.List().Execute();
    
    foreach (UserProfile profile in profiles.Items) {
      Console.WriteLine("Found user profile with ID {0} and name \"{1}\".",
          profile.ProfileId, profile.UserName);
    }
    

Tìm hiểu thêm

Truy cập Tài liệu tham khảo API để tìm hiểu về tất cả dịch vụ mà API phải cung cấp. Mỗi trang chi tiết về phương thức đều có một API Trình khám phá mà bạn có thể sử dụng để đưa ra yêu cầu kiểm thử ngay từ trình duyệt.

Xem các hướng dẫn khác của chúng tôi, trong đó đề cập đến các chủ đề nâng cao và cung cấp toàn bộ hướng dẫn ví dụ về các thao tác phổ biến.

Khi bạn đã sẵn sàng bắt đầu viết mã, hãy khám phá các tập hợp mẫu mã mà có thể được sửa đổi và được mở rộng để phù hợp với nhu cầu của bạn.