/**
  * @return an access token given a previously requested authorization code
  */
 public static function requestAccessToken($client_id, $client_secret, $code, $redirect_uri = NULL)
 {
     //create a new client without having a token
     $client = new Client(array('client_id' => $client_id, 'client_secret' => $client_secret, 'redirect_uri' => $redirect_uri));
     //reset/init any token, just in case
     $client->initToken();
     //authorize the api, ie: get a token
     if (!$client->authorize_api($code)) {
         throw new Exception($client->getError());
     }
     return $client->getToken();
 }
 /**
  * create custom settings form fields for accesstoken
  *  
  * @param mixed $key
  * @param mixed $data
  * 
  * @return String $html
  */
 public function generate_password_html($key, $data)
 {
     global $woocommerce;
     $html = '';
     $data['title'] = isset($data['title']) ? $data['title'] : '';
     $data['disabled'] = empty($data['disabled']) ? false : true;
     $data['class'] = isset($data['class']) ? $data['class'] : '';
     $data['css'] = isset($data['css']) ? $data['css'] : '';
     $data['placeholder'] = isset($data['placeholder']) ? $data['placeholder'] : '';
     $data['type'] = isset($data['type']) ? $data['type'] : 'text';
     $data['desc_tip'] = isset($data['desc_tip']) ? $data['desc_tip'] : false;
     $data['description'] = isset($data['description']) ? $data['description'] : '';
     // Description handling
     if ($data['desc_tip'] === true) {
         $description = '';
         $tip = $data['description'];
     } elseif (!empty($data['desc_tip'])) {
         $description = $data['description'];
         $tip = $data['desc_tip'];
     } elseif (!empty($data['description'])) {
         $description = $data['description'];
         $tip = '';
     } else {
         $description = $tip = '';
     }
     // Custom attribute handling
     $custom_attributes = array();
     if (!empty($data['custom_attributes']) && is_array($data['custom_attributes'])) {
         foreach ($data['custom_attributes'] as $attribute => $attribute_value) {
             $custom_attributes[] = esc_attr($attribute) . '="' . esc_attr($attribute_value) . '"';
         }
     }
     $html .= '<tr valign="top">' . "\n";
     $html .= '<th scope="row" class="titledesc">';
     $html .= '<label for="' . esc_attr($this->plugin_id . $this->id . '_' . $key) . '">' . wp_kses_post($data['title']) . '</label>';
     if ($tip) {
         $html .= '<img class="help_tip" data-tip="' . esc_attr($tip) . '" src="' . $woocommerce->plugin_url() . '/assets/images/help.png" height="16" width="16" />';
     }
     $token = $this->client->getToken();
     if (isset($_GET['code'])) {
         $this->client->initToken();
         $b_auth = $this->client->authorize_api();
         if ($b_auth) {
             $token = $this->client->getToken();
         } else {
             var_dump($this->client->getError());
         }
     }
     $token = isset($token) ? $token : $this->get_option($key);
     $html .= '</th>' . "\n";
     $html .= '<td class="forminp">' . "\n";
     $html .= '<fieldset><legend class="screen-reader-text"><span>' . wp_kses_post($data['title']) . '</span></legend>' . "\n";
     $html .= '<input class="input-text regular-input ' . esc_attr($data['class']) . '" type="' . esc_attr($data['type']) . '" name="' . esc_attr($this->plugin_id . $this->id . '_' . $key) . '" id="' . esc_attr($this->plugin_id . $this->id . '_' . $key) . '" style="' . esc_attr($data['css']) . '" value="' . esc_attr($token) . '" placeholder="' . esc_attr($data['placeholder']) . '" ' . disabled($data['disabled'], true, false) . ' ' . implode(' ', $custom_attributes) . ' />';
     $html .= '<a style="margin-left: 10px" href="#" class="button-primary" onclick="getAuthUrl();"> Get Access Token from GoCoin</a>';
     if ($description) {
         $html .= ' <p class="description">' . wp_kses_post($description) . '</p>' . "\n";
     }
     $html .= '</fieldset>';
     $html .= '</td>' . "\n";
     $html .= '</tr>' . "\n";
     return $html;
 }