Code covered by the BSD License  

Highlights from
View and add transaction to portfolios on Google Finance

image thumbnail

View and add transaction to portfolios on Google Finance

by

ted p teng (view profile)

 

10 Jul 2011 (Updated )

View and add transactions to portfolios on Google Finance. A Google account will be required.

[authorized, aToken]=connectAndAuthorize(userName, password, service)
function [authorized, aToken]=connectAndAuthorize(userName, password, service)
% this gets the authorization token from Google given the user info and
% specified service.
% below code is derived from 
% file: gcaleventor.m
%   author: Ofir Perry Bibi's work,
%   code location: http://www.mathworks.com/matlabcentral/fileexchange/25698-create-google-calendar-event-with-sms-and-email-notification
% and also: Google Finance API protocol documents

import java.io.*;
import java.net.*;
import java.lang.*;
loginurlString = 'https://www.google.com/accounts/ClientLogin';
params = {'Email', 'Passwd', 'source', 'service'};
vals = {userName,password,'MatlabCal-1',service};
authorized = true;
try  
    % open url connection
    url = URL( loginurlString );
    con = url.openConnection();
    % set up url connection to get retrieve information back
    con.setRequestMethod( 'POST' );
    con.setDoOutput( true );
    con.setRequestProperty('content-type','application/x-www-form-urlencoded');
    ps = PrintStream(con.getOutputStream());
    for i=1:length(params)
        if (i > 0)
            ps.print('&');
        end
        param = URLEncoder.encode(params{i}, 'UTF-8');
        value = URLEncoder.encode(vals{i}, 'UTF-8');
        ps.print(param.concat('=').concat(value));
    end
    ps.close();
    % pull the information back from the URL
    is = con.getInputStream();
    buf = java.lang.StringBuffer();
    c = int32(is.read());
    while( c ~= -1 )
        buf.append( char(c) );
        c = int32(is.read());
    end
    con.disconnect();
    % get the Authorization Token
    aToken=java.lang.String('auth').concat(buf.substring(buf.lastIndexOf('Auth')+4,buf.length()-1));
catch ME
    disp(['from line: ' num2str(ME.stack.line)]);
    error(ME.message);
end

end

Contact us