                     $services_oauth[$method_name]['permission'] = 0;      
         }
    }
  $services_oauth_serialized = serialize($services_oauth);  
  //drupal_set_message('<pre>'.print_r($user->sid, TRUE).'</pre>');
  db_query("INSERT INTO { oauth_services } (uid, consumer_key, services, timestamp, sid ) VALUES (%d, '%s', '%s', %d, '%s')", $user->uid, $form_state['values']['oauth_consumer_key'], $services_oauth_serialized, $form_state['values']['oauth_nonce_timestamp'], $user->sid) ; 	
  
  $server = _oauth_init_server();
  $q = $_GET['q'];
  unset($_GET['q']);
  try {
    $req = OAuthRequest::from_request();
    oauth_authorize_request_token($form_state['values']['oauth_token']);    
    //drupal_goto($req->get_parameter('oauth_callback'));
  } catch (OAuthException $e) {
    print($e->getMessage() . "\n<hr />\n");
    print_r($req);
    die();
  }
  // Set the $_GET['q'] back to it's original value
  $_GET['q'] = $q;
  
  $form_state['redirect'] = $form_state['values']['oauth_callback'];
}

/**
 * Authorize a request token
 *
 * Set the authorized property for the token, to allow an access token to be generated, 
 * also set the UID of the request token.
 *
 * @param key
 *   Request token key.
 */
function oauth_authorize_request_token($key) {
  global $user;
  db_query("UPDATE {oauth_token} SET authorized = 1, uid=%d WHERE token_key = '%s'", $user->uid, $key);
  }
