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 (WebException we)
            {
                response = (HttpWebResponse)we.Response;
            }

            return response;
        }

        public static void DoHttpPost(string targetUrl, string
postBody,
            out HttpStatusCode resultCode, out string resultBody)
        {
            HttpWebResponse response = DoHttpPost(targetUrl,
postBody);

            using (StreamReader reader = new StreamReader
(response.GetResponseStream()))
            {
                resultCode = response.StatusCode;
                resultBody = reader.ReadToEnd();
            }
        }
    }
}


On Dec 3, 11:14 am, "Julian (Google)" <[EMAIL PROTECTED]> wrote:
> Hi Fred,
>
> To be able to reuse a cached token, you need to modify the AppsService
> by adding the following constructor:
>
>        public AppsService(string domain, string authenticationToken)
>         {
>             this.domain = domain;
>             this.applicationName = "apps-" + domain;
>
>             emailListRecipientService = new EmailListRecipientService
> (applicationName);
>             emailListRecipientService.SetAuthenticationToken
> (authenticationToken);
>             ((GDataRequestFactory)
> emailListRecipientService.RequestFactory).KeepAlive = false;
>
>             emailListService = new EmailListService(applicationName);
>             emailListService.SetAuthenticationToken
> (authenticationToken);
>             ((GDataRequestFactory)
> emailListService.RequestFactory).KeepAlive = false;
>
>             nicknameService = new NicknameService(applicationName);
>             nicknameService.SetAuthenticationToken
> (authenticationToken);
>             ((GDataRequestFactory)
> nicknameService.RequestFactory).KeepAlive = false;
>
>             userAccountService = new UserService(applicationName);
>             userAccountService.SetAuthenticationToken
> (authenticationToken);
>             ((GDataRequestFactory)
> userAccountService.RequestFactory).KeepAlive = false;
>         }
>
> This will allow you to specify a Authentication Token instead of
> username and password.
>
> If you need to perform many operations I recommend using Threads, for
> example 10 running instances of AppService will help you speed up the
> process, you would need to make few modifications to the client to be
> able to use Threads.
>
> The number of users is usually updated every 24 hours, I ran a fix
> script and it should be back to normal at the moment.
>
> Cheers,
> Julian
>
> On Dec 1, 5:06 pm, NCCFred <[EMAIL PROTECTED]> wrote:
>
>
>
> > Hi Julian,
>
> > Thanks for the response.  I can verify that I’m only using one object
> > throughout my application.  I might have a very poor work around for
> > this issue.  Before I run my C# application I go 
> > tohttps://www.google.com/a/students.ncc.edu/UnlockCaptchaandmanually
> > unlock the Captcha.  After I manually unlock the Captcha, I run my
> > application and was able to delete 500+ users with no problem.
>
> > Also I'd like to add that I don't know if there's a way via the .NET
> > API to isolate the Token that's "inside" of the AppsService object.
> > Many suggestions in the Help and FAQ make it sound simple to cache or
> > save this token but I think it’s geared more towards a Java developer
> > and that framework, not the .NET API.  Would you or your team be able
> > to provide a sample C# application that isolates the AppsService
> > token?
>
> > Thanks in advance.
>
> > Fred
>
> > P.S. I have another issue of my “Dashboard” showing a negative number
> > of users, -1636 to be exact.  I don’t know if there two issues are
> > related but I wanted to let you know.  I have already opened up a
> > ticket for the negative user issue.
>
> > On Dec 1, 10:17 am, "Julian (Google)" <[EMAIL PROTECTED]> wrote:
>
> > > Hi Fred,
>
> > > I will investigate further this issue. Can you confirm that you are
> > > not re-authenticating by creating many instances of  the AppsService?
>
> > > From your code snippet, it seems you are creating only one instance,
> > > but I want to double check.
>
> > > Cheers,
> > > Julian.
>
> > > On Nov 28, 4:29 pm, NCCFred <[EMAIL PROTECTED]> wrote:
>
> > > > Hi Frank,
>
> > > > Thanks for the response.  I hope that's not the case because having to
> > > > create 20,000 nicknames will take a long time.
>
> > > > Hopefully someone from Google will be able to respond.  I'm going to
> > > > try and figure this out tonight.
>
> > > > Fred
>
> > > > On Nov 27, 6:56 am, Frank Mantek <[EMAIL PROTECTED]> wrote:
>
> > > > > Guessing: you have reached an API limit on the server side that looks 
> > > > >  
> > > > > the account after a certain amount of api activity in a certain 
> > > > > amount  
> > > > > of time.....
>
> > > > > Someone from the apps team will probably be able to clarify
>
> > > > > Frank Mantek
> > > > > Google
> > > > > On Nov 26, 2008, at 8:13 PM, NCCFred wrote:
>
> > > > > > Hello,
>
> > > > > > I’ve been working with the .NET Google API for several weeks
> > > > > > developing a front end application for Administration of our new
> > > > > > student email.  I have made great success using the API.
>
> > > > > > Today I came across a situation that confused me with the DeleteUser
> > > > > > function.  My application reads in a text file of usernames into an
> > > > > > Array.  I then use a for loop and access the data in the ArrayList. 
> > > > > >  I
> > > > > > pass this String to the DeleteUser Function and for some strange
> > > > > > reason ALWAYS receive a “CaptchaRequiredException” error on the 
> > > > > > 201st
> > > > > > record.  The data is clean and contains no errors.
>
> > > > > > I was thinking a possible time-out value is reached, but looking at
> > > > > > time between the first record (1:05:54) and the 200th record 
> > > > > > (1:13:15)
> > > > > > seems to have no significant pattern to me.
>
> > > > > > Does anyone have any idea why I would always be getting a
> > > > > > CaptchaRequiredException error on the 201st record every time?  Does
> > > > > > the token expire?  The DeleteUser function returns VOID so I really
> > > > > > don’t have anything to go on except my Print out on the “Output” and
> > > > > > by refreshing the Google Administrators Console for our domain.
>
> > > > > > Here’s a sample of the code below.
>
> > > > > > ArrayList CreateList = new ArrayList();  // Holds the User Names
>
> > > > > > AppsService service = new AppsService(DOMAIN, USERNAME, PASSWORD);
>
> > > > > > for( int i=0; i < CreateList.Length; i++)
> > > > > > {
> > > > > > service.DeleteUser( CreateList[i].ToString() );
>
> > > > > > System.Console.WriteLine(“Deleted “ + CreateList[0].ToString() + “
> > > > > > Record # “ + i);
>
> > > > > > }
>
> > > > > > Any help is apperciated, thanks.
> > > > > > Fred- Hide quoted text -
>
> > > > > - Show quoted text -- Hide quoted text -
>
> > > - Show quoted text -- 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to