Edits history of script submission #11322 for ' Get targeting options (pinterest)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Pinterest = {
      token: string;
    };
    /**
     * Get targeting options
     * You can use targeting values in ads placement to define your intended audience.
     */
    export async function main(
      auth: Pinterest,
      targeting_type:
        | "APPTYPE"
        | "GENDER"
        | "LOCALE"
        | "AGE_BUCKET"
        | "LOCATION"
        | "GEO"
        | "INTEREST"
        | "KEYWORD"
        | "AUDIENCE_INCLUDE"
        | "AUDIENCE_EXCLUDE",
      client_id: string | undefined,
      oauth_signature: string | undefined,
      timestamp: string | undefined,
      ad_account_id: string | undefined,
    ) {
      const url = new URL(
        `https://api.pinterest.com/v5/resources/targeting/${targeting_type}`,
      );
      for (const [k, v] of [
        ["client_id", client_id],
        ["oauth_signature", oauth_signature],
        ["timestamp", timestamp],
        ["ad_account_id", ad_account_id],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "GET",
        headers: {
          Authorization: "Bearer " + auth.token,
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 536 days ago