You are here

public function InstagramOAuth::get_access_token in Drupagram 7

Same name and namespace in other branches
  1. 6 drupagram.lib.php \InstagramOAuth::get_access_token()

Retrieves the access token.

Parameters

string $code:

string $redirect_uri:

string $grant_type:

Return value

type

File

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

Class

InstagramOAuth
A class to provide OAuth enabled access to the Instagram API

Code

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