All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.facebook.ads.sdk.APIContext Maven / Gradle / Ivy

There is a newer version: 2.11.1
Show newest version
/**
 * Copyright (c) 2015-present, Facebook, Inc. All rights reserved.
 *
 * You are hereby granted a non-exclusive, worldwide, royalty-free license to
 * use, copy, modify, and distribute this software in source code or binary
 * form for use in connection with the web services and APIs provided by
 * Facebook.
 *
 * As with any software that integrates with the Facebook platform, your use
 * of this software is subject to the Facebook Developer Principles and
 * Policies [http://developers.facebook.com/policy/]. This copyright notice
 * shall be included in all copies or substantial portions of the software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 */
package com.facebook.ads.sdk;

import java.io.PrintStream;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;


public class APIContext {
  public static final String DEFAULT_API_BASE = APIConfig.DEFAULT_API_BASE;
  public static final String DEFAULT_API_VERSION = APIConfig.DEFAULT_API_VERSION;
  public static final String DEFAULT_VIDEO_API_BASE = APIConfig.DEFAULT_VIDEO_API_BASE;
  private String endpointBase;
  private String videoEndpointBase;
  private String accessToken;
  private String appSecret;
  private String version;
  protected boolean isDebug = false;
  protected PrintStream logger = System.out;

  public APIContext(String endpointBase, String videoEndpointBase, String version, String accessToken, String appSecret) {
    this.version = version;
    this.endpointBase = endpointBase;
    this.videoEndpointBase = videoEndpointBase;
    this.accessToken = accessToken;
    this.appSecret = appSecret;
  }

  public APIContext(String accessToken) {
    this(DEFAULT_API_BASE, DEFAULT_VIDEO_API_BASE, DEFAULT_API_VERSION, accessToken, null);
  }

  public APIContext(String accessToken, String appSecret) {
    this(DEFAULT_API_BASE, DEFAULT_VIDEO_API_BASE, DEFAULT_API_VERSION, accessToken, appSecret);
  }

  public String getEndpointBase() {
    return this.endpointBase;
  }

  public String getVideoEndpointBase() {
    return this.videoEndpointBase;
  }

  public String getAccessToken() {
    return this.accessToken;
  }

  public String getAppSecret() {
    return this.appSecret;
  }

  public boolean hasAppSecret() {
    return appSecret != null;
  }

  public String getAppSecretProof() {
    return sha256(appSecret, accessToken);
  }

  public String getVersion() {
    return this.version;
  }

  public boolean isDebug() {
    return this.isDebug;
  }

  public APIContext enableDebug(boolean isDebug) {
    this.isDebug = isDebug;
    return this;
  }

  public PrintStream getLogger() {
    return this.logger;
  }

  public APIContext setLogger(PrintStream logger) {
    this.logger = logger;
    return this;
  }

  public void log(String s) {
    if (isDebug && logger != null) logger.println(s);
  }

  public static String sha256(String secret, String message) {
    try {
      Mac sha256HMAC = Mac.getInstance("HmacSHA256");
      SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
      sha256HMAC.init(secretKey);
      byte[] bytes = sha256HMAC.doFinal(message.getBytes());
      return toHex(bytes);
    } catch(Exception e){
      throw new IllegalArgumentException(
        "The provided app secret or access token is invalid!"
      );
    }
  }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy