Gmail auto-login

Started by mcvpjd3, April 02, 2014, 01:53:47 AM

Previous topic - Next topic

mcvpjd3

Hi folks, just wondering if anyone can help with this one.

We user Gmail as our corporate email system. We have some external people that we give gmail accounts to, but we only want them to be able to log in to those accounts from the PCs we give them. The only way we can see to do this is if they don't know the gmail password. So, in order for this to work we need to be able to log them in to Gmail on the PC using a password that they don't know.  Has anyone managed to get gmail to log in from an application?

There are a few tricks out there for doing it from a bookmark, but this would involve the password to be part of the bookmark URL in plain text.

Any help appreciated.

Deana

Here is a code sample I threw together that should accomplish logging into your online Gmail account:

Code (winbatch) Select
;***************************************************************************
;**                  Gmail Auto-Login
;**             Logs into your Gmail account online.
;**
;** Developer: Deana Falk 2014.04.02                 
;***************************************************************************

gmailacct = 'soandso@gmail.com'
gmailpswd = '****'


#DefineFunction udfIEPageLoadWait( objIE )
    ; Wait for webpage to load
    While !(objIE.readyState == 'complete' || objIE.readyState == 4 )
       Timedelay(0.1)           
    EndWhile
    While !(objIE.document.readyState == 'complete' || objIE.document.readyState == 4 )
       Timedelay(0.1)
    EndWhile
    Return 1
#EndFunction

url = "http://www.gmail.com"

; Initialize MSIE object
oIE   = ObjectCreate("InternetExplorer.Application")
oIE.Visible = @TRUE ; Change to @FALSE to hide the process from the user
oIE.Navigate(url)

; Wait for webpage to load
udfIEPageLoadWait( oIE )

;***************************************************************************
; Get forms collection
;***************************************************************************
; Get document object
oDoc = oIE.Document

; Forms Collection 
oForms = oDoc.Forms
oForm = 0
if oDoc != 0 ; Check if you have a valid document handle
   ; Loop through the collection of forms using index
   count = oForms.Length
   For index = 0 to count-1
         oForm = oForms.Item(index)
         If ObjectTypeGet(oForm)=="EMPTY" then continue
           formname = oForm.name
           if formname == 'gaia_loginform' then break
   Next   
Else
   Pause('Notice','Unable to obtain a handle to the document object on this webpage. Check URL.')
Endif
If oForm == 0
   Pause('Notice','Uable to locate the form on this page!')
   ; Quit
   oIE.Quit
   
   ; Close open COM/OLE handles
   oDoc = 0
   oIE = 0
EndIf

ignorehidden = 1
inputonly = 1
elementindex = 0
cElements = oForm.Elements
count = cElements.Length
ForEach oElement in cElements
      ; Get element attributes
      ; Alternatice option: oElement.getAttribute("attributename")
      tag = oElement.nodeName ;The value of tagName is the same as that of nodeName.
      type = StrUpper(oElement.Type)
      id = oElement.Id
      name = oElement.Name
      value = oElement.Value   

      ; Check if user wants to ignore HIDDEN form elements
      If ignorehidden
          If StrUpper(type) == 'HIDDEN' then continue
      EndIf

      ; Check if user wants to see non INPUT form elements
      If inputonly
         If StrUpper(tag) != 'INPUT' then continue
      EndIf

      ; Highlight each element on the form
      oElement.style.border = "1mm solid red"
     
      ; Set focus on element
      If !oElement.disabled
         ErrorMode(@off)
         oElement.Focus()
         ErrorMode(@cancel)
      EndIf
     
      ; Display results
      ;Pause('','Element Index: ' : elementindex : @lf :'Tag: ' : tag : @lf : 'Type: ' : type : @lf :'Id: ' : id : @lf :'Name: ' : name : @lf :'Value: ': value )

      Switch @True
         case name == 'Email'
            if value == '' then oElement.Value = gmailacct
            ;Message( name, oElement.Value )
         break

         case name == 'Passwd'
            if value == '' then oElement.Value = gmailpswd
            ;Message( name, oElement.Value )
         break

         case name == 'signIn'
            ; Click button
            ;oElement.Click 
           
            ;or

            ; Click button on IE 9 and newer
            ; createEvent initEvent DispatchEvent
            oEvent = oIE.document.createEvent("HTMLEvents")
            oEvent.initEvent("click", @TRUE, @TRUE)
            oElement.dispatchEvent(oEvent)
            oEvent = 0
         break

      EndSwitch
     

      ; Remove element highlight
      oElement.style.border = ""

      ; Increment element counter
      elementindex = elementindex+1
