You are here

class InstagramOAuth in Drupagram 7

Same name and namespace in other branches
  1. 6 drupagram.lib.php \InstagramOAuth

A class to provide OAuth enabled access to the Instagram API

Hierarchy

Expanded class hierarchy of InstagramOAuth

File

./drupagram.lib.php, line 448
Classes to implement the full Instagram API

View source
class InstagramOAuth extends Instagram {
  protected $client_id;
  protected $client_secret;
  protected $redirect_uri;
  protected $token;
  protected $access_token = NULL;
  protected $auth_user = array();

  /**
   * Constructor for the InstagramOAuth class
   */
  public function __construct($client_id = NULL, $client_secret = NULL, $redirect_uri = NULL, $access_token = NULL) {
    if (empty($client_id) || empty($client_secret)) {
      throw new InstagramException(t('You need to configure your Client ID and/or Client Secret keys.'));
    }
    $this->client_id = $client_id;
    $this->client_secret = $client_secret;
    if (isset($redirect_uri)) {
      $this->redirect_uri = $redirect_uri;
    }
    if (isset($access_token)) {
      $this->access_token = $access_token;
    }
  }

  /**
   * Requests token. Not implemented with Instagram yet.
   */
  public function get_request_token() {
    $url = $this
      ->create_url('oauth/request_token', '');
    try {
      $response = $this
        ->auth_request($url);
    } catch (InstagramException $e) {
    }
    parse_str($response, $token);
    $this->token = $token;
    $this->access_token = $token['access_token'];
    $this->auth_user = new InstagramUser($token['user']);
    return $token;
  }

  /**
   * Returns the properly formatted authorization url.
   *
   * @param string $redirect_uri. URI to redirect the user to after authorization
   * @param array $scope. Items can be: 'basic', 'comments', 'relationships' and 'likes'
   * @param string $response_type. Currently only 'code' is supported
   * @return string. Propertly formatted authorization url.
   */
  public function get_authorize_url($redirect_uri = NULL, $scope = array(
    'basic',
    'comments',
    'relationships',
    'likes',
  ), $response_type = 'code') {
    $url = $this
      ->create_url('oauth/authorize', '');
    $url .= '?client_id=' . $this->client_id;
    $url .= '&response_type=' . $response_type;
    if (isset($redirect_uri)) {
      $url .= '&redirect_uri=' . $redirect_uri;
    }
    if (isset($scope)) {
      $url .= '&scope=' . implode('+', $scope);
    }
    return $url;
  }

  /**
   * Returns the properly formatted authentication url
   */
  public function get_authenticate_url() {
    $url = $this
      ->create_url('oauth/authenticate', '');
    if (!empty($this->token)) {
      $url .= '?access_token=' . $this->token['access_token'];
    }
    return $url;
  }

  /**
   * Retrieves the access token.
   *
   * @param string $code
   * @param string $redirect_uri
   * @param string $grant_type
   * @return type
   */
  public function get_access_token($code, $redirect_uri, $grant_type = 'authorization_code') {
    if ($this->access_token !== NULL) {
      return $this->access_token;
    }
    $url = $this
      ->create_url('oauth/access_token', '');
    $params = array(
      'client_id' => $this->client_id,
      'client_secret' => $this->client_secret,
      'grant_type' => $grant_type,
      'redirect_uri' => $redirect_uri,
      'code' => $code,
    );
    try {
      $headers = array(
        'Content-Type' => 'application/x-www-form-urlencoded',
      );
      $response = $this
        ->auth_request($url, $params, 'POST', FALSE, $headers);
    } catch (InstagramException $e) {
      watchdog('drupagram OAuth', '!message', array(
        '!message' => $e
          ->__toString(),
      ), WATCHDOG_ERROR);
      return FALSE;
    }
    $token = json_decode($response, TRUE);
    $this->token = $token;
    $this->access_token = $token['access_token'];
    $token['user']['oauth_token'] = $token['access_token'];
    $this->auth_user = new InstagramUser($token['user']);
    return $token;
  }
  public function auth_request($url, $params = array(), $method = 'POST', $use_auth = TRUE, $headers = array()) {
    return $this
      ->request($url, $params, $method, $use_auth, $headers);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Instagram::$current_user protected property OAuth user object
Instagram::$endpoints protected property
Instagram::$format protected property
Instagram::$oauth_token protected property JSON encoded OAuth token
Instagram::$password protected property
Instagram::$source protected property
Instagram::$username protected property
Instagram::call public function Method for calling any drupagram api resource
Instagram::create_url protected function
Instagram::fetch protected function Get an array of Instagram objects from an API endpoint
Instagram::parse_response protected function
Instagram::request protected function Perform a request
Instagram::RESPONSE_CODE_PARAM constant The name of the GET param that holds the authentication code
Instagram::self_feed public function See the authenticated user's feed. GET /users/self/feed
Instagram::self_liked public function See the authenticated user's list of media they've liked. Note that this list is ordered by the order in which the user liked the media. Private media is returned as long as the authenticated user has permission to view that media. Liked…
Instagram::set_auth public function Set the username and password
Instagram::user_feed public function Fetch a user's feed
Instagram::user_info public function Get basic information about a user.
Instagram::user_lookup public function
Instagram::user_recent public function Get the most recent media published by a user. GET /users/{user-id}/media/recent
Instagram::user_search public function Search for a user by name. GET /users/search
InstagramOAuth::$access_token protected property Decoded plain access token Overrides Instagram::$access_token
InstagramOAuth::$auth_user protected property
InstagramOAuth::$client_id protected property
InstagramOAuth::$client_secret protected property
InstagramOAuth::$redirect_uri protected property
InstagramOAuth::$token protected property
InstagramOAuth::auth_request public function Perform an authentication required request. Overrides Instagram::auth_request
InstagramOAuth::get_access_token public function Retrieves the access token.
InstagramOAuth::get_authenticate_url public function Returns the properly formatted authentication url
InstagramOAuth::get_authorize_url public function Returns the properly formatted authorization url.
InstagramOAuth::get_request_token public function Requests token. Not implemented with Instagram yet.
InstagramOAuth::__construct public function Constructor for the InstagramOAuth class Overrides Instagram::__construct