בוא נתחיל

ה-API של Campaign Manager 360 מספק גישה פרוגרמטית למידע מחשבון Campaign Manager 360. הוא משמש לניהול וליצירה של קמפיינים ודוחות, בדיוק כמו שמשתמשים בשירותי האינטרנט של Campaign Manager 360 ובכלי הדוחות.

במדריך הזה נסביר איך להתחיל לעבוד עם Campaign Manager 360 API.

דרישות מוקדמות

כדי להשתמש ב-Campaign Manager 360 API תצטרכו לעמוד בכמה דרישות מוקדמות:

  1. צריך חשבון ב-Campaign Manager 360. למידע על הרשמה, ראו מפרסמים/סוכנויות.

  2. כדי לקבל גישה ל-API, צריך להפעיל את חשבון Campaign Manager 360. ברוב החשבונות האפשרות הזו מופעלת כברירת מחדל. אם אתם לא בטוחים, תוכלו לפנות לאיש הקשר האחראי לחשבון או לצוות התמיכה של Campaign Manager 360 לקבלת עזרה.

  3. צריכה להיות לך פרופיל משתמש עם גישה לחשבון הזה. לבקש ממנהל המערכת בחשבון Campaign Manager 360 ליצור פרופיל משתמש שמשויך לחשבון.

  4. בודקים את הרשאות פרופיל המשתמש בממשק המשתמש של Campaign Manager 360. האפשרויות האלה קובעות למי יש גישה לפרופיל המשתמש דרך ה-API. אין הרשאות API נפרדות.

יצירת פרויקט

כדי להתחיל להשתמש ב-Campaign Manager 360 API, קודם כל צריך ליצור או לבחור פרויקט במסוף Google API ולהפעיל את ה-API. הקישור הזה ינחה אתכם בתהליך ומפעיל את ה-API של Campaign Manager 360 באופן אוטומטי.

יצירת פרטי כניסה

כל הבקשות שאתם שולחים ל-Campaign Manager 360 API חייבות להיות מאושרות. לסקירה כללית קצרה על הרשאה, קראו את המאמר מתן הרשאה וזיהוי האפליקציה ל-Google.

בהוראות הבאות מוסבר איך יוצרים מזהה לקוח ב-OAuth 2.0 לשימוש בתהליך ההטמעה של אפליקציה מותקנת. להוראות לגבי יצירת פרטי כניסה לשימוש בתהליך של חשבון השירות, עיינו במדריך Service accounts.

  1. פועלים לפי ההוראות כדי להגדיר פרויקט ב-Google API Console.

  2. פותחים את דף פרטי הכניסה ב-API Console.
  3. לוחצים על CREATE CREDENTIALS > OAuth client ID.

    1. אם לא הגדרתם בעבר מסך הסכמה ל-OAuth עבור הפרויקט הזה, תופנו לעשות זאת עכשיו. לוחצים על הגדרת הסכמה במסך.

    2. בוחרים בסוג המשתמש ולוחצים על יצירה.

    3. ממלאים את הטופס הראשוני. במקרה הצורך, אפשר לערוך את הפרטים האלה מאוחר יותר. כשמסיימים, לוחצים על Save.

    4. חוזרים אל Credentials > CREATE CREDENTIALS > OAuth client ID כדי להמשיך.

  4. בוחרים את סוג האפליקציה במחשב, נותנים לה שם ולוחצים על Create.

בסיום, יוצג מזהה לקוח וסוד לקוח מסוג OAuth 2.0. אפשר להוריד אותם בפורמט JSON ולשמור אותם לשימוש עתידי.

התקנה של ספריית לקוח

ה-API של Campaign Manager 360 מבוסס על HTTP ועל JSON, כך שכל לקוח HTTP סטנדרטי יכול לשלוח אליו בקשות ולנתח את התשובות.

עם זאת, ספריות הלקוח של Google API מספקות שילוב טוב יותר של שפות, אבטחה משופרת ותמיכה בביצוע בקשות מורשות. ספריות הלקוח זמינות בכמה שפות תכנות, ועל ידי שימוש בהן אפשר להימנע מהצורך להגדיר בקשות HTTP ולנתח את התשובות באופן ידני.

כדי להתחיל, צריך לבחור את שפת התכנות שבה אתם משתמשים לפיתוח.

C#‎

מתקינים את ספריית הלקוח העדכנית של Campaign Manager 360 API עבור .NET. מומלץ להשתמש ב-NuGet כדי לנהל את ההתקנה.

פותחים את מסוף Admin של GetGet ומריצים את הפקודה הבאה:

Install-Package Google.Apis.Dfareporting.v3_4

מידע נוסף

Java

מתקינים את ספריית הלקוח העדכנית של Campaign Manager 360 API עבור Java. מומלץ להשתמש ב-Maven כדי לנהל את ההתקנה.

מוסיפים את התלות הבאות לקובץ 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>

מידע נוסף

PHP‏

מתקינים את ספריית הלקוח העדכנית של Campaign Manager 360 API עבור PHP. מומלץ להשתמש במלחין כדי לנהל את ההתקנה.

פותחים מסוף ומריצים את הפקודה הבאה:

composer require google/apiclient

אם כבר התקנתם את הספרייה ואתם רק רוצים לעדכן לגרסה האחרונה:

composer update google/apiclient

