/* NOTE THE PARTS MARKED 'CHANGE THIS' */ CREATE OR REPLACE package pkg_gcal is procedure gcal_redirect(token in varchar2, page in varchar2); procedure gcal_authenticate; function gcal_get_session_token(token in varchar2) return varchar2; procedure gcal_postevent(token in varchar2); end; CREATE OR REPLACE package body pkg_gcal is /* gcal_redirect - this procedure is used as the 'callback' URL called by Google after successful authentication */ procedure gcal_redirect(token in varchar2, page in varchar2) is v_temp_page varchar2(1000); begin v_temp_page := replace(page, 'TOKEN_VALUE', token); owa_util.redirect_url(v_temp_page); end gcal_redirect; function gcal_get_session_token(token in varchar2) return varchar2 is l_request utl_http.req; l_response utl_http.resp; v_auth_url varchar2(100) := 'https://www.google.com/accounts/AuthSubSessionToken'; v_result varchar2(200); hname varchar2(255); hvalue varchar2(1023); i integer; v_msg varchar2(80); v_token varchar2(80) := ''; begin utl_http.set_response_error_check(enable=>true); utl_http.set_wallet('file:/etc/oracle/wallets', 'yourpassword'); -- CHANGE THIS l_request := utl_http.begin_request(url=>v_auth_url, method=>'GET'); utl_http.set_header(r=>l_request, name=>'Content-Type', value=>'application/x-www-form-urlencoded'); utl_http.set_header(r=>l_request, name=> 'Authorization', value=>'AuthSub token="' || token || '"'); l_response := utl_http.get_response(r=>l_request); dbms_output.put_line('Response: ' || l_response.status_code); begin loop utl_http.read_line ( r => l_response, data => v_msg ); Dbms_Output.Put_Line ( v_msg ); if regexp_instr(v_msg, 'Token=') is not null then v_token := regexp_replace(v_msg, 'Token=', ''); end if; end loop; exception when utl_http.end_of_body then null; end; utl_http.end_response(l_response); return v_token; end gcal_get_session_token; /* gcal_authenticate - this procedure performs the initial redirection to the Google authentication page, passing across the URL to the gcal_redirect page */ procedure gcal_authenticate is v_redirect_url varchar2(2000); v_base_url varchar2(500) := 'https://www.google.com/accounts/AuthSubRequest?next='; v_next_url varchar2(500) := 'http://yourhost/pls/apex/yourschema.pkg_gcal.gcal_redirect?page='; -- CHANGE THIS v_this_page varchar2(500); begin -- obtain a link back to this page using the CGI Env variables v_this_page := 'http://' || owa_util.get_cgi_env('HTTP_HOST') || owa_util.get_cgi_env('SCRIPT_NAME') || owa_util.get_cgi_env('PATH_INFO') || '?' || owa_util.get_cgi_env('QUERY_STRING'); -- append the URL for this page onto the gcal_redirect URL v_next_url := v_next_url || v_this_page; -- encode the URL to take care of any special characters v_next_url := apex_util.url_encode(v_next_url); -- Append parameters to set the scope and the type of Token we want (a session token) v_redirect_url := v_base_url || v_next_url || '&scope=http%3A%2F%2Fwww.google.com%2Fcalendar%2Ffeeds%2F&session=1&secure=0'; -- perform the redirect owa_util.redirect_url(v_redirect_url); end gcal_authenticate; procedure gcal_postevent(token in varchar2) is l_request utl_http.req; l_response utl_http.resp; v_auth_url varchar2(100) := 'http://www.google.com/calendar/feeds/default/private/full'; v_auth_text varchar2(200) := 'AuthSub token="' || token || '"'; v_entry varchar2(2000) := '' || '' || 'Tennis with Beth' || 'Meet for a quick lesson.' || '' || 'Jo March' || 'jo@gmail.com' || '' || '' || '' || '' || '' || '' || '' || ''; begin utl_http.set_response_error_check(enable=>true); l_request := utl_http.begin_request(url=>v_auth_url, method=>'POST'); utl_http.set_header(r=>l_request, name=>'Content-Type', value=>'application/atom+xml'); utl_http.set_header(r=>l_request, name=> 'Authorization', value=>v_auth_text); utl_http.set_header(r=>l_request, name=>'Content-Length', value=>length(v_entry)); utl_http.write_text(r=>l_request, data=>v_entry); l_response := utl_http.get_response(r=>l_request); dbms_output.put_line(l_response.status_code); dbms_output.put_line('Reason: ' || l_response.reason_phrase); end gcal_postevent; end pkg_gcal;