Converting from GoogleApp Provisioning API to new SDKs - api

We have been creating and managing our GoogleApps accounts for a couple years by simply generating a block of XML code for the Provisioning API, and then using VBScript to do a POST. Now GoogleApps is requiring that we move to the new Administrative SDKs, and I'm not understanding how, or even if, we can do something similar with the new system.
Here is an example of the code we use to first get an authentication token:
' Create and send XML message to GoogleApps requesting Authentication Token
Set objXMLHTTP = CreateObject("Microsoft.XmlHttp")
objXMLHTTP.open "POST", "https://www.google.com/accounts/ClientLogin", FALSE
objXMLHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objXMLHTTP.send "&Email=Administrator%40company%2Ecom%2Eedu&Passwd=P#ssw0rd&accountType=HOSTED&service=apps"
If Err.Number <> 0 Then
WScript.Echo "Error: send request for GoogleApp Authentication Token failed"
WScript.Quit(1)
End If
' Get response from GoogleApps
strGGAATAuthToken = objXMLHTTP.responseText
If Err.Number <> 0 Then
WScript.Echo "ERROR: Getting GoogleApp Authentication Token (XMLHTTP.responseText) "
WScript.Quit(1)
End If
' Check for known errors in response text
If LCase(Left(strGGAATAuthToken, 6)) = "error=" Then
WScript.Echo "ERROR: GoogleApp replied with Error when asking for Authentication Token"
WScript.Quit(1)
Else
' Extract and return Authentication Token from response text
strGGAATToken = Mid(strGGAATAuthToken, InStr(strGGAATAuthToken, "Auth=") + 5)
GetGAAuthToken = True
End If
Here is a sample of the code we then use to create the account:
' Create XML Record that will be sent to GoogleApps
strXMLRecord = "<?xml version=" & Chr(34) & "1.0" & Chr(34) & " encoding=" & Chr(34) & "UTF-8" & Chr(34) & _
"?>" & vbCRLF
strXMLRecord = strXMLRecord & "<atom:entry xmlns:atom=" & Chr(34) & "http://www.w3.org/2005/Atom" & Chr(34) & _
vbCRLF
strXMLRecord = strXMLRecord & " xmlns:apps=" & Chr(34) & "http://schemas.google.com/apps/2006" & Chr(34) & _
">" & vbCRLF
strXMLRecord = strXMLRecord & " <atom:category scheme=" & Chr(34) & "http://schemas.google.com/g/2005#kind" & _
Chr(34) & vbCRLF
strXMLRecord = strXMLRecord & " term=" & Chr(34) & "http://schemas.google.com/apps/2006#user" & Chr(34) & _
"/>" & vbCRLF
strXMLRecord = strXMLRecord & " <apps:login userName=" & Chr(34) & strCGAAUsername & Chr(34) & vbCRLF
strXMLRecord = strXMLRecord & " password=" & Chr(34) & strCGAAPwd & Chr(34) & vbCRLF
strXMLRecord = strXMLRecord & " changePasswordAtNextLogin=" & Chr(34) & "true" & Chr(34) & vbCRLF
strXMLRecord = strXMLRecord & " suspended=" & Chr(34) & "false" & Chr(34) & "/>" & vbCRLF
' The following line is just so we have the syntax if we need to set quotas
'*****strXMLRecord = strXMLRecord & " <apps:quota limit=" & Chr(34) & "2048" & Chr(34) & "/>" & vbCRLF*****
strXMLRecord = strXMLRecord & " <apps:name familyName=" & Chr(34) & strCGAALastName & Chr(34) & " givenName=" & _
Chr(34) & strCGAAFirstName & Chr(34) & "/>"
strXMLRecord = strXMLRecord & vbCRLF & "</atom:entry>" & vbCRLF
' Create XML object, set headers, and send to GoogleApps
Set objXMLHTTP = CreateObject("Microsoft.XmlHttp")
objXMLHTTP.open "POST", "https://apps-apis.google.com/a/feeds/company.com/user/2.0", FALSE
objXMLHTTP.setRequestHeader "Content-type", "application/atom+xml"
objXMLHTTP.setRequestHeader "Authorization", "GoogleLogin auth=" & strGAAuthToken
objXMLHTTP.send strXMLRecord
If Err.Number <> 0 Then
WScript.Echo "ERROR: unable to XMLHTTP.send for GoogleApps acct creation"
CreateGAAccount = False
WScript.Quit(1)
End If
' Get response from GoogleApps
strResponseText = objXMLHTTP.responseText
If Err.Number <> 0 Then
WScript.Echo "ERROR: unable to get objXMLHTTP.responseText during GoogleApps acct creation"
CreateGAAccount = False
WScript.Quit(1)
End If
' If response reports an error exit function returning False
If InStr(Lcase(strResponseText), "errorcode=") <> 0 Then
WScript.Echo "ERROR: unable to create GoogleApps account"
CreateGAAccount = False
WScript.Quit(1)
End If
' Log GoogleApps account information returned from creation
WScript.Echo "GoogleApp account created for: " & strCGAAUsername
It's probably obvious, but I have a Windows background, not Linux; and I've done scripting, but not real programming. (And I have no experience doing Java and/or other Web programming at all.)
Thanks for any help!!

