Client Login to my Google Analytics Account with C#
The group you are posting to is a
Usenet group . Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
From:
Marty <mburo... @gmail.com>
Date: Tue, 6 Oct 2009 09:16:07 -0700 (PDT)
Local: Tues, Oct 6 2009 12:16 pm
Subject: Client Login to my Google Analytics Account with C#
I'm having a hard time authenticating into my Google Analytics account
with C#. I get a 403 forbidden error message with the following code:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://
www.google.com/accounts/ClientLogin ");
req.Headers.Add("accountType", "GOOGLE");
req.Headers.Add("Email", "my email...");
req.Headers.Add("Passwd", "my password...");
req.Headers.Add("service", "analytics");
req.Headers.Add("source", "test-test-1.00");
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
Any thoughts???
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
"Eric (Google)" <api.e... @google.com>
Date: Tue, 6 Oct 2009 21:52:37 -0700 (PDT)
Local: Wed, Oct 7 2009 12:52 am
Subject: Re: Client Login to my Google Analytics Account with C#
Hmm, well you're adding those parameters as headers.
Rather, you need to add them as form encoded to the body
of the request:
http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html#R...
Eric
On Oct 7, 3:16 am, Marty <mburo... @gmail.com> wrote:
> I'm having a hard time authenticating into my Google Analytics account
> with C#. I get a 403 forbidden error message with the following code:
> HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.google.com/accounts/ClientLogin ");
> req.Headers.Add("accountType", "GOOGLE");
> req.Headers.Add("Email", "my email...");
> req.Headers.Add("Passwd", "my password...");
> req.Headers.Add("service", "analytics");
> req.Headers.Add("source", "test-test-1.00");
> HttpWebResponse res = (HttpWebResponse)req.GetResponse();
> Any thoughts???
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Marty <mburo... @gmail.com>
Date: Fri, 9 Oct 2009 07:16:01 -0700 (PDT)
Local: Fri, Oct 9 2009 10:16 am
Subject: Re: Client Login to my Google Analytics Account with C#
I was able to login in using the following C# syntax:
string sreq = "https://www.google.com/accounts/ClientLogin?
accountType=HOSTED_OR_GOOGLE&Email=" + username + "&Passwd=" +
password + "&service=analytics&source=test-test-v1";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sreq);
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
The following IDs were returned: SID, LSID, and Auth. I'm not sure
what I'm supposed to do with these IDs.
For starters, I'd like to login into my Google Analytics account and
pull any number.
I've found the following data export protocol, but I'm not sure how my
Auth token fits in with this, and I'm really confused about the ids
that I need. Where exactly do I find those?
string sreq = "https://www.google.com/accounts/analytics/feeds/data?
start-date=2008-10-01&end-
date=2008-10-31&dimensions=ga:source,ga:medium";
sreq = sreq + "&metrics=ga:visits,ga:bounces&sort=-
ga:visits&filters=ga:medium%3D%3Dreferral&max-results=5&ids=ga:xxxxx
&prettyprint=true";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sreq);
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
Thanks,
Marty
On Oct 7, 12:52 am, "Eric (Google)" <api.e... @google.com> wrote:
> Hmm, well you're adding those parameters as headers.
> Rather, you need to add them as form encoded to the body
> of the request:
http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html#R ...
> Eric
> On Oct 7, 3:16 am,Marty<mburo... @gmail.com> wrote:
> > I'm having a hard time authenticating into my Google Analytics account
> > with C#. I get a 403 forbidden error message with the following code:
> > HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.google.com/accounts/ClientLogin ");
> > req.Headers.Add("accountType", "GOOGLE");
> > req.Headers.Add("Email", "my email...");
> > req.Headers.Add("Passwd", "my password...");
> > req.Headers.Add("service", "analytics");
> > req.Headers.Add("source", "test-test-1.00");
> > HttpWebResponse res = (HttpWebResponse)req.GetResponse();
> > Any thoughts???
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Marty <mburo... @gmail.com>
Date: Fri, 9 Oct 2009 07:28:01 -0700 (PDT)
Local: Fri, Oct 9 2009 10:28 am
Subject: Re: Client Login to my Google Analytics Account with C#
Actually, I discovered the data export app which answers a lot of my
questions:
http://code.google.com/apis/analytics/docs/gdata/gdataExplorer.html
However, I get an "unauthorized" error when I tried to use this. Do I
have to pass an auth token into the header or something?
https://www.google.com/analytics/feeds/data?ids=ga%xxxxx&dimensions=g... ";
Thanks,
Marty
On Oct 9, 10:16 am, Marty <mburo... @gmail.com> wrote:
> I was able to login in using the following C# syntax:
> string sreq = "https://www.google.com/accounts/ClientLogin?
> accountType=HOSTED_OR_GOOGLE&Email=" + username + "&Passwd=" +
> password + "&service=analytics&source=test-test-v1";
> HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sreq);
> HttpWebResponse res = (HttpWebResponse)req.GetResponse();
> The following IDs were returned: SID, LSID, and Auth. I'm not sure
> what I'm supposed to do with these IDs.
> For starters, I'd like to login into my Google Analytics account and
> pull any number.
> I've found the following data export protocol, but I'm not sure how my
> Auth token fits in with this, and I'm really confused about the ids
> that I need. Where exactly do I find those?
> string sreq = "https://www.google.com/accounts/analytics/feeds/data?
> start-date=2008-10-01&end-
> date=2008-10-31&dimensions=ga:source,ga:medium";
> sreq = sreq + "&metrics=ga:visits,ga:bounces&sort=-
> ga:visits&filters=ga:medium%3D%3Dreferral&max-results=5&ids=ga:xxxxx
> &prettyprint=true";
> HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sreq);
> HttpWebResponse res = (HttpWebResponse)req.GetResponse();
> Thanks,Marty
> On Oct 7, 12:52 am, "Eric (Google)" <api.e... @google.com> wrote:
> > Hmm, well you're adding those parameters as headers.
> > Rather, you need to add them as form encoded to the body
> > of the request:http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html#R ...
> > Eric
> > On Oct 7, 3:16 am,Marty<mburo... @gmail.com> wrote:
> > > I'm having a hard time authenticating into my Google Analytics account
> > > with C#. I get a 403 forbidden error message with the following code:
> > > HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.google.com/accounts/ClientLogin ");
> > > req.Headers.Add("accountType", "GOOGLE");
> > > req.Headers.Add("Email", "my email...");
> > > req.Headers.Add("Passwd", "my password...");
> > > req.Headers.Add("service", "analytics");
> > > req.Headers.Add("source", "test-test-1.00");
> > > HttpWebResponse res = (HttpWebResponse)req.GetResponse();
> > > Any thoughts???
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Marty <mburo... @gmail.com>
Date: Fri, 9 Oct 2009 15:47:20 -0700 (PDT)
Local: Fri, Oct 9 2009 6:47 pm
Subject: Re: Client Login to my Google Analytics Account with C#
I created a simple C# class that shows how to authenticate using a
simple client login, and retrieve the number of visitors for a year
from Google Analytics.
Hopefully this will help somebody else.
namespace GA
{
public class GoogleAnalytics
{
public string SID { get; set; } // Have no idea what this is
used for.
public string LSID { get; set; } // Have no idea what this is
used for.
public string Auth { get; set; } // Our authorization token
sent in ExecuteQuery().
/// <summary>
/// Simple client login.
/// </summary>
/// <param name="username"></param>
/// <param name="password"></param>
public void Login( string username, string password)
{
string sreq = "https://www.google.com/accounts/ClientLogin?
accountType=HOSTED_OR_GOOGLE&Email=" + username + "&Passwd=" +
password + "&service=analytics&source=test-test-v1";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create
(sreq);
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
Stream responseBody = res.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding
("utf-8");
StreamReader readStream = new StreamReader(responseBody,
encode);
string loginStuff = readStream.ReadToEnd();
SID = loginStuff.Substring(0, loginStuff.IndexOf
("LSID")).Replace( "SID=","").TrimEnd( '\n' );
LSID = loginStuff.Substring(loginStuff.IndexOf("LSID"),
(loginStuff.IndexOf("Auth") - loginStuff.IndexOf("LSID"))).Replace
( "LSID=", "" ).TrimEnd( '\n' );
Auth = loginStuff.Substring(loginStuff.IndexOf
("Auth")).Replace( "Auth=", "" ).TrimEnd('\n');
}
/// <summary>
/// Adds the authorization token created from Login() to our
request and executes our query based on
/// the query created at: http://code.google.com/apis/analytics/docs/gdata/gdataExplorer.html .
///
/// The basic RSS format:
///
/// <rss xmlns:dxp="http://schemas.google.com/analytics/2009 "
xmlns:atom="http://www.w3.org/2005/Atom " xmlns:openSearch="http://
a9.com/-/spec/opensearchrss/1.0/" version="2.0">
/// <channel>
/// <description />
/// <atom:id></atom:id>
/// <lastBuildDate></lastBuildDate>
/// <title></title>
/// <managingEditor></managingEditor>
/// <generator></generator>
/// <openSearch:totalResults>1</openSearch:totalResults>
/// <openSearch:startIndex>1</openSearch:startIndex>
/// <openSearch:itemsPerPage>50</openSearch:itemsPerPage>
/// <dxp:aggregates>
/// <dxp:metric confidenceInterval="0.0" name="ga:visits"
type="integer" value="150" />
/// </dxp:aggregates>
/// <dxp:dataSource>
/// <dxp:property name="ga:profileId" value="15804376" />
/// <dxp:property name="ga:webPropertyId" value="UA-379213-2" /
/// <dxp:property name="ga:accountName" value="mburolla" />
/// <dxp:tableId></dxp:tableId>
/// <dxp:tableName></dxp:tableName>
/// </dxp:dataSource>
/// <dxp:endDate></dxp:endDate>
/// <dxp:startDate></dxp:startDate>
/// <item>
/// <guid isPermaLink="false">
http://www.google.com/
analytics/feeds/data?ids=ga:XXXX&ga:year=2009&start-
date=2009-03-01&end-date=2009-10-09</guid>
/// <atom:updated></atom:updated>
/// <title></title>
/// <link></link>
/// <dxp:dimension name="ga:year" value="2009" />
/// <dxp:metric confidenceInterval="0.0"
name="ga:visits" type="integer" value="150" />
/// </item>
/// </channel>
/// </rss>
/// </summary>
/// <param name="dataExportQuery"></param>
/// <returns></returns>
public string ExecuteQuery(string dataExportQuery)
{
string retval = "";
dataExportQuery = dataExportQuery + "&alt=rss"; // Formats
the content to the RSS spec (couldn't parse the atom files).
HttpWebRequest req = (HttpWebRequest)WebRequest.Create
(dataExportQuery);
req.Headers.Add("Authorization: GoogleLogin auth=" +
Auth);
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
Stream responseBody = res.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding
("utf-8");
StreamReader readStream = new StreamReader(responseBody,
encode);
XmlDocument d = new XmlDocument();
d.LoadXml(readStream.ReadToEnd());
XmlNodeList nl = d.DocumentElement.SelectNodes("/rss/
channel/item");
foreach (XmlNode n in nl)
{
foreach (XmlNode nn in n.ChildNodes)
{
if ( nn.Name == "dxp:dimension" )
retval="Year: " + nn.Attributes
[ "value" ].Value;
if ( nn.Name == "dxp:metric" )
{
retval+= ( ", Visits: " + nn.Attributes
[ "value" ].Value );
break;
}
}
}
return retval;
}
}
}
On Oct 9, 10:28 am, Marty <mburo
... @gmail.com> wrote:
> Actually, I discovered the data export app which answers a lot of my
> questions:
> http://code.google.com/apis/analytics/docs/gdata/gdataExplorer.html
> However, I get an "unauthorized" error when I tried to use this. Do I
> have to pass an auth token into the header or something?
> https://www.google.com/analytics/feeds/data?ids=ga%xxxxx&dimensions=g ...";
> Thanks,Marty
> On Oct 9, 10:16 am,Marty<mburo... @gmail.com> wrote:
> > I was able to login in using the following C# syntax:
> > string sreq = "https://www.google.com/accounts/ClientLogin?
> > accountType=HOSTED_OR_GOOGLE&Email=" + username + "&Passwd=" +
> > password + "&service=analytics&source=test-test-v1";
> > HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sreq);
> > HttpWebResponse res = (HttpWebResponse)req.GetResponse();
> > The following IDs were returned: SID, LSID, and Auth. I'm not sure
> > what I'm supposed to do with these IDs.
> > For starters, I'd like to login into my Google Analytics account and
> > pull any number.
> > I've found the following data export protocol, but I'm not sure how my
> > Auth token fits in with this, and I'm really confused about the ids
> > that I need. Where exactly do I find those?
> > string sreq = "https://www.google.com/accounts/analytics/feeds/data?
> > start-date=2008-10-01&end-
> > date=2008-10-31&dimensions=ga:source,ga:medium";
> > sreq = sreq + "&metrics=ga:visits,ga:bounces&sort=-
> > ga:visits&filters=ga:medium%3D%3Dreferral&max-results=5&ids=ga:xxxxx
> > &prettyprint=true";
> > HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sreq);
> > HttpWebResponse res = (HttpWebResponse)req.GetResponse();
> > Thanks,Marty
> > On Oct 7, 12:52 am, "Eric (Google)" <api.e... @google.com> wrote:
> > > Hmm, well you're adding those parameters as headers.
> > > Rather, you need to add them as form encoded to the body
> > > of the request:http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html#R ...
> > > Eric
> > > On Oct 7, 3:16 am,Marty<mburo... @gmail.com> wrote:
> > > > I'm having a hard time authenticating into my Google Analytics account
> > > > with C#. I get a 403 forbidden error message with the following code:
> > > > HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.google.com/accounts/ClientLogin ");
> > > > req.Headers.Add("accountType", "GOOGLE");
> > > > req.Headers.Add("Email", "my email...");
> > > > req.Headers.Add("Passwd", "my password...");
> > > > req.Headers.Add("service", "analytics");
> > > > req.Headers.Add("source", "test-test-1.00");
> > > > HttpWebResponse res = (HttpWebResponse)req.GetResponse();
> > > > Any thoughts???
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
"Eric (Google)" <api.e... @google.com>
Date: Fri, 9 Oct 2009 16:07:16 -0700 (PDT)
Local: Fri, Oct 9 2009 7:07 pm
Subject: Re: Client Login to my Google Analytics Account with C#
Thanks for posting the soln. Marty. You should
also post this in the Analytics API forum. It'll
be most relevant there.
Eric
On Oct 10, 9:47 am, Marty <mburo... @gmail.com> wrote:
> I created a simple C# class that shows how to authenticate using a
> simple client login, and retrieve the number of visitors for a year
> from Google Analytics.
> Hopefully this will help somebody else.
> namespace GA
> {
> public class GoogleAnalytics
> {
> public string SID { get; set; } // Have no idea what this is
> used for.
> public string LSID { get; set; } // Have no idea what this is
> used for.
> public string Auth { get; set; } // Our authorization token
> sent in ExecuteQuery().
> /// <summary>
> /// Simple client login.
> /// </summary>
> /// <param name="username"></param>
> /// <param name="password"></param>
> public void Login( string username, string password)
> {
> string sreq = "https://www.google.com/accounts/ClientLogin?
> accountType=HOSTED_OR_GOOGLE&Email=" + username + "&Passwd=" +
> password + "&service=analytics&source=test-test-v1";
> HttpWebRequest req = (HttpWebRequest)WebRequest.Create
> (sreq);
> HttpWebResponse res = (HttpWebResponse)req.GetResponse();
> Stream responseBody = res.GetResponseStream();
> Encoding encode = System.Text.Encoding.GetEncoding
> ("utf-8");
> StreamReader readStream = new StreamReader(responseBody,
> encode);
> string loginStuff = readStream.ReadToEnd();
> SID = loginStuff.Substring(0, loginStuff.IndexOf
> ("LSID")).Replace( "SID=","").TrimEnd( '\n' );
> LSID = loginStuff.Substring(loginStuff.IndexOf("LSID"),
> (loginStuff.IndexOf("Auth") - loginStuff.IndexOf("LSID"))).Replace
> ( "LSID=", "" ).TrimEnd( '\n' );
> Auth = loginStuff.Substring(loginStuff.IndexOf
> ("Auth")).Replace( "Auth=", "" ).TrimEnd('\n');
> }
> /// <summary>
> /// Adds the authorization token created from Login() to our
> request and executes our query based on
> /// the query created at:http://code.google.com/apis/analytics/docs/gdata/gdataExplorer.html .
> ///
> /// The basic RSS format:
> ///
> /// <rss xmlns:dxp="http://schemas.google.com/analytics/2009 "
> xmlns:atom="http://www.w3.org/2005/Atom " xmlns:openSearch="http://
> a9.com/-/spec/opensearchrss/1.0/" version="2.0">
> /// <channel>
> /// <description />
> /// <atom:id></atom:id>
> /// <lastBuildDate></lastBuildDate>
> /// <title></title>
> /// <managingEditor></managingEditor>
> /// <generator></generator>
> /// <openSearch:totalResults>1</openSearch:totalResults>
> /// <openSearch:startIndex>1</openSearch:startIndex>
> /// <openSearch:itemsPerPage>50</openSearch:itemsPerPage>
> /// <dxp:aggregates>
> /// <dxp:metric confidenceInterval="0.0" name="ga:visits"
> type="integer" value="150" />
> /// </dxp:aggregates>
> /// <dxp:dataSource>
> /// <dxp:property name="ga:profileId" value="15804376" />
> /// <dxp:property name="ga:webPropertyId" value="UA-379213-2" /
> /// <dxp:property name="ga:accountName" value="mburolla" />
> /// <dxp:tableId></dxp:tableId>
> /// <dxp:tableName></dxp:tableName>
> /// </dxp:dataSource>
> /// <dxp:endDate></dxp:endDate>
> /// <dxp:startDate></dxp:startDate>
> /// <item>
> /// <guid isPermaLink="false">http://www.google.com/
> analytics/feeds/data?ids=ga:XXXX&ga:year=2009&start-
> date=2009-03-01&end-date=2009-10-09</guid>
> /// <atom:updated></atom:updated>
> /// <title></title>
> /// <link></link>
> /// <dxp:dimension name="ga:year" value="2009" />
> /// <dxp:metric confidenceInterval="0.0"
> name="ga:visits" type="integer" value="150" />
> /// </item>
> /// </channel>
> /// </rss>
> /// </summary>
> /// <param name="dataExportQuery"></param>
> /// <returns></returns>
> public string ExecuteQuery(string dataExportQuery)
> {
> string retval = "";
> dataExportQuery = dataExportQuery + "&alt=rss"; // Formats
> the content to the RSS spec (couldn't parse the atom files).
> HttpWebRequest req = (HttpWebRequest)WebRequest.Create
> (dataExportQuery);
> req.Headers.Add("Authorization: GoogleLogin auth=" +
> Auth);
> HttpWebResponse res = (HttpWebResponse)req.GetResponse();
> Stream responseBody = res.GetResponseStream();
> Encoding encode = System.Text.Encoding.GetEncoding
> ("utf-8");
> StreamReader readStream = new StreamReader(responseBody,
> encode);
> XmlDocument d = new XmlDocument();
> d.LoadXml(readStream.ReadToEnd());
> XmlNodeList nl = d.DocumentElement.SelectNodes("/rss/
> channel/item");
> foreach (XmlNode n in nl)
> {
> foreach (XmlNode nn in n.ChildNodes)
> {
> if ( nn.Name == "dxp:dimension" )
> retval="Year: " + nn.Attributes
> [ "value" ].Value;
> if ( nn.Name == "dxp:metric" )
> {
> retval+= ( ", Visits: " + nn.Attributes
> [ "value" ].Value );
> break;
> }
> }
> }
> return retval;
> }
> }
> }
> On Oct 9, 10:28 am, Marty <mburo... @gmail.com> wrote:
> > Actually, I discovered the data export app which answers a lot of my
> > questions:
> >http://code.google.com/apis/analytics/docs/gdata/gdataExplorer.html
> > However, I get an "unauthorized" error when I tried to use this. Do I
> > have to pass an auth token into the header or something?
> >https://www.google.com/analytics/feeds/data?ids=ga%xxxxx&dimensions=g ...";
> > Thanks,Marty
> > On Oct 9, 10:16 am,Marty<mburo... @gmail.com> wrote:
> > > I was able to login in using the following C# syntax:
> > > string sreq = "https://www.google.com/accounts/ClientLogin?
> > > accountType=HOSTED_OR_GOOGLE&Email=" + username + "&Passwd=" +
> > > password + "&service=analytics&source=test-test-v1";
> > > HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sreq);
> > > HttpWebResponse res = (HttpWebResponse)req.GetResponse();
> > > The following IDs were returned: SID, LSID, and Auth. I'm not sure
> > > what I'm supposed to do with these IDs.
> > > For starters, I'd like to login into my Google Analytics account and
> > > pull any number.
> > > I've found the following data export protocol, but I'm not sure how my
> > > Auth token fits in with this, and I'm really confused about the ids
> > > that I need. Where exactly do I find those?
> > > string sreq = "https://www.google.com/accounts/analytics/feeds/data?
> > > start-date=2008-10-01&end-
> > > date=2008-10-31&dimensions=ga:source,ga:medium";
> > > sreq = sreq + "&metrics=ga:visits,ga:bounces&sort=-
> > > ga:visits&filters=ga:medium%3D%3Dreferral&max-results=5&ids=ga:xxxxx
> > > &prettyprint=true";
> > > HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sreq);
> > > HttpWebResponse res = (HttpWebResponse)req.GetResponse();
> > > Thanks,Marty
> > > On Oct 7, 12:52 am, "Eric (Google)" <api.e... @google.com> wrote:
> > > > Hmm, well you're adding those parameters as headers.
> > > > Rather, you need to add them as form encoded to the body
> > > > of the request:http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html#R ...
> > > > Eric
> > > > On Oct 7, 3:16 am,Marty<mburo... @gmail.com> wrote:
> > > > > I'm having a hard time authenticating into my Google Analytics account
> > > > > with C#. I get a 403 forbidden error message with the following code:
> > > > > HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.google.com/accounts/ClientLogin ");
> > > > > req.Headers.Add("accountType", "GOOGLE");
> > > > > req.Headers.Add("Email", "my email...");
> > > > > req.Headers.Add("Passwd", "my password...");
> > > > > req.Headers.Add("service", "analytics");
> > > > > req.Headers.Add("source", "test-test-1.00");
> > > > > HttpWebResponse res = (HttpWebResponse)req.GetResponse();
> > > > > Any thoughts???
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Nick <api.ni... @google.com>
Date: Fri, 9 Oct 2009 16:19:13 -0700 (PDT)
Local: Fri, Oct 9 2009 7:19 pm
Subject: Re: Client Login to my Google Analytics Account with C#
Awesome Marty.
In case you didn't see it, Frank Mantek has an Analytics client
library avaliable in the Google Data .NET library here:
http://code.google.com/p/google-gdata/source/browse/#svn/trunk/client...
This might be helpful if you plan on supporting other auth mechanisms.
-Nick
On Oct 9, 3:47 pm, Marty <mburo... @gmail.com> wrote:
> I created a simple C# class that shows how to authenticate using a
> simple client login, and retrieve the number of visitors for a year
> from Google Analytics.
> Hopefully this will help somebody else.
> namespace GA
> {
> public class GoogleAnalytics
> {
> public string SID { get; set; } // Have no idea what this is
> used for.
> public string LSID { get; set; } // Have no idea what this is
> used for.
> public string Auth { get; set; } // Our authorization token
> sent in ExecuteQuery().
> /// <summary>
> /// Simple client login.
> /// </summary>
> /// <param name="username"></param>
> /// <param name="password"></param>
> public void Login( string username, string password)
> {
> string sreq = "https://www.google.com/accounts/ClientLogin?
> accountType=HOSTED_OR_GOOGLE&Email=" + username + "&Passwd=" +
> password + "&service=analytics&source=test-test-v1";
> HttpWebRequest req = (HttpWebRequest)WebRequest.Create
> (sreq);
> HttpWebResponse res = (HttpWebResponse)req.GetResponse();
> Stream responseBody = res.GetResponseStream();
> Encoding encode = System.Text.Encoding.GetEncoding
> ("utf-8");
> StreamReader readStream = new StreamReader(responseBody,
> encode);
> string loginStuff = readStream.ReadToEnd();
> SID = loginStuff.Substring(0, loginStuff.IndexOf
> ("LSID")).Replace( "SID=","").TrimEnd( '\n' );
> LSID = loginStuff.Substring(loginStuff.IndexOf("LSID"),
> (loginStuff.IndexOf("Auth") - loginStuff.IndexOf("LSID"))).Replace
> ( "LSID=", "" ).TrimEnd( '\n' );
> Auth = loginStuff.Substring(loginStuff.IndexOf
> ("Auth")).Replace( "Auth=", "" ).TrimEnd('\n');
> }
> /// <summary>
> /// Adds the authorization token created from Login() to our
> request and executes our query based on
> /// the query created at:http://code.google.com/apis/analytics/docs/gdata/gdataExplorer.html .
> ///
> /// The basic RSS format:
> ///
> /// <rss xmlns:dxp="http://schemas.google.com/analytics/2009 "
> xmlns:atom="http://www.w3.org/2005/Atom " xmlns:openSearch="http://
> a9.com/-/spec/opensearchrss/1.0/" version="2.0">
> /// <channel>
> /// <description />
> /// <atom:id></atom:id>
> /// <lastBuildDate></lastBuildDate>
> /// <title></title>
> /// <managingEditor></managingEditor>
> /// <generator></generator>
> /// <openSearch:totalResults>1</openSearch:totalResults>
> /// <openSearch:startIndex>1</openSearch:startIndex>
> /// <openSearch:itemsPerPage>50</openSearch:itemsPerPage>
> /// <dxp:aggregates>
> /// <dxp:metric confidenceInterval="0.0" name="ga:visits"
> type="integer" value="150" />
> /// </dxp:aggregates>
> /// <dxp:dataSource>
> /// <dxp:property name="ga:profileId" value="15804376" />
> /// <dxp:property name="ga:webPropertyId" value="UA-379213-2" /
> /// <dxp:property name="ga:accountName" value="mburolla" />
> /// <dxp:tableId></dxp:tableId>
> /// <dxp:tableName></dxp:tableName>
> /// </dxp:dataSource>
> /// <dxp:endDate></dxp:endDate>
> /// <dxp:startDate></dxp:startDate>
> /// <item>
> /// <guid isPermaLink="false">http://www.google.com/
> analytics/feeds/data?ids=ga:XXXX&ga:year=2009&start-
> date=2009-03-01&end-date=2009-10-09</guid>
> /// <atom:updated></atom:updated>
> /// <title></title>
> /// <link></link>
> /// <dxp:dimension name="ga:year" value="2009" />
> /// <dxp:metric confidenceInterval="0.0"
> name="ga:visits" type="integer" value="150" />
> /// </item>
> /// </channel>
> /// </rss>
> /// </summary>
> /// <param name="dataExportQuery"></param>
> /// <returns></returns>
> public string ExecuteQuery(string dataExportQuery)
> {
> string retval = "";
> dataExportQuery = dataExportQuery + "&alt=rss"; // Formats
> the content to the RSS spec (couldn't parse the atom files).
> HttpWebRequest req = (HttpWebRequest)WebRequest.Create
> (dataExportQuery);
> req.Headers.Add("Authorization: GoogleLogin auth=" +
> Auth);
> HttpWebResponse res = (HttpWebResponse)req.GetResponse();
> Stream responseBody = res.GetResponseStream();
> Encoding encode = System.Text.Encoding.GetEncoding
> ("utf-8");
> StreamReader readStream = new StreamReader(responseBody,
> encode);
> XmlDocument d = new XmlDocument();
> d.LoadXml(readStream.ReadToEnd());
> XmlNodeList nl = d.DocumentElement.SelectNodes("/rss/
> channel/item");
> foreach (XmlNode n in nl)
> {
> foreach (XmlNode nn in n.ChildNodes)
> {
> if ( nn.Name == "dxp:dimension" )
> retval="Year: " + nn.Attributes
> [ "value" ].Value;
> if ( nn.Name == "dxp:metric" )
> {
> retval+= ( ", Visits: " + nn.Attributes
> [ "value" ].Value );
> break;
> }
> }
> }
> return retval;
> }
> }
> }
> On Oct 9, 10:28 am, Marty <mburo... @gmail.com> wrote:
> > Actually, I discovered the data export app which answers a lot of my
> > questions:
> >http://code.google.com/apis/analytics/docs/gdata/gdataExplorer.html
> > However, I get an "unauthorized" error when I tried to use this. Do I
> > have to pass an auth token into the header or something?
> >https://www.google.com/analytics/feeds/data?ids=ga%xxxxx&dimensions=g ...";
> > Thanks,Marty
> > On Oct 9, 10:16 am,Marty<mburo... @gmail.com> wrote:
> > > I was able to login in using the following C# syntax:
> > > string sreq = "https://www.google.com/accounts/ClientLogin?
> > > accountType=HOSTED_OR_GOOGLE&Email=" + username + "&Passwd=" +
> > > password + "&service=analytics&source=test-test-v1";
> > > HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sreq);
> > > HttpWebResponse res = (HttpWebResponse)req.GetResponse();
> > > The following IDs were returned: SID, LSID, and Auth. I'm not sure
> > > what I'm supposed to do with these IDs.
> > > For starters, I'd like to login into my Google Analytics account and
> > > pull any number.
> > > I've found the following data export protocol, but I'm not sure how my
> > > Auth token fits in with this, and I'm really confused about the ids
> > > that I need. Where exactly do I find those?
> > > string sreq = "https://www.google.com/accounts/analytics/feeds/data?
> > > start-date=2008-10-01&end-
> > > date=2008-10-31&dimensions=ga:source,ga:medium";
> > > sreq = sreq + "&metrics=ga:visits,ga:bounces&sort=-
> > > ga:visits&filters=ga:medium%3D%3Dreferral&max-results=5&ids=ga:xxxxx
> > > &prettyprint=true";
> > > HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sreq);
> > > HttpWebResponse res = (HttpWebResponse)req.GetResponse();
> > > Thanks,Marty
> > > On Oct 7, 12:52 am, "Eric (Google)" <api.e... @google.com> wrote:
> > > > Hmm, well you're adding those parameters as headers.
> > > > Rather, you need to add them as form encoded to the body
> > > > of the request:http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html#R ...
> > > > Eric
> > > > On Oct 7, 3:16 am,Marty<mburo... @gmail.com> wrote:
> > > > > I'm having a hard time authenticating into my Google Analytics account
> > > > > with C#. I get a 403 forbidden error message with the following code:
> > > > > HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.google.com/accounts/ClientLogin ");
> > > > > req.Headers.Add("accountType", "GOOGLE");
> > > > > req.Headers.Add("Email", "my email...");
> > > > > req.Headers.Add("Passwd", "my password...");
> > > > > req.Headers.Add("service", "analytics");
> > > > > req.Headers.Add("source", "test-test-1.00");
> > > > > HttpWebResponse res = (HttpWebResponse)req.GetResponse();
> > > > > Any thoughts???
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
cas <anasousa.as... @gmail.com>
Date: Thu, 3 Dec 2009 09:33:23 -0800 (PST)
Local: Thurs, Dec 3 2009 12:33 pm
Subject: Re: Client Login to my Google Analytics Account with C#
Hi,
I create a query at: http://code.google.com/apis/analytics/docs/gdata/gdataExplorer.html
the query is the following (this is the first query on "Popular
Queries" categories)
strquery = "
https://www.google.com/analytics/feeds/data?dimensions=ga%3Adate&metr...
" ;
// then I called this 2 methodes:
login ("myuser","mypass"); //ok - the sid, lsid and auth have values
string res = ExecuteQuery (strquery);
but it occur one error in this line of your code:
HttpWebResponse res = (HttpWebResponse)req.GetResponse(); // error
message is: « (400) Bad Request»
Is there anything that i made wrong? Can you help please on this.
thanks in advance,
cas
On 9 Out, 22:47, Marty <mburo... @gmail.com> wrote:
> I created a simple C# class that shows how to authenticate using a
> simple client login, and retrieve the number of visitors for a year
> from Google Analytics.
> Hopefully this will help somebody else.
> namespace GA
> {
> public class GoogleAnalytics
> {
> public string SID { get; set; } // Have no idea what this is
> used for.
> public string LSID { get; set; } // Have no idea what this is
> used for.
> public string Auth { get; set; } // Our authorization token
> sent in ExecuteQuery().
> /// <summary>
> /// Simple client login.
> /// </summary>
> /// <param name="username"></param>
> /// <param name="password"></param>
> public void Login( string username, string password)
> {
> string sreq = "https://www.google.com/accounts/ClientLogin?
> accountType=HOSTED_OR_GOOGLE&Email=" + username + "&Passwd=" +
> password + "&service=analytics&source=test-test-v1";
> HttpWebRequest req = (HttpWebRequest)WebRequest.Create
> (sreq);
> HttpWebResponse res = (HttpWebResponse)req.GetResponse();
> Stream responseBody = res.GetResponseStream();
> Encoding encode = System.Text.Encoding.GetEncoding
> ("utf-8");
> StreamReader readStream = new StreamReader(responseBody,
> encode);
> string loginStuff = readStream.ReadToEnd();
> SID = loginStuff.Substring(0, loginStuff.IndexOf
> ("LSID")).Replace( "SID=","").TrimEnd( '\n' );
> LSID = loginStuff.Substring(loginStuff.IndexOf("LSID"),
> (loginStuff.IndexOf("Auth") - loginStuff.IndexOf("LSID"))).Replace
> ( "LSID=", "" ).TrimEnd( '\n' );
> Auth = loginStuff.Substring(loginStuff.IndexOf
> ("Auth")).Replace( "Auth=", "" ).TrimEnd('\n');
> }
> /// <summary>
> /// Adds the authorization token created from Login() to our
> request and executes our query based on
> /// the query created at:http://code.google.com/apis/analytics/docs/gdata/gdataExplorer.html .
> ///
> /// The basic RSS format:
> ///
> /// <rss xmlns:dxp="http://schemas.google.com/analytics/2009 "
> xmlns:atom="http://www.w3.org/2005/Atom " xmlns:openSearch="http://
> a9.com/-/spec/opensearchrss/1.0/" version="2.0">
> /// <channel>
> /// <description />
> /// <atom:id></atom:id>
> /// <lastBuildDate></lastBuildDate>
> /// <title></title>
> /// <managingEditor></managingEditor>
> /// <generator></generator>
> /// <openSearch:totalResults>1</openSearch:totalResults>
> /// <openSearch:startIndex>1</openSearch:startIndex>
> /// <openSearch:itemsPerPage>50</openSearch:itemsPerPage>
> /// <dxp:aggregates>
> /// <dxp:metric confidenceInterval="0.0" name="ga:visits"
> type="integer" value="150" />
> /// </dxp:aggregates>
> /// <dxp:dataSource>
> /// <dxp:property name="ga:profileId" value="15804376" />
> /// <dxp:property name="ga:webPropertyId" value="UA-379213-2" /
> /// <dxp:property name="ga:accountName" value="mburolla" />
> /// <dxp:tableId></dxp:tableId>
> /// <dxp:tableName></dxp:tableName>
> /// </dxp:dataSource>
> /// <dxp:endDate></dxp:endDate>
> /// <dxp:startDate></dxp:startDate>
> /// <item>
> /// <guid isPermaLink="false">http://www.google.com/
> analytics/feeds/data?ids=ga:XXXX&ga:year=2009&start-
> date=2009-03-01&end-date=2009-10-09</guid>
> /// <atom:updated></atom:updated>
> /// <title></title>
> /// <link></link>
> /// <dxp:dimension name="ga:year" value="2009" />
> /// <dxp:metric confidenceInterval="0.0"
> name="ga:visits" type="integer" value="150" />
> /// </item>
> /// </channel>
> /// </rss>
> /// </summary>
> /// <param name="dataExportQuery"></param>
> /// <returns></returns>
> public string ExecuteQuery(string dataExportQuery)
> {
> string retval = "";
> dataExportQuery = dataExportQuery + "&alt=rss"; // Formats
> the content to the RSS spec (couldn't parse the atom files).
> HttpWebRequest req = (HttpWebRequest)WebRequest.Create
> (dataExportQuery);
> req.Headers.Add("Authorization: GoogleLogin auth=" +
> Auth);
> HttpWebResponse res = (HttpWebResponse)req.GetResponse();
> Stream responseBody = res.GetResponseStream();
> Encoding encode = System.Text.Encoding.GetEncoding
> ("utf-8");
> StreamReader readStream = new StreamReader(responseBody,
> encode);
> XmlDocument d = new XmlDocument();
> d.LoadXml(readStream.ReadToEnd());
> XmlNodeList nl = d.DocumentElement.SelectNodes("/rss/
> channel/item");
> foreach (XmlNode n in nl)
> {
> foreach (XmlNode nn in n.ChildNodes)
> {
> if ( nn.Name == "dxp:dimension" )
> retval="Year: " + nn.Attributes
> [ "value" ].Value;
> if ( nn.Name == "dxp:metric" )
> {
> retval+= ( ", Visits: " + nn.Attributes
> [ "value" ].Value );
> break;
> }
> }
> }
> return retval;
> }
> }
> }
> On Oct 9, 10:28 am, Marty <mburo... @gmail.com> wrote:
> > Actually, I discovered the data export app which answers a lot of my
> > questions:
> >http://code.google.com/apis/analytics/docs/gdata/gdataExplorer.html
> > However, I get an "unauthorized" error when I tried to use this. Do I
> > have to pass an auth token into the header or something?
> >https://www.google.com/analytics/feeds/data?ids=ga%xxxxx&dimensions=g ...";
> > Thanks,Marty
> > On Oct 9, 10:16 am,Marty<mburo... @gmail.com> wrote:
> > > I was able to login in using the following C# syntax:
> > > string sreq = "https://www.google.com/accounts/ClientLogin?
> > > accountType=HOSTED_OR_GOOGLE&Email=" + username + "&Passwd=" +
> > > password + "&service=analytics&source=test-test-v1";
> > > HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sreq);
> > > HttpWebResponse res = (HttpWebResponse)req.GetResponse();
> > > The following IDs were returned: SID, LSID, and Auth. I'm not sure
> > > what I'm supposed to do with these IDs.
> > > For starters, I'd like to login into my Google Analytics account and
> > > pull any number.
> > > I've found the following data export protocol, but I'm not sure how my
> > > Auth token fits in with this, and I'm really confused about the ids
> > > that I need. Where exactly do I find those?
> > > string sreq = "https://www.google.com/accounts/analytics/feeds/data?
> > > start-date=2008-10-01&end-
> > > date=2008-10-31&dimensions=ga:source,ga:medium";
> > > sreq = sreq + "&metrics=ga:visits,ga:bounces&sort=-
> > > ga:visits&filters=ga:medium%3D%3Dreferral&max-results=5&ids=ga:xxxxx
> > > &prettyprint=true";
> > > HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sreq);
> > > HttpWebResponse res = (HttpWebResponse)req.GetResponse();
> > > Thanks,Marty
> > > On Oct 7, 12:52 am, "Eric (Google)" <api.e... @google.com> wrote:
> > > > Hmm, well you're adding those parameters as headers.
> > > > Rather, you need to add them as form encoded to the body
> > > > of the request:http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html#R ...
> > > > Eric
> > > > On Oct 7, 3:16 am,Marty<mburo... @gmail.com> wrote:
> > > > > I'm having a hard time authenticating into my Google Analytics account
> > > > > with C#. I get a 403 forbidden error message with the following code:
> > > > > HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.google.com/accounts/ClientLogin ");
> > > > > req.Headers.Add("accountType", "GOOGLE");
> > > > > req.Headers.Add("Email", "my email...");
> > > > > req.Headers.Add("Passwd", "my password...");
> > > > > req.Headers.Add("service", "analytics");
> > > > > req.Headers.Add("source", "test-test-1.00");
> > > > > HttpWebResponse res = (HttpWebResponse)req.GetResponse();
> > > > > Any thoughts???
You must
Sign in before you can post messages.
You do not have the permission required to post.