Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uploading Custom Audience #5

Closed
faisalbasra opened this issue Jan 15, 2016 · 2 comments
Closed

Uploading Custom Audience #5

faisalbasra opened this issue Jan 15, 2016 · 2 comments
Assignees

Comments

@faisalbasra
Copy link

There is no guide or example for creating & using custom audience. Uploading Custom audience of phone number, email, Mobile advertising ID etc.

@JiamingFB JiamingFB self-assigned this Jan 15, 2016
@faisalbasra faisalbasra changed the title Custom Audience Example/Guide Uploading Custom Audience Mar 9, 2016
@JiamingFB
Copy link
Contributor

To create and update the custom audience, we need to construct the payload of the custom audience update request. For details about updating the custom audience, you can check https://developers.intern.facebook.com/docs/marketing-api/reference/custom-audience/users/

Here's some sample code you can refer to. I use JsonObject/Array to construct the payload.

  AdAccount account = new AdAccount(ACCOUNT_ID, context);

  CustomAudience audience = account.createCustomAudience()
    .setName("Java SDK Test Custom Audience")
    .setDescription("Test Audience")
    .setSubtype(EnumSubtype.VALUE_CUSTOM)
    .execute();

  // Audience payload schema
  JsonArray schema = new JsonArray();
  schema.add(new JsonPrimitive("EMAIL_SHA256"));
  schema.add(new JsonPrimitive("PHONE_SHA256"));

  // Audience payload data
  JsonArray personA = new JsonArray();
  personA.add(new JsonPrimitive(sha256("aaa@example.com")));
  personA.add(new JsonPrimitive(sha256("1234567890")));
  JsonArray personB = new JsonArray();
  personB.add(new JsonPrimitive(sha256("bbb@example.com")));
  personB.add(new JsonPrimitive(sha256("1234567890")));
  JsonArray personC = new JsonArray();
  personC.add(new JsonPrimitive(sha256("ccc@example.com")));
  personC.add(new JsonPrimitive(sha256("1234567890")));

  JsonArray data = new JsonArray();
  data.add(personA);
  data.add(personB);
  data.add(personC);

  JsonObject payload = new JsonObject();
  payload.add("schema", schema);
  payload.add("data", data);

  audience.createUser()
    .setPayload(payload.toString())
    .execute();
  public static String sha256(String message) {
    try {
      MessageDigest digest = MessageDigest.getInstance("SHA-256");
      byte[] hash = digest.digest(message.getBytes(StandardCharsets.UTF_8));
      return toHex(hash);
    } catch(Exception e){
      return null;
    }
  }

  public static String toHex(byte[] bytes) {
    StringBuilder sb = new StringBuilder();
    for (byte b : bytes) {
        sb.append(String.format("%1$02x", b));
    }
    return sb.toString();
  }

@faisalbasra
Copy link
Author

Alright. Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants