Author Topic: OAuth and what then?  (Read 849 times)

bwill

  • Newbie
  • *
  • Posts: 8
  • Karma: +0/-0
    • View Profile
OAuth and what then?
« on: March 17, 2018, 05:28:42 AM »
Hi,

I've successfully connected with OAuth to Gmail. But what comes afterwards?

I understand where I've got the access token and I'm supposed to save it.

Code: [Select]
Dim sbJson As New Chilkat.StringBuilder
sbJson.Append(oauth2.AccessTokenResponse)
sbJson.WriteFile("qa_data/tokens/_gmailSmtp.json","utf-8",False)

The next time I need to log in I would guess that I need this somewhere. But where? How do I use this instead of trying to log in again? Something like

Code: [Select]
if got access token then
 load access token from preferences into oauth
else
  do the connect stuff
end if

But even when being logging in getting the mailboxes fails. The code below:

Code: [Select]
CkoMailboxTask = CkoImap.ListMailboxesAsync("", "*")
call CkoMailboxTask.Run
While CkoMailboxTask.Finished <> True
  if globals.StopArchiving then
    if not CkoMailboxTask.Cancel then app.SleepCurrentThread(10)
    Return Nil
  end if
  CkoMailboxTask.SleepMs(25)
wend

if not CkoMailboxTask.TaskSuccess then
  globals.theErrorLog.LogItem(CurrentMethodName + " task didn't finish")
  Return Nil
end if

dim CkoMailboxes as new Chilkat.Mailboxes
if not CkoMailboxes.LoadTaskResult(CkoMailboxTask) then
  globals.theErrorLog.LogItem(CurrentMethodName + " wasn't able to load result from task") '<------ no mailboxes
  Retur
n Nil
end if

fails when loading the result of the task. A simple

dim theM as Chilkat.Mailboxes = CkoImap.ListMailboxes("", "*")

also doesn't work. Do I need to connect the Oauth to Chilkat.Imap?

Xojo 2017r3, 64bit, macOS 10.11., Chilkat plugin from feb 2018.

Mit freundlichen Grüßen/Regards

Beatrix Willius

http://www.mothsoftware.com
Mail Archiver X: The email archiving solution for professionals


bwill

  • Newbie
  • *
  • Posts: 8
  • Karma: +0/-0
    • View Profile
Re: OAuth and what then?
« Reply #1 on: March 17, 2018, 05:52:06 AM »
I've overlooked the example https://www.example-code.com/xojo/gmail_smtp_send_using_oauth2_token.asp . Which is for sending mail. Here is the most important part:

Code: [Select]
mailman.OAuth2AccessToken = accessToken
However, Chilkat.Imap doesn't have this mailman.OAuth2AccessToken property. What am I missing?

Mit freundlichen Grüßen/Regards

Beatrix Willius

http://www.mothsoftware.com
Mail Archiver X: The email archiving solution for professionals


bwill

  • Newbie
  • *
  • Posts: 8
  • Karma: +0/-0
    • View Profile
Re: OAuth and what then?
« Reply #3 on: March 19, 2018, 06:31:41 AM »
Thanks!

Mit freundlichen Grüßen/Regards

Beatrix Willius

http://www.mothsoftware.com
Mail Archiver X: The email archiving solution for professionals

bwill

  • Newbie
  • *
  • Posts: 8
  • Karma: +0/-0
    • View Profile
Re: OAuth and what then?
« Reply #4 on: March 19, 2018, 07:28:37 AM »
Still no luck with the code:

Code: [Select]
dim Token as String = setTokenGmail '<-- set the token, this works fine

CkoImap = new Chilkat.Imap
CkoImap.Ssl = True
CkoImap.Port = 993

dim success as Boolean = CkoImap.Connect(ServerName)
if not success then
  System.DebugLog(CkoImap.LastErrorText)
  Return "loginfailed"
end if

CkoImap.AuthMethod = "XOAUTH2"

'Login, Use the email address of the GMail account that granted authorization.
success = CkoImap.Login(UserName + "@gmail.com", Token)
If (success <> True) Then '<----- success is false here
  System.DebugLog(CkoImap.LastErrorText)
Else
  System.DebugLog("Successfully authenticate with GMail IMAP using XOAUTH2!")
End If

Last response is:
+ eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ==
aaab NO [AUTHENTICATIONFAILED] Invalid credentials (Failure)

I tried with 2 different accounts. What am I doing wrong?

Mit freundlichen Grüßen/Regards

Beatrix Willius

http://www.mothsoftware.com
Mail Archiver X: The email archiving solution for professionals

Chilkat

  • Administrator
  • Full Member
  • *****
  • Posts: 103
  • Karma: +6/-0
    • View Profile
Re: OAuth and what then?
« Reply #5 on: March 19, 2018, 04:26:27 PM »
When you use Chilkat.OAuth2 to get the token, and the browser pops up to allow the GMail account owner to authorize the access, make sure the GMail account authorizing the access is the same as the email address passed to CkoImap.Login.

bwill

  • Newbie
  • *
  • Posts: 8
  • Karma: +0/-0
    • View Profile
Re: OAuth and what then?
« Reply #6 on: March 23, 2018, 03:10:04 AM »
Hi,

yes, I made sure that the account names match. And no dice, I still can't get this to work.

Stupid questions:
- I need to supply the full token with the json including the brackets?
- Is there a chance that the encoding matters or the EOLs or an empty space at the end?

Mit freundlichen Grüßen/Regards

Beatrix Willius

http://www.mothsoftware.com
Mail Archiver X: The email archiving solution for professionals