Jump to content

[UDF] Gmail API - Emails automation with AutoIt!


Ascer
 Share

Recommended Posts

  • Developers

@tonycstechapps or should I say @tonycst or @tonycst2 or ....  whatever .... you are banned.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

This process explained in https://developers.google.com/gmail/api/guides/uploads

UDF is not specific on what type of upload user is requesting. There are 3 types one of which is  media.

Changing type of email from

$oHttp.Open("POST", "https://www.googleapis.com/gmail/v1/users/" & _URIEncode($sEmailAdress) & "/messages/send", False)

to

$oHttp.Open("POST", "https://www.googleapis.com/gmail/v1/users/" & _URIEncode($sEmailAdress) & "/messages/send?uploadType=media", False)

helps but not 100%. My personal computer has no problem sending pictures while Work computer (with all the antivirus, firewalls and other garbage)  still randomly misses one or another in no particular order.

Does anybody have a solution ?

Thanks  in advance.

 

If $Evolution=True Then
    Why ($Human worse then $Animal) ;?
EndIf

Link to comment
Share on other sites

  • 1 year later...
On 9/11/2019 at 4:06 AM, antalgu said:

Hello, does anyone know how to get the info returned from the function gmailUsersMessagesList

I was just trying it out and got that there was no error in the execution of the function, but i wasn't able to retrieve the information i wanted afterwards. It's strange because I thought the array returned from the function was 2D, but when asked for any on the elements it contained i got this error (2nd image), and if i treated it as if it was only 1D the MsgBox text would just appear to be blank.

132.thumb.PNG.aa1cbf4e1b806fd0e3e743a7327673e4.PNG1342.thumb.PNG.b9f284cfaa9828c3290fce1647414ba7.PNG

SOLUTION FOUND, SEE BELOW!!

I have the exact same problem:

Local $aRet = gmailUsersMessagesList($sYourGmailAdress, $aToken, "Bearer", False, 'IMPORTANT')

Result is an array, containing these records - _ArrayDisplay($aRet):

0001.png.1eba23ff7cf806afea4efea231bede96.png

Array (2D array with message and thread IDs)

0 = no other pages

18 = 18 messages with label IMPORTANT

Then I continue with _ArrayDisplay($aRet[0]) and I get 18 records, all arrays

0002.png.5ec3b942a86f4f741d3a21d07ee31aa2.png

But, if I want to display some records, I get error for any of those two commands:

_ArrayDisplay($aRet[0][0])
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $aRet = ' & $aRet[0][0] & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console


: ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
_ArrayDisplay($aRet[0][0])
_ArrayDisplay(^ ERROR

At the same time, UBound($aRet[0]) is 18, which  is correct, there are 18 messages with label IMPORTANT.

And all those 18 records are arrays, right ???

SOLUTION :

_ArrayDisplay(($aRet[0])[0]) - and I get a simple 2 records array, with message and thread IDs:

0003.png.e2eccae89b0279706328a15486f33cc7.png

And in order to get each record Message Id - (($aRet[0])[$i])[0], Thread Id - (($aRet[0])[$i])[1]

 

Edited by queensoft
Link to comment
Share on other sites

53 minutes ago, queensoft said:

At the same time, UBound($aRet[0]) is 18, which  is correct, there are 18 messages with label IMPORTANT.
And all those 18 records are arrays, right ???

You can add arrays in arrays like Matryoshka dolls :D

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

Yes, I know, I know both things.

But so far I have only used 1D and 2D arrays, with simple (integer / string) values.

Never array inside array. And I didn't know how to access the values of the "smaller doll" inside the "big doll" !! :)

But I have found the solution and edited my original post.

Thanks.

Edited by queensoft
Link to comment
Share on other sites

SOLUTION FOUND, SEE BELOW!!

Now I have another problem - cannot read email message:

$ret01 = gmailUsersMessagesGet((($aRet[0])[$i])[1], $aToken, "Bearer", "full")

Everything up to that point is working perfectly: oauth, access token, refresh token, send email, gmailUsersLabelsList, get Thread ID

I have added multiple scopes to oauth/project, as per this: https://developers.google.com/gmail/api/reference/rest/v1/users.messages/get

0005.png.b689ff6ca60d5afe7d765d0b55bcef81.png

I have renewed and refreshed authorization (oAuth2GetAuthorizationCode, oAuth2GetAccessToken, oAuth2RefreshAccessToken).

Error is this:

+++ Line: 479, Func: gmailUsersMessagesGet() -> Error durning send request to Google. $oHttp.Status = 401
$oHttp.ResponseText = {
  "error": {
    "code": 401,
    "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "errors": [
      {
        "message": "Login Required.",
        "domain": "global",
        "reason": "required",
        "location": "Authorization",
        "locationType": "header"
      }
    ],
    "status": "UNAUTHENTICATED",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.ErrorInfo",
        "reason": "CREDENTIALS_MISSING",
        "domain": "googleapis.com",
        "metadata": {
          "method": "caribou.api.proto.MailboxService.GetMessage",
          "service": "gmail.googleapis.com"
        }
      }
    ]
  }
}

Again, all other operations are working perfectly: send email, get labels list, get list of messages with specific label (gmailUsersMessagesList)

SOLUTION :

I was missing a parameter - $sYourGmailAdress:

$ret01 = gmailUsersMessagesGet((($aRet[0])[$i])[1], $sYourGmailAdress, $aToken, "Bearer", "full")

 

Edited by queensoft
Link to comment
Share on other sites

This is definitely a bug.

Not in the actual Gmail/Oauth code, but in the JSON code.

gmailUsersLabelsList function output for custom labels:

{
      "id": "Label_1",
      "name": "[Imap]/Sent",
      "type": "user"
    },
    {
      "id": "Label_14",
      "name": "[Imap]/Trash",
      "type": "user"
    },
    {
      "id": "Label_6866461006926381418",
      "name": "testxxx",
      "type": "user"
    }

Func _JsonValue from oAuth.au3 will stop at first ] and will not read some of the labels.

Can anyone please fix the RegEx to read all labels?

In the mean time, I think I will just use _StringBetween to extract all Label IDs.

Thanks

Link to comment
Share on other sites

  • 1 year later...

Anyone try sending an attachment using this UDF?
My script does send the pdf file but it comes out damaged on the receiving end. Maybe some kind of encoding has to be done to the file. If so does anyone know how to correct this?
 

$pdfFilePath = @ScriptDir & "\SampleEmail.pdf"
gmailUsersMessagesSend("tet@test.org", _ 
                                             "My Document Scan", _ 
                                             "Please see attached document scanned. This email does not accept replies back.", _ 
                                             "noreply@mysite.org", _ 
                                             $sAccessToken, _ 
                                             "Bearer", _ 
                                             Default, _ 
                                             $pdfFilePath)

 

Edited by NassauSky
Link to comment
Share on other sites

It depends what do you understand by less secure application. It's true that you have to turn off that setting but it doesn't mean you are using plain http and insecure connections but a less secure access to account according to Google security standards.

Link to comment
Share on other sites

8 minutes ago, NassauSky said:

@Andreik it seems that using that UDF probably needs less secure app access and I didn't want to do that.

@queensoft maybe we'll get some someone experienced with this taking a look.

I'm using the script with Oauth authorization (client ID, secret code, refresh code....)

I don't think I have "less secure app" enabled in Google for the script / app.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...