Archived Forum Post

Index of archived forum posts

Question:

Using my own OAuth2 token with mailman?

Jul 20 '17 at 10:09

I have looked at the documentation about GMail and I quote:

GMail: If sending via smtp.gmail.com, then send with OAuth2 authentication if possible. Otherwise you will need to change your GMail account settings to allow for sending by less secure apps. See the links below.

I have reviewed the sample code that uses your HTTP library for the task in hand. However, I am already having to fund the Email library and at the moment it is beyond my circumstances to purchase an additional library.

Now, I have already written my own VB.NET utility to communicate with a GMail Calendar and I do the authentication like this:

Private Function DoAuthentication(ByRef rStrToken As String, ByRef rParameters As OAuth2Parameters) As Boolean
    Dim credential As UserCredential
    Dim Secrets = New ClientSecrets() With {
        .ClientId = m_strClientID,
        .ClientSecret = m_strClientSecret
    }
    'm_Scopes.Add(CalendarService.Scope.Calendar)
    m_Scopes.Add("https://www.googleapis.com/auth/calendar https://www.google.com/m8/feeds/")

    Try
        credential = GoogleWebAuthorizationBroker.AuthorizeAsync(Secrets, m_Scopes,
                                                                 "user", CancellationToken.None,
                                                                 New FileDataStore("PublicTalkSoftware.Calendar.Application")).Result()

        ' Create the calendar service using an initializer instance
        Dim initializer As New BaseClientService.Initializer() With {
            .HttpClientInitializer = credential,
            .ApplicationName = "GoogleCalIF"
        }
        m_Service = New CalendarService(initializer)

        rStrToken = credential.Token.AccessToken.ToString()
        rParameters.AccessToken = credential.Token.AccessToken
        rParameters.RefreshToken = credential.Token.RefreshToken
    Catch ex As Exception
        ' We encountered some kind of problem, perhaps they have not yet authenticated?
        ' Can we isolate that as the exception?

        Return False
    End Try

    Return True
End Function

So what I am asking is if they is a similar way that I can do the authentication bit myself so that I can just pass the token needed into the mailman object?

Thanks.

Update

From what I can tell, I can use my same utility that I developed. I jus have to make sure it has requested to use the email API. Then, I follow the same code as above, but using the right scope.

Finally, I should be able to return the token string and just pass that as a parameter to your OAuth2 method.

Tha sound right?


Answer

I think I have sorted this one out. Things I needed to do:

  1. Update the Google App I had developed to have access to the Gmail API.
  2. Revoke access to my current Google App.
  3. Re-authenticate again so that I was granted the access to the GMail account and make a note of the token.

I use that token with your Email method and it all works. :)