Hi Anirudh,
Thanks for replying.I appreciate it.
I am using Poster add-ons of firefox for sending XML HTTP
Request.Intresting thing is that i recieve SID token but when i put
this SID token in actual XML Activity report in <token></token> tag it
do not works.It just give html codes for Google page.Again i assure
that i am putting valid
<domain>example.com</domain>
<date>YYYY-MM-DD</date>
everythig is fine.
what you think do i manipulate
https://www.google.com/accounts/ClientLogin
https://www.google.com/hosted_or_google/services/v1.0/reports/ReportingData
address,by
https://www.google.com/a/example.com/services/v1.0/reports/ReportingData
As far as C# file is concerned
first i write something like below,
C:\Windows\Microsoft.NET\Framework\VERSION\csc.exe
then i write
C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe
ReportingAPIClient.cs
it is not asking to take parameters like email,password,domain,token
etc.i am pretty sure that i do not have to manipulate given
ReportingAPIClient.cs file.In your case if you can run the C# file
then can you forward me that particular c# ReportingAPIClient.cs
file???
Here is the file which i use.
===========
/* Copyright (c) 2007 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Collections;
using System.Web;
namespace Google.Apps.Reporting
{
/// <summary>
/// This contains the logic for constructing and submitting a
report
/// request to the Google Apps reporting service and reading the
response.
///
/// The description of the web service protocol can be found at:
///
///
http://code.google.com/apis/apps/reporting/google_apps_reporting_api.html
///
/// Example usage:
/// ReportingAPIClient client = new ReportingAPIClient();
/// client.email = "[EMAIL PROTECTED]";
/// client.password = "passwd";
/// client.domain = "example.com";
/// client.ClientLogin();
/// Get the latest accounts report to standard output.
/// client.getReport("accounts", null, null);
/// Get the accounts report for May 15, 2007 and save it to
out.txt.
/// client.getReport("accounts", "2007-05-15", "out.txt");
/// </summary>
public class ReportingAPIClient
{
/// <summary>
/// URL to POST to obtain an authentication token
/// </summary>
private const string CLIENT_LOGIN_URL =
"https://www.google.com/accounts/ClientLogin";
/// <summary>
/// URL to POST to retrive resports from the API
/// </summary>
private const string REPORTING_URL =
"https://www.google.com/hosted/services/v1.0/reports/
ReportingData";
/// <summary>
/// Date format of the API
/// </summary>
private const string DATE_FORMAT = "yyyy-MM-dd";
/// <summary>
/// Hour of the day when the API data gets published
/// </summary>
private const int PUBLISH_HOUR_OF_DAY = 13; // Publish hour +
1 hour;
/// <summary>
/// Time diference to UTC
/// </summary>
private const int PUBLISH_TIME_DIFERENCE_TO_UTC = -8;
/// <summary>
/// Email command-line argument
/// </summary>
private const string EMAIL_ARG = "email";
/// <summary>
/// Password command-line argument
/// </summary>
private const string PASSWORD_ARG = "password";
/// <summary>
/// Domain command-line argument
/// </summary>
private const string DOMAIN_ARG = "domain";
/// <summary>
/// Report command-line argument
/// </summary>
private const string REPORT_ARG = "report";
/// <summary>
/// Date command-line argument
/// </summary>
private const string DATE_ARG = "date";
/// <summary>
/// Output File command-line argument
/// </summary>
private const string OUT_FILE_ARG = "out";
/// <summary>
/// Message for command-line usage
/// </summary>
private const string USAGE = "Usage: " +
"ReportingAPI --" + EMAIL_ARG + " <email> --" +
PASSWORD_ARG + " <password> [ --" +
DOMAIN_ARG + " <domain> ] --" +
REPORT_ARG + " <report name> [ --" +
DATE_ARG + " <YYYY-MM-DD> ] [ --" +
OUT_FILE_ARG + " <file name> ]";
/// <summary>
/// List of command-line arguments
/// </summary>
private static string[] PROPERTY_NAMES = new String[]
{EMAIL_ARG,
PASSWORD_ARG, DOMAIN_ARG, REPORT_ARG, DATE_ARG,
OUT_FILE_ARG};
/// <summary>
/// List of required command-line arguments
/// </summary>
private static string[] REQUIRED_PROPERTY_NAMES = new String[]
{
EMAIL_ARG, PASSWORD_ARG, REPORT_ARG};
/// <summary>
/// Google Apps Domain
/// </summary>
public string domain = null;
/// <summary>
/// Email address of an Administrator account
/// </summary>
public string email = null;
/// <summary>
/// Password of the Administrator account
/// </summary>
public string password = null;
/// <summary>
/// Identifies the type of account
/// </summary>
private string accountType = "HOSTED";
/// <summary>
/// Identifies the Google service
/// </summary>
private string service = "apps";
/// <summary>
/// Contains a token value that Google uses to authorize
/// access to the requested report data
/// </summary>
private string token = null;
/// <summary>
/// Default constructor
/// </summary>
public ReportingAPIClient()
{
}
/// <summary>
/// Retrieves the Authentication Token
/// </summary>
/// <returns>Returns the authentication token.</returns>
public string GetToken()
{
return this.token;
}
/// <summary>
/// Logs in the user and initializes the Token
/// </summary>
public void ClientLogin()
{
string token = null;
UTF8Encoding encoding = new UTF8Encoding();
string postData = "Email=" +
HttpUtility.UrlEncode(this.email) +
"&Passwd=" + HttpUtility.UrlEncode(this.password) +
"&accountType=" +
HttpUtility.UrlEncode(this.accountType) +
"&service=" + HttpUtility.UrlEncode(this.service);
byte[] data = encoding.GetBytes(postData);
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(CLIENT_LOGIN_URL);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
Stream inputStream = request.GetRequestStream();
inputStream.Write(data, 0, data.Length);
inputStream.Close();
HttpWebResponse response =
(HttpWebResponse)request.GetResponse();
string responseStr = (new StreamReader(
response.GetResponseStream())).ReadToEnd();
char[] tokenizer = { '\r', '\n' };
string[] parts = responseStr.Split(tokenizer);
foreach (string part in parts)
{
if (part.StartsWith("SID="))
{
token = part.Substring(4);
break;
}
}
this.token = token;
}
/// <summary>
/// Creates a XML request for the Report
/// </summary>
/// <param name="reportName">The name of the Report:
activity,
/// disk_space, email_clients, quota_limit_accounts,
/// summary, suspended_account</param>
/// <param name="date">Date of the Report</param>
/// <returns>Thx XML request as a string</returns>
public string createRequestXML(string reportName, string date)
{
if (this.domain == null)
{
this.domain = getAdminEmailDomain();
}
string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
xml += "<rest xmlns=\"google:accounts:rest:protocol\"";
xml += " xmlns:xsi=\"";
xml += "http://www.w3.org/2001/XMLSchema-instance\">";
xml += "<type>Report</type>";
xml += "<token>" + this.GetToken() + "</token>";
xml += "<domain>" + this.domain + "</domain>";
xml += "<date>" + date + "</date>";
xml += "<reportType>daily</reportType>";
xml += "<reportName>" + reportName + "</reportName>";
xml += "</rest>";
return xml;
}
/// <summary>
/// Get the domain of the admin's email address.
/// </summary>
/// <returns>the domain, otherwise returns null</returns>
public string getAdminEmailDomain()
{
if (this.email != null)
{
int atIndex = this.email.IndexOf('@');
if (atIndex > 0 && atIndex + 1 < this.email.Length)
{
return this.email.Substring(atIndex + 1);
}
}
else
{
throw new ArgumentNullException("Invalid Email");
}
return null;
}
/// <summary>
/// Get the reports by reportName for a Date, and writes the
report
/// at filename /// or to the console if filename is
null.
/// </summary>
/// <param name="reportName">The name of the Report:
activity,
/// disk_space, email_clients, quota_limit_accounts,summary,
/// suspended_account</param>
/// <param name="date">Date of the Report,
/// null date gets latest date available</param>
/// <param name="fileName">file name to write the
/// report or null for console</param>
public void getReport(string reportName, string date, string
fileName)
{
if (date == null)
{
date = getLatestReportDate().ToString(DATE_FORMAT);
}
else
{
try
{
date = System.Convert.ToDateTime(date).ToString
(DATE_FORMAT);
}
catch
{
throw new ArgumentException("Invalid Date");
}
}
string xml = createRequestXML(reportName, date);
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(REPORTING_URL);
request.Method = "POST";
UTF8Encoding encoding = new UTF8Encoding();
byte[] postBuffer = encoding.GetBytes(xml);
request.ContentLength = postBuffer.Length;
request.ContentType = "application/x-www-form-urlencoded";
Stream requestStream = request.GetRequestStream();
requestStream.Write(postBuffer, 0, postBuffer.Length);
requestStream.Close();
HttpWebResponse response =
(HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(
response.GetResponseStream());
string firstLine = null;
if (reader.Peek() >= 0)
{
firstLine = reader.ReadLine();
checkError(firstLine, reader);
}
if (fileName != null)
{
FileStream fileStream = null;
StreamWriter streamWriter = null;
fileStream = new FileStream(fileName,
FileMode.Create);
streamWriter = new StreamWriter(fileStream);
if (firstLine != null)
{
streamWriter.BaseStream.Seek(0, SeekOrigin.End);
streamWriter.WriteLine(firstLine);
}
while (reader.Peek() >= 0)
{
streamWriter.BaseStream.Seek(0, SeekOrigin.End);
streamWriter.WriteLine(reader.ReadLine());
}
streamWriter.Close();
fileStream.Close();
}
else
{
if (firstLine != null)
{
Console.WriteLine(firstLine);
}
while (reader.Peek() >= 0)
{
Console.WriteLine(reader.ReadLine());
}
}
reader.Close();
}
/// <summary>
/// Get the reports by reportName for a Date, and writes the
report
/// at filename or to the console if filename is null.
/// </summary>
/// <param name="reportName">The name of the Report:
activity,
/// disk_space, email_clients, quota_limit_accounts,summary,
/// suspended_account</param>
/// <param name="date">
/// Date of the Report, null date gets latest date
available</param>
/// <param name="fileName">
/// file name to write the report or null for console</
param>
public void getReport(string reportName,
DateTime date, string fileName)
{
this.getReport(reportName, date.ToString(DATE_FORMAT),
fileName);
}
/// <summary>
/// Checks for errors on the Http Response, errors are on XML
format.
/// When the response is xml throws an Exception with the xml
text.
/// </summary>
/// <param name="firstLine">
/// First line of the StreamReader from the Http Response</
param>
/// <param name="reader">StreamReader from the Http Response</
param>
private void checkError(string firstLine, StreamReader reader)
{
if (firstLine.Trim().StartsWith("<?xml"))
{
String xmlText = firstLine;
while (reader.Peek() >= 0)
{
xmlText += reader.ReadLine();
}
throw new Exception(xmlText);
}
}
/// <summary>
/// Get latest available report date,
/// based on report service time zone.
/// Reports for the current date are available after 12:00
PST8PDT
/// the following day.
/// </summary>
/// <returns>Lastest date available</returns>
public DateTime getLatestReportDate()
{
if
(DateTime.UtcNow.AddHours(PUBLISH_TIME_DIFERENCE_TO_UTC).Hour
< PUBLISH_HOUR_OF_DAY)
{
return DateTime.Now.AddDays(-2);
}
else
{
return DateTime.Now.AddDays(-1);
}
}
/// <summary>
/// Gets the properties from the command-line arguments.
/// </summary>
/// <param name="args">command-line arguments</param>
/// <returns>Properties Hashtable</returns>
private static Hashtable getProperties(string[] args)
{
Hashtable properties = new Hashtable();
for (int i = 0; i < args.Length; i++)
{
bool found = false;
for (int j = 0; j < PROPERTY_NAMES.Length; j++)
{
if (args[i].Equals("--" + PROPERTY_NAMES[j]))
{
found = true;
if (i + 1 < args.Length)
{
properties.Add(PROPERTY_NAMES[j], args[i +
1]);
i++;
break;
}
else
{
throw new ArgumentException("Missing value
for " +
"command-line parameter " + args[i]);
}
}
}
if (!found)
{
throw new ArgumentException(
"Unrecognized parameter " + args[i]);
}
}
for (int i = 0; i < REQUIRED_PROPERTY_NAMES.Length; i++)
{
if (properties[REQUIRED_PROPERTY_NAMES[i]] == null)
{
throw new ArgumentException("Missing value for " +
"command-line parameter " +
REQUIRED_PROPERTY_NAMES[i]);
}
}
return properties;
}
/// <summary>
/// Request a report based on command-line arguments.
/// </summary>
/// <param name="args">command-line arguments</param>
public static void Main(string[] args)
{
Hashtable props = null;
try
{
props = getProperties(args);
}
catch (ArgumentException e)
{
Console.WriteLine(e.Message);
Console.WriteLine(USAGE);
return;
}
try
{
ReportingAPIClient client = new ReportingAPIClient();
client.email = (string)props[EMAIL_ARG];
client.password = (string)props[PASSWORD_ARG];
client.domain = (string)props[DOMAIN_ARG];
client.ClientLogin();
client.getReport((string)props[REPORT_ARG],
(string)props[DATE_ARG],
(string)props[OUT_FILE_ARG]);
}
catch (Exception e)
{
Console.WriteLine("Operation failed: {0}", e.Message);
}
}
}
}
=====================================
if you feel anything to edit here then feel free to reply back.
On Aug 20, 9:10 am, "Anirudh (Google)" <[EMAIL PROTECTED]> wrote:
> Hi Arkesh,
>
> I'm sorry i could not completely understand your first question. I
> just downloaded the .NET Reporting API client library source code and
> compiled using "c:\<PATH_TO_.NET_FRAMEWORK>\csc.exe
> ReportingAPIClient.cs" and was able to successfully do so. Do you see
> any error messages at the command prompt?
>
> To obtain tokens from a standalone application, you need to POST to
> ClientLogin interface with your
> credentials:http://code.google.com/apis/apps/reporting/google_apps_reporting_api....
>
> In response you will get SID, LSID and Auth tokens. The Reporting API
> uses SID token, the Auth token is used for Provisioning API and LSID
> token is not relevant in this context.
> The Reporting API client library will obtain a SID token from
> ClientLogin interface and use that to fetch reports so you won't need
> an additional tool.
>
> You did mention that you were using the 'Auth' token for the report
> request and receiving Google home page as response. If you are writing
> your own code instead of using the library, could you please try with
> SID token?
>
> Hope that helps.
>
> -Anirudh
>
> On Aug 19, 1:27 am, Arkesh <[EMAIL PROTECTED]> wrote:
>
> > HI,
> > I have a few questions on Google Api
> > reportinghttp://code.google.com/apis/apps/reporting/google_apps_reporting_api....
>
> > (1) As .NET client library
> > * Download the .NET client library
> > is not working properly at
> > Path to CSC:
> > C:\Windows\Microsoft.NET\Framework\VERSION\csc.exe
>
> > Example:
> > C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe
> > ReportingAPIClient.cs
>
> > Which software or tool or patch one should use for generating SID/LSID/
> > AUTH Key??? is the "poster add ons" for firefox is ok???
>
> > (2) what is difference between SID/LSID/AUTH key???
> > (3) After sending request
> > athttps://www.google.com/hosted/services/v1.0/reports/ReportingData
>
> > ************Accounts Report***************
> > <?xml version="1.0" encoding="UTF-8"?>
> > <rest xmlns="google:accounts:rest:protocol"
> > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
> > <type>Report</type>
> > <token>DQAAAHQAAADgjT625094TCzxcWC4Ss-MH...</token>
> > <domain>example.com</domain>
> > <date>2006-08-01</date>
> > <page>1</page>
> > <reportType>daily</reportType>
> > <reportName>accounts</reportName>
> > </rest>
> > ***************end*************************
> > why it is just generating Google home page only.It should generate
> > actual report.
>
> > (4) Here i consider <type><token> <domain> <date> <page> <reportType>
> > <reportName> as ok.I read all the forums /Google help everything.But
> > still it generate html code of GOOGLE Home page.Even i put Auth key in
> > token,date as valid YYYY-MM-DD format,domain as working valid domain
> > etc???
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Apps APIs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/google-apps-apis?hl=en
-~----------~----~----~----~------~----~------~--~---