You are here

function social_content_instagram_token_generator_form in Social Content 7

@file Social Content administration area for Instagram.

Provides menu callbacks for the administration area of Instagram.

1 string reference to 'social_content_instagram_token_generator_form'
social_content_instagram_menu in modules/instagram/social_content_instagram.module
Implements hook_menu().

File

modules/instagram/social_content_instagram.admin.inc, line 9
Social Content administration area for Instagram.

Code

function social_content_instagram_token_generator_form($form, $form_state, $social_content_type) {
  $settings = social_content_get_settings($social_content_type);
  $redirect_url = url(current_path(), array(
    'absolute' => TRUE,
  ));
  $form['description'] = array(
    '#markup' => '<p>' . t('Use this form to generate the Instagram access token.') . '</p>' . '<p>' . t('You can obtain your client id and secret by visiting !instagram_url', array(
      '!instagram_url' => SOCIAL_CONTENT_INSTAGRAM_CLIENT_DETAILS_URL,
    )) . '</p>' . '<p>' . t('Please ensure that you have the following redirect uri in your account settings:') . '</p>' . ' <strong>' . $redirect_url . '</strong>',
  );
  if (isset($_GET['code'])) {
    $fields = array(
      'client_id' => $settings['client_id'],
      'client_secret' => $settings['client_secret'],
      'grant_type' => 'authorization_code',
      'redirect_uri' => $redirect_url,
      'code' => $_GET['code'],
    );
    $result = drupal_http_request(SOCIAL_CONTENT_INSTAGRAM_ACCESS_TOKEN_URL, array(
      'method' => 'post',
      'data' => drupal_http_build_query($fields),
    ));
    $data = json_decode($result->data);
    if ($result->code != 200) {
      drupal_set_message(t('Error: !error', array(
        '!error' => $result->data,
      )), 'error');
    }
    else {
      drupal_set_message(t('New access token is !access_token', array(
        '!access_token' => $data->access_token,
      )));
    }
  }
  $form['id'] = array(
    '#type' => 'textfield',
    '#title' => t('Client ID'),
    '#default_value' => $settings['client_id'],
  );
  $form['secret'] = array(
    '#type' => 'textfield',
    '#title' => t('Client Secret'),
    '#default_value' => $settings['client_secret'],
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Get access token'),
  );
  return $form;
}