0

几年来,我们通过简单地为 Provisioning API 生成一段 XML 代码,然后使用 VBScript 进行 POST 来创建和管理我们的 GoogleApps 帐户。现在 GoogleApps 要求我们迁移到新的管理 SDK,我不明白我们如何,甚至是否可以对新系统做类似的事情。

这是我们用来首先获取身份验证令牌的代码示例:

' 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

以下是我们随后用于创建帐户的代码示例:

  ' 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

这可能很明显,但我有 Windows 背景,而不是 Linux;我已经完成了脚本,但不是真正的编程。(而且我根本没有做 Java 和/或其他 Web 编程的经验。)

谢谢你的帮助!!

4

1 回答 1

0

使用 Admin SDK 的步骤非常相似。首先,您将获得身份验证,现在 google 使用 Oauth 2,您可以在此处找到有关该https://developers.google.com/accounts/docs/OAuth2的文档, 您可以测试 Oauth 的工作原理:https://developers.google。 com/oauthplayground/

通过身份验证后,您现在可以调用 Directory API 来创建新用户。这是与插入方法相关的文档https://developers.google.com/admin-sdk/directory/v1/reference/users/insert

正如您在文档中看到的,您将发送相同的参数(名称、密码等),但现在它不会被格式化为 xml,而是这些参数将被格式化为 json(这里有一些关于 json 的信息格式:http ://www.w3schools.com/json/ )

我知道信息量很大,希望对你有帮助。

于 2015-01-21T18:59:01.343 回答