Hi Tony,
Below is a copy of the C# Code:
I've verified that there's no white spaces and I'm not truncating the
Auth token when I'm doing my "Substring". I'll be playing with this
today - on another note, Any chance of this Constructor making it's
way into the next release of the .NET API? - I think that would he out
a lot of Developers.
using System;
using System.Collections.Generic;
using System.Text;
using Google.GData.Apps;
using Google.GData.Extensions;
using Google.GData.Client;
using System.Web;
using System.Net;
using System.Diagnostics;
using System.IO;
namespace GoogleMod
{
class Program
{
static void Main(string[] args)
{
UserEntry entry;
string email = "[EMAIL PROTECTED]";
string password = "XXXXX";
string Auth = GetSecurityToken(email, password);
Debug.WriteLine("Auth = " + Auth);
AppsService service = new AppsService("students.ncc.edu",
Auth);
try
{
entry = service.RetrieveUser("XXXXXXXX");
Debug.WriteLine(entry.Name.FamilyName);
}
catch (AppsException e)
{
Debug.WriteLine(e.ErrorCode);
Debug.WriteLine(e.Data);
Debug.WriteLine(e.Message);
Debug.WriteLine(e.Reason);
Debug.WriteLine(e.Response);
Debug.WriteLine(e.ResponseString);
Debug.WriteLine(e.Source);
}
}
static string GetSecurityToken(string email, string password)
{
WebRequest request = WebRequest.Create("https://
www.google.com/accounts/ClientLogin");
request.Method = "POST"; // Set the Method property of the
request to POST
string postData = "accountType=HOSTED&Email=" + email +
"&Passwd=" + password + "&service=xapi&source=NCC-TESTAPP-1.0";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-
urlencoded"; // Set the ContentType property of the WebRequest.
request.ContentLength = byteArray.Length; // Set the
ContentLength property of the WebRequest
Stream dataStream = request.GetRequestStream(); // Get the
request stream
dataStream.Write(byteArray, 0, byteArray.Length); // Write
the data to the request stream
dataStream.Close(); // Close the Stream object
WebResponse response = request.GetResponse(); // Get the
response
Debug.WriteLine(((HttpWebResponse)
response).StatusDescription);
dataStream = response.GetResponseStream(); // Get the
stream containing content returned by the server.
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Debug.WriteLine("Server Responded " + responseFromServer);
string sid = string.Empty;
string lsid = string.Empty;
string Auth = string.Empty;
foreach (string token in responseFromServer.Split('\n'))
{
if (token.StartsWith("SID="))
{
sid = token.Substring(4);
}
else if (token.StartsWith("LSID="))
{
lsid = token.Substring(5);
}
else if (token.StartsWith("Auth="))
{
Auth = token.Substring(5);
}
else
{
if (token.Trim().Length > 0)
{
Debug.WriteLine("Error authenticating Google
user " + email);
}
}
}
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
return Auth;
}
}
}
On Dec 3, 11:48 pm, "Tony (Google)" <[EMAIL PROTECTED]> wrote:
> Hi Fred,
>
> Can you post the latest code here so I can help you take a look first?
>
> Thanks,
>
> --Tony
>
> On Dec 3, 8:18 pm, NCCFred <[EMAIL PROTECTED]> wrote:
>
>
>
> > Hi Tony,
>
> > I forgot to add service name and source.
>
> > string postData = "accountType=HOSTED&Email=" + email + "&Passwd=" +
> > password + "&service=xapi&source=NCC-TESTAPP-1.0";
>
> > Once I did this I was able to get the "Auth" token, but I'm still
> > getting the same error. I'm wondering if Julian could email me the
> > DLL's.
>
> > Error:
> > ---------
> > Google.GData.Client.GDataRequestException was unhandled
> > Message="Execution of request
> > failed:https://www.google.com/a/feeds/students.ncc.edu/user/2.0/intisof"
> > Source="Google.GData.Apps"
> > ResponseString="<HTML>\n<HEAD>\n<TITLE>Token invalid</TITLE>\n</HEAD>
> > \n<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\">\n<H1>Token invalid</H1>
> > \n<H2>Error 401</H2>\n</BODY>\n</HTML>\n"
> > StackTrace:
> > at Google.GData.Apps.UserService.Query(UserQuery feedQuery)
> > at Google.GData.Apps.AppsService.RetrieveUser(String username)
> > at GoogleMod.Program.Main(String[] args) in C:\Documents and
> > Settings\Administrator\Desktop\VSprojects\GoogleMod\GoogleMod
> > \Program.cs:line 29
> > at System.AppDomain._nExecuteAssembly(Assembly assembly, String
> > [] args)
> > at System.AppDomain.ExecuteAssembly(String assemblyFile,
> > Evidence assemblySecurity, String[] args)
> > at
> > Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
> > at System.Threading.ThreadHelper.ThreadStart_Context(Object
> > state)
> > at System.Threading.ExecutionContext.Run(ExecutionContext
> > executionContext, ContextCallback callback, Object state)
> > at System.Threading.ThreadHelper.ThreadStart()
>
> > On Dec 3, 8:40 pm, "Tony (Google)" <[EMAIL PROTECTED]> wrote:
>
> > > Hi Fred,
>
> > > Yes, there is really an "Auth" token in the response. Since all three
> > > tokens (SID, LSID and Auth) will be returned in the response, my guess
> > > is that your current code logic just skips the second and third tokens
> > > as the first condition of the "if" statement is satisfied. You may
> > > want to update your code logic and create an array to store all three
> > > tokens for other services. For example, the Reporting API is using
> > > the SID.
>
> > > Thanks,
>
> > > --Tony
>
> > > On Dec 3, 5:13 pm, NCCFred <[EMAIL PROTECTED]> wrote:
>
> > > > Hi Julian,
>
> > > > Thanks for the code sample, it took me sometime to actually understand
> > > > exactly where this belonged. I was able to add that Constructor code
> > > > to the AppsService Class and recompile the project which produced 3
> > > > new DLL's (Extensions.dll, Client.dll and Apps.dll). I copied these
> > > > to the correct location on my machine and made a small test app (see
> > > > code below).
>
> > > > Questions:
> > > > 1. Am I supposed to get a value for "Auth"? The documentation says I
> > > > will get a SID, LSID and Auth, I do not see the "Auth". I assumed to
> > > > use the SID and got the following error below when trying to
> > > > RetrieveAllUsers();
>
> > > > Exception/Error:
> > > > Execution of request
> > > > failed:https://www.google.com/a/feeds/students.ncc.edu/user/2.0
>
> > > > Google.GData.Client.GDataRequestException was unhandled
> > > > Message="Execution of request
> > > > failed:https://www.google.com/a/feeds/students.ncc.edu/user/2.0"
> > > > Source="Google.GData.Apps"
> > > > ResponseString="<HTML>\n<HEAD>\n<TITLE>Token invalid</TITLE>\n</HEAD>
> > > > \n<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\">\n<H1>Token invalid</H1>
> > > > \n<H2>Error 401</H2>\n</BODY>\n</HTML>\n"
> > > > StackTrace:
> > > > at Google.GData.Apps.UserService.Query(UserQuery feedQuery)
> > > > at Google.GData.Apps.AppsService.RetrieveAllUsers()
> > > > at GoogleMod.Program.Main(String[] args) in C:\Documents and
> > > > Settings\Administrator\Desktop\TempVS\GoogleMod\GoogleMod
> > > > \Program.cs:line 30
> > > > at System.AppDomain._nExecuteAssembly(Assembly assembly, String
> > > > [] args)
> > > > at System.AppDomain.ExecuteAssembly(String assemblyFile,
> > > > Evidence assemblySecurity, String[] args)
> > > > at
> > > > Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
> > > > at System.Threading.ThreadHelper.ThreadStart_Context(Object
> > > > state)
> > > > at System.Threading.ExecutionContext.Run(ExecutionContext
> > > > executionContext, ContextCallback callback, Object state)
> > > > at System.Threading.ThreadHelper.ThreadStart()
>
> > > > My Code:
> > > > Program.cs
> > > > ---------------------
>
> > > > using System;
> > > > using System.Collections.Generic;
> > > > using System.Text;
> > > > using Google.GData.Apps;
> > > > using Google.GData.Extensions;
> > > > using Google.GData.Client;
> > > > using System.Web;
> > > > using System.Net;
> > > > using System.Diagnostics;
> > > > using System.IO;
>
> > > > namespace GoogleMod
> > > > {
> > > > class Program
> > > > {
> > > > static void Main(string[] args)
> > > > {
> > > > UserFeed feed = null;
>
> > > > string email = "[EMAIL PROTECTED]";
> > > > string password = "XXXXX";
>
> > > > string SID = GetSecurityToken(email, password);
> > > > Debug.WriteLine("SID = " + SID);
>
> > > > AppsService service = new AppsService("students.ncc.edu",
> > > > SID);
>
> > > > try
> > > > {
> > > > feed = service.RetrieveAllUsers();
>
> > > > for (int i = 0; i < feed.Entries.Count; i++)
> > > > {
> > > > UserEntry entry = feed.Entries[i] as UserEntry;
>
> > > > Debug.WriteLine(entry.Name.FamilyName);
> > > > Debug.WriteLine(entry.Name.GivenName);
> > > > Debug.WriteLine(entry.Login.UserName);
> > > > }
>
> > > > }
> > > > catch (AppsException e)
> > > > {
> > > > Debug.WriteLine(e.ErrorCode);
> > > > Debug.WriteLine(e.Data);
> > > > Debug.WriteLine(e.Message);
> > > > Debug.WriteLine(e.Reason);
> > > > Debug.WriteLine(e.Response);
> > > > Debug.WriteLine(e.ResponseString);
> > > > Debug.WriteLine(e.Source);
> > > > }
>
> > > > }
>
> > > > static string GetSecurityToken(string email, string password)
> > > > {
> > > > string postBody = string.Format(
> > > > "accountType=HOSTED&Email={0}&Passwd={1}",
> > > > HttpUtility.UrlEncode(email),
> > > > HttpUtility.UrlEncode(password));
>
> > > > string targetUrl = "https://www.google.com/accounts/
> > > > ClientLogin";
>
> > > > string result;
> > > > HttpStatusCode status;
>
> > > > WebUtility.DoHttpPost(targetUrl, postBody, out status, out
> > > > result);
>
> > > > string sid = string.Empty;
> > > > string lsid = string.Empty;
>
> > > > foreach (string token in result.Split('\n'))
> > > > {
> > > > if (token.StartsWith("SID="))
> > > > {
> > > > sid = token.Substring(4);
> > > > }
> > > > else if (token.StartsWith("LSID="))
> > > > {
> > > > lsid = token.Substring(5);
> > > > }
> > > > else
> > > > {
> > > > if (token.Trim().Length > 0)
> > > > {
> > > > Debug.WriteLine("Error authenticating Google
> > > > user " + email);
> > > > }
> > > > }
> > > > }
>
> > > > return sid;
> > > > }
>
> > > > }
>
> > > > }
>
> > > > WebUtility.cs
> > > > ----------------------
>
> > > > using System;
> > > > using System.IO;
> > > > using System.Net;
> > > > using System.Text;
>
> > > > namespace GoogleMod
> > > > {
> > > > /// <summary>
> > > > /// Provides convenience methods for issuing a HTTP "POST" command
> > > > and returning the results.
> > > > /// </summary>
> > > > public class WebUtility
> > > > {
> > > > private WebUtility() { ;}
>
> > > > public static HttpWebResponse DoHttpPost(string targetUrl,
> > > > string postBody)
> > > > {
> > > > HttpWebRequest request =
> > > > (HttpWebRequest)WebRequest.Create(targetUrl);
>
> > > > request.Method = "POST";
>
> > > > Encoding encoding = Encoding.GetEncoding("ISO-8859-1");
>
> > > > byte[] postBuffer = encoding.GetBytes(postBody);
>
> > > > request.ContentLength = postBuffer.Length;
> > > > request.ContentType = "application/x-www-form-urlencoded";
>
> > > > using (Stream requestStream = request.GetRequestStream())
> > > > {
> > > > requestStream.Write(postBuffer, 0, postBuffer.Length);
> > > > }
>
> > > > HttpWebResponse response;
>
> > > > try
> > > > {
> > > > response = (HttpWebResponse)request.GetResponse();
> > > > }
> > > > catch
>
> ...
>
> read more »- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---