The steps to use the Admin SDK are very similar.
First you will get authenticated, now google uses Oauth 2 here you can find the documentation on that https://developers.google.com/accounts/docs/OAuth2
here you can test how Oauth works: https://developers.google.com/oauthplayground/
After being authenticated you can now call the Directory API to create a new user. Here is the documentation related to the insert method https://developers.google.com/admin-sdk/directory/v1/reference/users/insert
As you can see in the Doc, you will be sending the same parameters (name, password, etc.) but now it won't be formatted as xml, instead those parameter will be formatted as json (here is a little information on json formatting: http://www.w3schools.com/json/)
I know it's a lot of information, I hope it helps.

Related

VBA send file in binary code to API via POST method

I have last problem with my code. Code sending via POST variables from Outlook to API.
My last problem is how to send variables and mail attachment in one POST request to API.
first 7zip comprimation for mail attachement:
strSource = cstrFileAttachment & "*.*"
strTarget = cstrFileattachment & "Zip\attachment.zip"
strPassword = randomPassword(cintLongPassword)
strCommand = """" & PathZipProgram & """ a -tzip """ & strTarget & _
""" -p" & strPassword & " """ & strSource & """"
Now i have c:\attachment\attachment.zip
Next part is send variables to API:
Dim SendDataToApi As String
strFrom = 1
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
URL = "https://url.domain.com/api/data"
objHTTP.Open "POST", URL, False
objHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
SendDataToApi = "mail_from=" & strFrom & "&mail_to=" & strKomu & "&file_attachment=" & fileAttachment & "&url_attribute=" & strWebLink & "&sms_code=" & strHeslo & "&id_message=" & IdMessage & "&mobile_phone=" & strPhone & "&date_send=" & strDateSend & "&date_expiration=" & strDateExp
objHTTP.Send SendDataToApi
Variables are sended, but fileAttachment is send as a string, so API get path where file is saved.
My question is how implement code below (found on internet) to my code sendDataToApi and POST attachment.zip as a binary insteed of string.
Private Function Upload(strUploadUrl, strFilePath, strFileField, strDataPairs)
'Uses POST to upload a file and miscellaneous form data
strUploadUrl = "https://url.domain.com/api/data"
strFilePath = cstrFilepathAttachment & "Zip\attachment.zip"
'strFileField is the web page equivalent form field name for the file (File1)
'strDataPairs are pipe-delimited form data pairs (foo=bar|snap=crackle)
Const MULTIPART_BOUNDARY = "---------------------------0123456789012"
Dim ado, rs
Dim lngCount
Dim bytFormData, bytFormStart, bytFormEnd, bytFile
Dim strFormStart, strFormEnd, strDataPair
Dim web
Const adLongVarBinary = 205
'Read the file into a byte array
Set ado = CreateObject("ADODB.Stream")
ado.Type = 1
ado.Open
ado.LoadFromFile strFilePath
bytFile = ado.Read
ado.Close
'Create the multipart form data.
'Define the end of form
strFormEnd = vbCrLf & "--" & MULTIPART_BOUNDARY & "--" & vbCrLf
'First add any ordinary form data pairs
strFormStart = ""
For Each strDataPair In Split(strDataPairs, "|")
strFormStart = strFormStart & "--" & MULTIPART_BOUNDARY & vbCrLf
strFormStart = strFormStart & "Content-Disposition: form-data; "
strFormStart = strFormStart & "name=""" & Split(strDataPair, "=")(0) & """"
strFormStart = strFormStart & vbCrLf & vbCrLf
strFormStart = strFormStart & Split(strDataPair, "=")(1)
strFormStart = strFormStart & vbCrLf
Next
'Now add the header for the uploaded file
strFormStart = strFormStart & "--" & MULTIPART_BOUNDARY & vbCrLf
strFormStart = strFormStart & "Content-Disposition: form-data; "
strFormStart = strFormStart & "name=""" & strFileField & """; "
strFormStart = strFormStart & "filename=""" & Mid(strFilePath, InStrRev(strFilePath, "\") + 1) & """"
strFormStart = strFormStart & vbCrLf
strFormStart = strFormStart & "Content-Type: application/upload" 'bogus, but it works
strFormStart = strFormStart & vbCrLf & vbCrLf
'Create a recordset large enough to hold everything
Set rs = CreateObject("ADODB.Recordset")
rs.Fields.Append "FormData", adLongVarBinary, Len(strFormStart) + LenB(bytFile) + Len(strFormEnd)
rs.Open
rs.AddNew
'Convert form data so far to zero-terminated byte array
For lngCount = 1 To Len(strFormStart)
bytFormStart = bytFormStart & ChrB(Asc(Mid(strFormStart, lngCount, 1)))
Next
rs("FormData").AppendChunk bytFormStart & ChrB(0)
bytFormStart = rs("formData").GetChunk(Len(strFormStart))
rs("FormData") = ""
'Get the end boundary as a zero-terminated byte array
For lngCount = 1 To Len(strFormEnd)
bytFormEnd = bytFormEnd & ChrB(Asc(Mid(strFormEnd, lngCount, 1)))
Next
rs("FormData").AppendChunk bytFormEnd & ChrB(0)
bytFormEnd = rs("formData").GetChunk(Len(strFormEnd))
rs("FormData") = ""
'Now merge it all
rs("FormData").AppendChunk bytFormStart
rs("FormData").AppendChunk bytFile
rs("FormData").AppendChunk bytFormEnd
bytFormData = rs("FormData")
rs.Close
'Upload it
Set web = CreateObject("WinHttp.WinHttpRequest.5.1")
web.Open "POST", strUploadUrl, False
web.SetRequestHeader "Content-Type", "multipart/form-data; boundary=" & MULTIPART_BOUNDARY
web.Send bytFormData
End Function
UPDATE:
when i added part of code from #Tim Williams
in my database is saved file as /tmp/phpAJOtVw what do i doing wrong ?
Upload is a standalone method, so you should be able to call it something like this:
sUrl = "https://url.domain.com/api/data" 'API endpoint
fPath = "c:\attachment\attachment.zip" 'attachment location
FileFieldName = "checkYourApiForThis" 'API specifies this
DataPairs = "mail_from=" & strFrom & _
"&mail_to=" & strKomu & _
"&file_attachment=" & fileAttachment & _
"&url_attribute=" & strWebLink & _
"&sms_code=" & strHeslo & _
"&id_message=" & IdMessage & _
"&mobile_phone=" & strPhone & _
"&date_send=" & strDateSend & _
"&date_expiration=" & strDateExp
'call the function
'expects |-delimited name/value pairs, not &, so do a replace
Upload sUrl, fPath, FileFieldName, Replace(DataPairs, "&", "|")
You should remove these hard-coded values from the top of Upload:
strUploadUrl = "https://url.domain.com/api/data"
strFilePath = cstrFilepathAttachment & "Zip\attachment.zip"

How to change font color for updated Access data in Outlook mail

In Access 2010 I have tables, e.g. Employee(Pracownicy). I can update the data in the table using the subform and the update button.
Updating the data in the subform automatically generates an Outlook mail containing the data in the updated record.
I need to change font color for updated data in the mail body.
The code to update the data and generate e-mail:
Private Sub cmdUpdate2_Click()
CurrentDb.Execute "update Pracownicy" & _
" SET Identyfikator='" & Me.txtID & "'" & _
", Imie='" & Me.txtImie & "'" & _
", Nazwisko ='" & Me.txtNazwisko & "'" & _
", Wiek ='" & Me.txtWiek & "'" & _
", Data_urodzenia ='" & Me.txtData & "'" & _
", Miejsce_urodzenia ='" & Me.txtMiejsce & "'" & _
", Miejscowosc ='" & Me.txtMiejscowosc & "'" & _
", Plec ='" & Me.txtPlec & "'" & _
" where Identyfikator='" & Me.txtID & "'"
'------------------------------------SEND EMAIL----------------------
'Dim varName As Variant
'Dim strUCC As String
Dim varSubject As Variant
Dim varBody As Variant
Dim Poczta As Object
Dim MojMail As Object
On Error Resume Next
'varName = ""
varSubject = "Employer List "
varBody = "Hello" & _
"<br><br>Employer List: " & _
"<br><br><B>Identyfikator:</B> " & Me.txtID & " " & _
"<br><B>Imie:</B> " & Me.txtImie & " " & _
"<br><B>Nazwisko:</B> " & Me.txtNazwisko & " " & _
"<br><B>Wiek:</B> " & Me.txtWiek & " " & _
"<br><B>Data urodzenia:</B> " & Me.txtData & " " & _
"<br><B>Miejsce urodzenia:</B> " & Me.txtMiejsce & " " & _
"<br><B>Miejscowosc:</B> " & Me.txtMiejscowosc & " " & _
"<br><B>Plec:</B> " & Me.txtPlec & " "
Set Poczta = CreateObject("outlook.application")
Set MojMail = Poczta.createitem(0)
With MojMail
'.To =
'.BCC =
.subject = varSubject
'.ReadReceiptRequested = True
'.originatorDeliveryReportRequested = True
.htmlbody = varBody & "<br>"
.display
'.send
End With
Set Poczta = Nothing
Set MojMail = Nothing
If Err.Number <> 0 Then
MsgBox ("Atention")
End If
On Error GoTo 0
'------------------------------------------------------------------------
DoCmd.Close
MsgBox ("End Update")
End Sub
I think this becomes more of an HTML question rather than VBA. Try adding a FONT tag to the following line and see if that works for you.
"<br><br><B><font color="red">Identyfikator:</font></B> " & Me.txtID & " " & _

In VBA OutLook, how do I string together Namespace property specifiers? I'm trying to filter inbox by subject line for both unread and read emails.

I'm trying to parse through my inbox by subject line for both unread and read emails.
However, when I parse through subject line only, the filter returns only read emails.
'Filters inbox by subject line (only returns READ emails)
strFilter = "#SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " LIKE '%M4 Vitals Grade%'"
'Returns Entire Inbox (READ AND UNREAD emails)
strFilter2 = "#SQL=" & Chr(34) & "urn:schemas:httpmail:read" & Chr(34) & "=True"
'Does not work...
strFilter3 = "#SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " LIKE '%M4 Vitals Grade%' WHERE " & Chr(34) & "urn:schemas:httpmail:read" & Chr(34) & " = true"
Set filteredItems = objFolder.Items.Restrict(strFilter)
Please let me know if you need more information! Thank you
Try the following one:
strFilter =" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " LIKE '%M4 Vitals Grade%' AND [UnRead] = True"

Send mail using VB Script?

I have the following code to monitor a drive. Now I an getting Echo for each file creation or deletion event.
Is there and way to modify the WScript.Echo to send a mail notification?
strDrive = "c"
arrFolders(0) = strDrive & "\\\\"
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
'Loop throught the array of folders setting up the monitor for Each
i = 0
For Each strFolder In arrFolders
'Create the event sink
strCommand = "Set EventSink" & i & " = WScript.CreateObject" & "(""WbemScripting.SWbemSink"", ""SINK" & i & "_"")"
ExecuteGlobal strCommand
'Setup Notification
strQuery = "SELECT * FROM __InstanceOperationEvent WITHIN 1 " & "WHERE Targetinstance ISA 'CIM_DirectoryContainsFile'" & " and TargetInstance.GroupComponent = " & "'Win32_Directory.Name=""" & strFolder & """'"
strCommand = "objWMIservice.ExecNotificationQueryAsync EventSink" & i & ", strQuery"
ExecuteGlobal strCommand
'Create the OnObjectReady Sub
strCommand = "Sub SINK" & i & "_OnObjectReady(objObject, " & "objAsyncContext)" & VbCrLf & vbTab & "Wscript.Echo objObject.TargetInstance.PartComponent" & VbCrLf & "End Sub"
WScript.Echo strCommand
ExecuteGlobal strCommand
i = i + 1
Next
WScript.Echo "Waiting for events..."
i = 0
While (True)
Wscript.Sleep(1000)
Wend
Instead of Echoing like below:
strCommand = "Sub SINK" & i & "_OnObjectReady(objObject, " & "objAsyncContext)" & VbCrLf & vbTab & "Wscript.Echo objObject.TargetInstance.PartComponent" & VbCrLf & "End Sub"
I want to send a mail like this:
strCommand = "Sub SINK" & i & "_OnObjectReady(objObject, " & "objAsyncContext)" & VbCrLf & vbTab & "
Set outobj = CreateObject("Outlook.Application")
Set mailobj = outobj.CreateItem(0)
With mailobj
.To = toAddress
.Subject = Subject
.HTMLBody = strHTML
.Send
End With
" & VbCrLf & "End Sub"
Is it possible or is there an other way to do this..?
I don't know what server do you use, but on Windows 2003 and 2008 e.g. you can use CDO object to create a email. You might use a smart host to send your email to.
Check this link: http://www.paulsadowski.com/wsh/cdo.htm
Also you can choose any free email component to create a email and use a smtp server to send your email. Or check this side where you can use a component including many examples how to do it: http://www.chilkatsoft.com/email-activex.asp.
** UPDATED **
This Script checks and send a email as you requestted:
strDrive = "d:"
Dim arrFolders(0) : arrFolders(0) = strDrive & "\\\\"
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
'Loop throught the array of folders setting up the monitor for Each
i = 0
For Each strFolder In arrFolders
'Create the event sink
WScript.Echo "setup for folder: " & strFolder & vbLf
strCommand = "Set EventSink" & i & " = WScript.CreateObject" & "(""WbemScripting.SWbemSink"", ""SINK" & i & "_"")"
ExecuteGlobal strCommand
'Setup Notification
strQuery = "SELECT * " _
& "FROM __InstanceOperationEvent " _
& "WITHIN 1 " _
& "WHERE Targetinstance ISA 'CIM_DirectoryContainsFile'" _
& " AND TargetInstance.GroupComponent = " & "'Win32_Directory.Name=""" & strFolder & """'"
strCommand = "objWMIservice.ExecNotificationQueryAsync EventSink" & i & ", strQuery"
ExecuteGlobal strCommand
'Create the OnObjectReady Sub
strCommand = "Sub SINK" & i & "_OnObjectReady(objObject, " & "objAsyncContext)" & vbLf _
& " Wscript.Echo objObject.TargetInstance.PartComponent" & vbLf _
& " SendMail(objObject.TargetInstance.PartComponent)" & vbLf _
& "End Sub"
'WScript.Echo strCommand
ExecuteGlobal strCommand
i = i + 1
Next
WScript.Echo "Waiting for events..."
i = 0
While (True)
Wscript.Sleep(1000)
Wend
Function SendMail(vBody)
Dim oMail : Set oMail = CreateObject("CDO.Message")
'Name or IP of Remote SMTP Server
oMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
oMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "your.smtp.server"
oMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
oMail.Configuration.Fields.Update
oMail.Subject = "Email Watch Info Message"
oMail.From = "alert#yourdomain.net"
oMail.To = "target#yourdomain.net"
oMail.TextBody = vBody
oMail.Send
End Function
Correct the settings in the send mail function and your are fine.
In theory, the VBSendMail DLL should be able to do what you want.

Script stops on protected files such as system files

This code stops after a while due to protected files such as system files, "Permission Denied".
Is there a way to modify the code below so that it can handle such protected files or bypass them?
Set objFS=CreateObject("Scripting.FileSystemObject")
WScript.Echo Chr(34) & "Full Path" &_
Chr(34) & "," & Chr(34) & "File Size" &_
Chr(34) & "," & Chr(34) & "File Date modified" &_
Chr(34) & "," & Chr(34) & "File Date Created" &_
Chr(34) & "," & Chr(34) & "File Date Accessed" & Chr(34)
Set objArgs = WScript.Arguments
strFolder = objArgs(0)
Set objFolder = objFS.GetFolder(strFolder)
Go (objFolder)
Sub Go(objDIR)
If objDIR <> "\System Volume Information" Then
For Each eFolder in objDIR.SubFolders
Go eFolder
Next
End If
For Each strFile In objDIR.Files
WScript.Echo Chr(34) & strFile.Path & Chr(34) & "," &_
Chr(34) & strFile.Size & Chr(34) & "," &_
Chr(34) & strFile.DateLastModified & Chr(34) & "," &_
Chr(34) & strFile.DateCreated & Chr(34) & "," &_
Chr(34) & strFile.DateLastAccessed & Chr(34)
Next
End Sub
Then call it from the command line
like this.
c:\test> cscript //nologo myscript.vbs "c:\" > "C:\test\Output.csv"
I've simplified your code (based upon your duplicate question) and without trying to handle errors I can see a problem: objDIR.SubFolders fails when one of the subfolders (such as \System Volume Information) doesn't have permissions to be viewed! You need to use another method on Folder to enumerate the foldernames, combine them with the existing path and then trap the error .GetFolder may cause when you don't have permissions. (I don't have time to code that solution at the moment.)
Option Explicit
Dim objFS
Dim objArgs
Dim strFolder
Dim objFolder
Set objFS = WScript.CreateObject("Scripting.FileSystemObject")
WScript.StdOut.WriteLine """Full Path"",""File Size""," & _
"""File Date modified"",""File Date Created""," & _
"""File Date Accessed"""
Set objArgs = WScript.Arguments
strFolder = objArgs(0)
Set objFolder = objFS.GetFolder(strFolder)
Go objFolder
Sub Go(objDIR)
Dim strFile
On Error Resume Next
For Each eFolder in objDIR.SubFolders
Go eFolder
Next
For Each strFile In objDIR.Files
WScript.StdOut.WriteLine """" & strFile.Path & """,""" & _
strFile.Size & """,""" & _
strFile.DateLastModified & """,""" & _
strFile.DateCreated & """,""" & _
strFile.DateLastAccessed & """"
Next
End Sub
VBScript allows error trapping, though not as gracefully as VBA. Try the script below.
On Error Resume Next
'[ ... code ... ]
Dim test_result, divisor
divisor = 1 '' No error
'divisor = 0 '' raise error #11
'divisor = "zero" '' raise a different error
test_result = 2/divisor
If Err.Number = 11 then ''This line must appear at the point error is raised
MsgBox "Handled Error: " & Err.Description
ElseIf Err.Number > 0 then
MsgBox "Error: " & Err.Number & " " & Err.Description
Err.Clear ''if you wanted to proceed clean from here
End If
MsgBox "Result: " & test_result
ensure the process has permissions. see
You can ignore script errors in VBScript by adding
On Error Resume Next
before the part of the code where you want to ignore errors.
The statement to restore the default behavior is
On Error GoTo 0
And just a remark: Method calls in VB and VBScript don't use parenthesis if they appear as a single statement. So the line Go (objFolder) should be replaced by Go objFolder.