//please make code workable
using system; using system.collections.generic; using system.linq; using system.text; using system.net; using system.io; using system.xml.linq; using system.globalization; namespace googleanalyticssupport { public class reportrequestorwithsorting { #region fields //i getting bad request error private static readonly string requesturlformat = "https://www.googleapis.com/analytics/v3/data?ids={1}&metrics={2}&sort={3}&start-date={4}&end-date={5}&start-index={6}&max-results={7}"; private static readonly string authurlformat = "accounttype=google&email={0}&passwd={1}&source=reimers.dk-analyticsreader-0.1&service=analytics"; private static cultureinfo ci = cultureinfo.getcultureinfo("en-us"); private string _token = null; private string _username = null; private string _password = null; #endregion #region constructor public reportrequestorwithsorting() { } public reportrequestorwithsorting(string email, string password) { _username = email; _password = password; } #endregion #region properties public string email { { return _username; } set { if (!string.equals(_username, value)) { _username = value; _token = null; } } } public string password { { return _password; } set { if (!string.equals(_password, value)) { _password = value; _token = null; } } } #endregion #region methods \\struggling replace new authentication method\\ private string gettoken(string username, string password) { if (string.isnullorempty(_username) || string.isnullorempty(_password)) { throw new argumentnullexception("username, password", "username and/or password not set"); } string authbody = string.format(authurlformat, username, password); httpwebrequest req = (httpwebrequest)httpwebrequest.create("https://accounts.google.com/o/oauth2/auth"); req.method = "post"; req.contenttype = "application/x-www-form-urlencoded"; req.useragent = "example.dk req"; stream stream = req.getrequeststream(); streamwriter sw = new streamwriter(stream); sw.write(authbody); sw.close(); sw.dispose(); httpwebresponse response = (httpwebresponse)req.getresponse(); streamreader sr = new streamreader(response.getresponsestream()); string token = sr.readtoend(); string[] tokens = token.split(new string[] { "\n" }, stringsplitoptions.removeemptyentries); foreach (string item in tokens) { if (item.startswith("auth=")) { return item.replace("auth=", ""); } } return string.empty; } public ienumerable<analyticsaccountinfo> getaccounts() { if (string.isnullorempty(_token)) { _token = gettoken(_username, _password); } httpwebrequest req = (httpwebrequest)httpwebrequest.create("https://www.googleapis.com/analytics/v2.4/management/accounts"); req.headers.add("authorization: googlelogin auth=" + _token); httpwebresponse response = (httpwebresponse)req.getresponse(); stream responsestream = response.getresponsestream(); streamreader sr = new streamreader(responsestream); string responsexml = sr.readtoend(); xdocument doc = xdocument.parse(responsexml); xnamespace dxpspace = doc.root.getnamespaceofprefix("dxp"); xnamespace defaultspace = doc.root.getdefaultnamespace(); var entries = en in doc.root.descendants(defaultspace + "entry") select new analyticsaccountinfo { accountid = en.elements(dxpspace + "property").where(xe => xe.attribute("name").value == "ga:accountid").first().attribute("value").value, accountname = en.elements(dxpspace + "property").where(xe => xe.attribute("name").value == "ga:accountname").first().attribute("value").value, id = en.element(defaultspace + "id").value, title = en.element(defaultspace + "title").value, profileid = en.elements(dxpspace + "property").where(xe => xe.attribute("name").value == "ga:profileid").first().attribute("value").value, webpropertyid = en.elements(dxpspace + "property").where(xe => xe.attribute("name").value == "ga:webpropertyid").first().attribute("value").value }; return entries; } private xdocument getreport(analyticsaccountinfo account, ienumerable<dimension> dimensions, ienumerable<metric> metrics , ienumerable<sort> sorts, datetime from, datetime to, int startindex = 1, int maxresults = 250) { if (string.isnullorempty(_token)) { _token = gettoken(_username, _password); } stringbuilder dims = new stringbuilder(); foreach (dimension item in dimensions) { dims.append("ga:" + item.tostring() + ","); } stringbuilder mets = new stringbuilder(); foreach (metric item in metrics) { mets.append("ga:" + item.tostring() + ","); } stringbuilder srt = new stringbuilder(); foreach (sort item in sorts) { srt.append("-" + "ga:" + item.tostring() + ","); } string requesturl = string.format(requesturlformat, "ga:" + account.profileid, dims.tostring().trim(",".tochararray()), mets.tostring().trim(",".tochararray()) , srt.tostring().trim(",".tochararray()), from.tostring("yyyy-mm-dd"), to.tostring("yyyy-mm-dd"), startindex, maxresults); httpwebrequest req = (httpwebrequest)httpwebrequest.create(requesturl); req.headers.add("authorization: googlelogin auth=" + _token); httpwebresponse response = (httpwebresponse)req.getresponse(); stream responsestream = response.getresponsestream(); string responsexml = new streamreader(responsestream, encoding.utf8, true).readtoend(); xdocument doc = xdocument.parse(responsexml); return doc; } public ienumerable<genericentry> reportrequestwf(analyticsaccountinfo account, ienumerable<dimension> dimensions, ienumerable<metric> metrics , ienumerable<sort> sorts, datetime from, datetime to, int startindex = 1, int maxresults = 250) { xdocument doc = getreport(account, dimensions, metrics , sorts, from, to, startindex, maxresults); xnamespace dxpspace = doc.root.getnamespaceofprefix("dxp"); xnamespace defaultspace = doc.root.getdefaultnamespace(); var gr = r in doc.root.descendants(defaultspace + "entry") select new genericentry { dimensions = new list<keyvaluepair<dimension, string>>( rd in r.elements(dxpspace + "dimension") select new keyvaluepair<dimension, string>( (dimension)enum.parse( typeof(dimension), rd.attribute("name").value.replace("ga:", ""), true), rd.attribute("value").value)), metrics = new list<keyvaluepair<metric, string>>( rm in r.elements(dxpspace + "metric") select new keyvaluepair<metric, string>( (metric)enum.parse(typeof(metric), rm.attribute("name").value.replace("ga:", ""), true), rm.attribute("value").value)), sorts = new list<keyvaluepair<sort, string>>( rs in r.elements(dxpspace + "sort") select new keyvaluepair<sort, string>( (sort)enum.parse(typeof(sort), rs.attribute("name").value.replace("ga:", ""), true), rs.attribute("value").value)) }; return gr; } #endregion } } just created generic, dimension , metrics class files , called them here.
that code not work. appear trying authenticate client login (login , password) google shut down client login april 2015 can no longer use client login access google apis.
you need move using oauth2 or service account
normaly recomend using .net client library appear trying merge ssis @ time .net client library not strong name signed wont work ssis. have manually recreate oauth2 flow.
useful links
- tutorial: google 3 legged oauth2 flow
- custom google analytics ssis task , connection manager (note developer on project)
Comments
Post a Comment