Next

Pause('Gmail Auto-Login', 'You should now be logged in')

; Quit
;oIE.Quit

; Close open COM/OLE handles
oDoc = 0
oIE = 0
Exit
Deana F.
Technical Support
Wilson WindowWare Inc.

Deana

This simplified Gmail login code sample also seems to work:


Code (winbatch) Select
;***************************************************************************
;**                  Gmail Auto-Login Simple
;**             Logs into your Gmail account online.
;**
;** Developer: Deana Falk 2014.04.02                 
;***************************************************************************

gmailacct = 'soandso@gmail.com'
gmailpswd = '****'

; Ensure that you are signed-out of Gmail
 
url = "http://gmail.com"
oIE = ObjectCreate('internetexplorer.application')
oIE.visible = @true;
oIE.navigate(url);
while oIE.Busy 
    TimeDelay(1)
endwhile

; Wait for element
;While @true
   oElement = oIE.Document.getElementById("Email")
;   type = ObjectTypeGet(obj)
;   if type != 'NULL' then break
;EndWhile
oElement.value = gmailacct


; Wait for element
;While @true
   oElement = oIE.Document.getElementById("Passwd")
;   type = ObjectTypeGet(obj)
;   if type != 'NULL' then break
;EndWhile
oElement.value = gmailpswd

; Wait for element
;While @true
   oElement = oIE.Document.getElementById("signIn")
;   type = ObjectTypeGet(obj)
;   if type != 'NULL' then break
;EndWhile

; Click button
;oElement.Click             
;or
; Click button on IE 9 and newer
; createEvent initEvent DispatchEvent
oEvent = oIE.Document.createEvent("HTMLEvents")
oEvent.initEvent("click", @TRUE, @TRUE)
oElement.dispatchEvent(oEvent)

oEvent = 0
oElement = 0
oIE = 0
Exit
Deana F.
Technical Support
Wilson WindowWare Inc.

mcvpjd3

Thanks Deana, the first one worked fantastic. (I kept getting an error on the second one - 3131: OleExecute: Not a valid OLE object. Period may have been used in place of a comma on line 27). I haven't used the COM/OLE stuff much in winbatch so I wouldn't know where to start looking into it.

I do have one more favour to ask. How would I go about amending this (if possible) to work with Google Chrome. We tell all users to use Chrome for Gmail as there are lots of bits of Gmail that only work properly in Chrome.

Thanks again for the code :-)

lionking

Thanks Deana,

Both scripts are working fine with me.
I especially like the simplified version since I do not have to click on the OK button to have it login to my email account.

Thanks for your neat and cool scripts.

stanl

FYI:  I tried the simplified script with IE 11.  While it did log in to Gmail correctly, an error popped up. See attached.
And, yes, the correct gmail account and password were in the script. I also cleared my caches and again the script worked but with the error. Strange.

Deana

Quote from: stanl on May 26, 2014, 04:06:30 AM
FYI:  I tried the simplified script with IE 11.  While it did log in to Gmail correctly, an error popped up. See attached.
And, yes, the correct gmail account and password were in the script. I also cleared my caches and again the script worked but with the error. Strange.

I recommend adding code to check that you have a valid oElement object. Maybe post some debugtrace output from your script ( removing any private info ) then post here for review.
Deana F.
Technical Support
Wilson WindowWare Inc.

stanl

It solved itself, after running CCleaner.