בהתאם למערכת שלכם, יכול להיות שתצטרכו להוסיף sudo לתחילת הפקודות האלה.

מידע נוסף

Python

מתקינים את ספריית הלקוח העדכנית של Campaign Manager 360 API עבור Python. מומלץ להשתמש ב-pip כדי לנהל את ההתקנה.

פותחים מסוף ומריצים את הפקודה הבאה:

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

בהתאם למערכת שלכם, יכול להיות שתצטרכו להוסיף sudo לתחילת הפקודות האלה.

מידע נוסף

Ruby

מתקינים את ספריית הלקוח העדכנית של Campaign Manager 360 API עבור Ruby. מומלץ להשתמש ב-RubyGems כדי לנהל את ההתקנה.

פותחים מסוף ומריצים את הפקודה הבאה:

gem install google-api-client

אם כבר התקנתם את הספרייה ואתם רק רוצים לעדכן לגרסה האחרונה:

gem update -y google-api-client

בהתאם למערכת שלכם, יכול להיות שתצטרכו להוסיף sudo לתחילת הפקודות האלה.

מידע נוסף

שפות נוספות נתמכות בדף ספריות לקוח.

שליחת בקשה

כשפרטי הכניסה ל-OAuth 2.0 נוצרים וספריית הלקוח מותקנת, אתם מוכנים להתחיל להשתמש ב-Campaign Manager 360 API. בהמשך מוסבר איך לתת הרשאה, להגדיר את הלקוח ולהגיש את הבקשה הראשונה.

C#‎

  1. טוענים את קובץ הסודות של הלקוחות ויוצרים פרטי כניסה להרשאה.

    בפעם הראשונה שתבצעו את השלב הזה, תתבקשו לאשר את הבקשה בדפדפן. לפני האישור, יש לוודא שהתחברת עם חשבון Google שיש לו גישה ל-Campaign Manager 360. האפליקציה שלך תקבל הרשאה לגשת לנתונים בשם החשבון שמחובר כרגע.

    // 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. יצירת לקוח מורשה של DfaReporting.

    // 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. מבצעים פעולה.

    // 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. טוענים את קובץ הסודות של הלקוחות ויוצרים פרטי כניסה להרשאה.

    בפעם הראשונה שתבצעו את השלב הזה, תתבקשו לאשר את הבקשה בדפדפן. לפני האישור, יש לוודא שהתחברת עם חשבון Google שיש לו גישה ל-Campaign Manager 360. האפליקציה שלך תקבל הרשאה לגשת לנתונים בשם החשבון שמחובר כרגע.

    // 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. יצירת לקוח מורשה של DfaReporting.

    // 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. מבצעים פעולה.

    // 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. טוענים את קובץ הסודות של הלקוחות ויוצרים פרטי כניסה להרשאה.

    בפעם הראשונה שתבצעו את השלב הזה, תתבקשו לאשר את הבקשה בדפדפן. לפני האישור, יש לוודא שהתחברת עם חשבון Google שיש לו גישה ל-Campaign Manager 360. האפליקציה שלך תקבל הרשאה לגשת לנתונים בשם החשבון שמחובר כרגע.

    // 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. יצירת לקוח מורשה של DfaReporting.

    // Create a Dfareporting service object.
    $service = new Google_Service_Dfareporting($client);
    
  3. מבצעים פעולה.

    // 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. טוענים את קובץ הסודות של הלקוחות ויוצרים פרטי כניסה להרשאה.

    בפעם הראשונה שתבצעו את השלב הזה, תתבקשו לאשר את הבקשה בדפדפן. לפני האישור, יש לוודא שהתחברת עם חשבון Google שיש לו גישה ל-Campaign Manager 360. האפליקציה שלך תקבל הרשאה לגשת לנתונים בשם החשבון שמחובר כרגע.

    # 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. יצירת לקוח מורשה של DfaReporting.

    # Construct a service object via the discovery service.
    service = discovery.build('dfareporting', 'v4', http=http)
    
  3. מבצעים פעולה.

    # 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. טוענים את קובץ הסודות של הלקוחות ויוצרים פרטי כניסה להרשאה.

    בפעם הראשונה שתבצעו את השלב הזה, תתבקשו לאשר את הבקשה בדפדפן. לפני האישור, יש לוודא שהתחברת עם חשבון Google שיש לו גישה ל-Campaign Manager 360. האפליקציה שלך תקבל הרשאה לגשת לנתונים בשם החשבון שמחובר כרגע.

    # 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. יצירת לקוח מורשה של DfaReporting.

    # 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. מבצעים פעולה.

    // 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);
    }
    

מידע נוסף

כדי לקבל מידע נוסף על כל השירותים ש-API מציע, אתם יכולים לעיין בהפניית API. כל דף של פרטי שיטה מכיל API API מוטמע, שבאמצעותו אפשר לשלוח בקשות בדיקה ישירות מהדפדפן.

תוכלו לעיין במדריכים הנוספים שלנו שעוסקים בנושאים מתקדמים, ומספקים דוגמאות מקצה לקצה למשימות נפוצות.

כשתהיו מוכנים להתחיל לכתוב קוד, אתם מוזמנים לעיין באוסף המקיף של דוגמאות הקוד שלנו, שאנחנו עשויים לשנות ולהתאים אותו לצרכים שלכם.