Re: Example signup form?


sierragamers@...
 

Greetings Alison,

My application is probably very different. I'm using Dot Net from the server side. That said, I'm putting some code at the end of this message. 

The thing I struggled with was that I couldn't get dot net to post to Groups.IO's servers. I had a couple of challenges that were easily overcome, but only after I discovered what they were. Specifically, the authentication is two-fold; the CSRF .. AND, the cookie that is obtained when you log in. Being server side, I had to create an empty cookie container, collect a cookie on the login and then give it back later for the DirectAdd call. My second challenge took a day to solve. Apparently Groups.IO's servers want TSL1 or 2, and dot net was trying to communicate with TLS 3. All I knew was the call was inexplicable timing out. Argh.

Anyway .. there's no greater tip I can give you than to download Postman. https://www.getpostman.com/downloads/ I never would have figured it out otherwise. You really need to make the calls from Postman first to see the behavior. 

Anyway ... here's some excerpts from my code:

This is the method that does the login .... (It's not likely to be much help)

 
    Function loginToGroupsIo(ByVal email As String, ByVal password As String) As GioInfo
        Dim paramsInBody As Specialized.NameValueCollection = New Specialized.NameValueCollection
        Dim paramsOnUrl As Specialized.NameValueCollection = New Specialized.NameValueCollection
 
        paramsOnUrl.Add("email", email)
        paramsOnUrl.Add("password", password)
 
        Dim uri As String = "https://groups.io/api/v1/login"
 
        Dim cookieContainer As Net.CookieContainer = New Net.CookieContainer
        Dim rtn_generic As WpToolsV2.Rtn_generic = wp.GioPostHtmlv2(uri, paramsInBody, paramsOnUrl, "", cookieContainer)
        If (rtn_generic.success) Then
        Else
            reportF.Append(rtn_generic.reason)
            Return Nothing
        End If
 
        Dim js As JavaScriptSerializer = New JavaScriptSerializer
 
        Dim loginObject As Object = js.DeserializeObject(rtn_generic.reason)
        Dim userObject As Object = loginObject("user")
 
        Dim gi As GioInfo = New GioInfo
        gi.loggedInUserId = userObject("id")
        gi.csrf = userObject("csrf_token")
        gi.cookieContainer = cookieContainer
        gi.userObject = userObject
        gi.loginObject = loginObject
        Return gi
    End Function

And the code for the DirectAdd:

 
    Sub addViaDirectAdd(ByRef gi As GioInfo,
                         ByRef listOfEmails As List(Of String))
 
        Dim rtn_generic As WpToolsV2.Rtn_generic = Nothing
        Dim js As JavaScriptSerializer = New JavaScriptSerializer
        Dim paramsInBody As Specialized.NameValueCollection = New Specialized.NameValueCollection
        Dim paramsOnUrl As Specialized.NameValueCollection = New Specialized.NameValueCollection
 
        paramsInBody.Add("group_id", gi.groupid)
        paramsInBody.Add("group_name", gi.group_name)
        Dim newLine As String = vbCrLf
        Dim f As FastString = New FastString
        For Each email As String In listOfEmails
            f.Append(email & newLine)
        Next
 
        paramsInBody.Add("emails", f.concat)
        paramsInBody.Add("csrf", gi.csrf)
 
        Dim uri As String = "https://groups.io/api/v1/directadd"
 
        rtn_generic = wp.GioPostHtmlv2(uri, paramsInBody, paramsOnUrl, "", gi.cookieContainer)
 
 
        If (rtn_generic.success) Then
        Else
            reportF.Append(rtn_generic.reason)
            Return
        End If
 
    End Sub

Something worth noting ... 

Login is simple -- the parameters go on the URL. All of the other calls use the parameters inside the body of the email.

Here is the source for my actual method that does the posting. It won't help you .. but .. someone down the line might find it useful...

There is a lot of useless code within the method. I struggled to get it to work, so I was logging everything after every call so I could see what was happening. And, the code is a bit messy because "posting comes in various flavors" and I tried them all. At one point I was posting as a multi-part form, etc. 

Anyway .. I hope all this helps. Good luck!

 
    Public Function GioPostHtmlv2(
ByVal uri As String,
ByVal parametersInBody As Specialized.NameValueCollection,
ByVal parametersOnUrl As Specialized.NameValueCollection,
ByVal accessToken As String,
ByRef cookieContainer As CookieContainer) As Rtn_generic
        Dim f As FastString = New FastString
        f.Append("URI: " & uri & vbCrLf)
 
 
        ' make a parameter list
 
        Dim parametersf As FastString = New FastString
        Dim allKeys() As String = parametersOnUrl.AllKeys
 
        Dim firstTime As Boolean = True
        For Each key As String In allKeys
            If (firstTime) Then
                firstTime = False
                parametersf.Append("?")
            Else
                parametersf.Append("&")
            End If
            parametersf.Append(key & "=")
            parametersf.Append(HttpContext.Current.Server.UrlEncode(parametersOnUrl(key)))
        Next
 
        Dim postData As String = parametersf.concat
        If (postData Is Nothing) Then
            postData = ""
        End If
        uri &= postData
 
        f.Append("URI with Parms: " & uri & vbCrLf)
 
        System.Net.ServicePointManager.SecurityProtocol =
                        SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls12
 
        Dim rtn As Rtn_generic = New Rtn_generic
 
 
        Dim request As Net.HttpWebRequest = Net.WebRequest.Create(uri)
 
        request.ContentType = "application/x-www-form-urlencoded"
        f.Append(request.ContentType & vbCrLf)
 
        request.Method = "POST"
        request.KeepAlive = True
        request.Credentials = Net.CredentialCache.DefaultCredentials
        request.PreAuthenticate = True
        request.Accept = "application/json"
        If (accessToken = "") Then
        Else
            request.Headers.Add("Authorization", "Bearer " & accessToken)
        End If
 
        request.CookieContainer = cookieContainer
 
        Using requestStream As IO.Stream = request.GetRequestStream()
            parametersf.Reset()
 
            allKeys = parametersInBody.AllKeys
 
            firstTime = True
            For Each key As String In allKeys
                If (firstTime) Then
                    firstTime = False
                    parametersf.Append("")
                Else
                    parametersf.Append("&")
                End If
                parametersf.Append(key & "=")
                parametersf.Append(HttpContext.Current.Server.UrlEncode(parametersInBody(key)))
            Next
 
            postData = parametersf.concat
            If (postData Is Nothing) Then
                postData = ""
            End If
            f.Append("Parameters in the body:" & vbCrLf)
            f.Append(postData & vbCrLf)
            Dim parameterBytes As Byte() = Text.Encoding.UTF8.GetBytes(postData)
 
            requestStream.Write(parameterBytes, 0, parameterBytes.Count)
 
        End Using
 
        Dim response As Net.WebResponse = Nothing
 
        Try
 
            response = request.GetResponse()
 
            Using responseStream As IO.Stream = response.GetResponseStream()
 
                Using responseReader As New IO.StreamReader(responseStream)
 
                    Dim responseText = responseReader.ReadToEnd()
                    Diagnostics.Debug.Write(responseText)
                    rtn.success = True
                    rtn.reason = responseText
                End Using
 
            End Using
 
        Catch exception As Net.WebException
 
            response = exception.Response
 
            If (response IsNot Nothing) Then
 
                Using reader As New IO.StreamReader(response.GetResponseStream())
 
                    Dim responseText = reader.ReadToEnd()
                    Diagnostics.Debug.Write(responseText)
                    rtn.success = False
                    rtn.reason = responseText
                End Using
 
                response.Close()
 
            End If
 
        Finally
 
            request = Nothing
 
        End Try
 
        rtn.log = f.concat
 
        Return rtn
    End Function


Join api@beta.groups.io to automatically receive all group messages.