<docbook><section><title>VirtuosoOdsControllersClientApi</title><bridgehead class="http://www.w3.org/1999/xhtml:h3">Simple Client Written in VSP</bridgehead>
<programlisting>&lt;html&gt;
&lt;?vsp
    declare consumer_key, consumer_secret, oauth_token, oauth_secret, signature, timest, nonce varchar;
    declare srv, res, signature_base, ret_url, url, tmp, sid varchar;
    declare meth, pars any;
    consumer_key := {?&#39;key&#39;};
    srv := sprintf (&#39;http://localhost:%s/OAuth/&#39;, server_http_port ());
    sid := null;
    res := &#39;&#39;;

    sid :=  {?&#39;sid&#39;};
    if (sid = &#39;&#39;)
      sid := null;  
    else
      {
        -- if selected token is not same as one from the session we need to get new authentication token
        if (consumer_key &lt;&gt; OAUTH..get_consumer_key (sid))
     {
       OAUTH..session_terminate (sid);
            sid := null;
     }
      }

    meth := get_keyword (&#39;meth&#39;, params, &#39;weblog.post.new&#39;);;
    pars := get_keyword (&#39;pars&#39;, params, &#39;inst_id=15&amp;title=testing&amp;description=Some test post&#39;);;

    if ({?&#39;rt&#39;} is not null and sid is null) -- request new token for the session
      {
   url := srv||&#39;request_token&#39;;
   url := OAUTH..sign_request (&#39;GET&#39;, url, &#39;&#39;, consumer_key, sid);
        res := http_get (url);
   sid := OAUTH..parse_response (sid, consumer_key, res);
   OAUTH..set_session_data (sid, params);
   ret_url := sprintf (&#39;http://localhost:%s/oauth/oauth_client.vsp?ready=%U&#39;, server_http_port (), sid);
   -- call authorize
        url := sprintf (&#39;%sauthorize?oauth_token=%U&amp;oauth_callback=%U&#39;, srv, OAUTH..get_auth_token (sid), ret_url);
   http_status_set (301);
        http_header (sprintf (&#39;Location: %s\r\n&#39;, url));
        return;
      }
    else if ({?&#39;ready&#39;} is not null) -- get access token
      {
   -- we go here when token above is authorized
        sid := {?&#39;ready&#39;};
   url := srv||&#39;access_token&#39;;
   consumer_key := OAUTH..get_consumer_key (sid);
   url := OAUTH..sign_request (&#39;GET&#39;, url, &#39;&#39;, consumer_key, sid);
        res := http_get (url);
   sid := OAUTH..parse_response (sid, consumer_key, res);

      }
    if (sid is not null) -- access token is ready, and we can call API
      {
        -- here we are ready to call service
   if ({?&#39;rt&#39;} is null)
          {
            tmp := OAUTH..get_session_data (sid);
            pars := get_keyword (&#39;pars&#39;, tmp, pars);
       meth := get_keyword (&#39;meth&#39;, tmp, meth);
     }
   url := sprintf (&#39;http://localhost:%s/ods/api/%s&#39;, server_http_port (), meth);
   tmp := split_and_decode (pars);
   params := &#39;&#39;;
   for (declare i,l int, l:=length (tmp); i &lt; l; i := i + 2)
      {
        params := params || sprintf (&#39;%U=%U&amp;&#39;, tmp[i], tmp[i+1]);
      }
   --params := sprintf (&#39;inst_id=%d&amp;description=%U&amp;title=%U&#39;, 15, &#39;Some test post&#39;, &#39;testing&#39;);
   consumer_key := OAUTH..get_consumer_key (sid);
   url := OAUTH..sign_request (&#39;GET&#39;, url, params, consumer_key, sid);
        res := http_get (url);
        --dbg_obj_print (res);
      }
?&gt;
    &lt;head&gt;&lt;title&gt;OAuth Client&lt;/title&gt;&lt;/head&gt;
    &lt;body&gt;
   &lt;h1&gt;OAuth client for ODS Controllers&lt;/h1&gt;
   &lt;form method=&quot;POST&quot; action=&quot;oauth_client.vsp&quot;&gt;
   &lt;input type=&quot;hidden&quot; name=&quot;sid&quot; value=&quot;&lt;?V sid ?&gt;&quot;/&gt;
   APPLICATION : &lt;br/&gt; &lt;select name=&quot;key&quot;&gt;
   &lt;?vsp for select a_name, a_key from OAUTH..APP_REG do { ?&gt;
            &lt;option value=&quot;&lt;?V a_key ?&gt;&quot; &lt;?vsp if (consumer_key = a_key) http (&#39;selected&#39;); ?&gt;&gt;&lt;?V a_name ?&gt;&lt;/option&gt;
   &lt;?vsp } ?&gt;
              &lt;/select&gt;
   &lt;?vsp
   if (sid is not null) http (sprintf (&#39; TOKEN: %s&#39;, OAUTH..get_auth_token (sid)));
   ?&gt;
   &lt;br/&gt;
   ODS API: &lt;br/&gt;&lt;input type=&quot;text&quot; name=&quot;meth&quot; value=&quot;&lt;?V meth ?&gt;&quot; size=50/&gt; &lt;br/&gt;
   PARAMETERS: &lt;br/&gt; &lt;textarea name=&quot;pars&quot; rows=&quot;5&quot; cols=&quot;50&quot;&gt;&lt;?V pars ?&gt;&lt;/textarea&gt; &lt;br/&gt;
        &lt;input type=&quot;submit&quot; value=&quot;Execute&quot; name=&quot;rt&quot;/&gt;&lt;br/&gt;
   &lt;/form&gt;
   RESULT:
   &lt;hr/&gt;
   &lt;pre&gt;&lt;?V res ?&gt;&lt;/pre&gt;
    &lt;/body&gt;
&lt;/html&gt;
</programlisting></section></docbook>