#include ;==================================================================================================================================================================================== ; Date: 2018-02-12, 11:46 ; ; Bug Fixs: 2018-02-17, 7:31 -> Fixed problems with adding items to array and minor bugs. ; ; Description: UDF for using Gmail API interface. This UDF requires oAuth.au3 and Gmail account. ; ; Function(s): ; gmailUsersGetProfile() -> Information about your account. ; gmailUsersLabelsList() -> Get all available labels ids. ex. "INBOX", "UNREAD" ; gmailUsersLabelsGet() -> Get information about specific label id. ; gmailUsersMessagesBatchDelete() -> Delete many messages emails by id. ; gmailUsersMessagesBatchModify() -> Set status for many messages ex. "INBOX", "UNREAD" ; gmailUsersMessagesDelete() -> Totaly delete email from ur account. ; gmailUsersMessagesGet() -> Get all information about specific email. ; gmailUsersMessagesList() -> Get list of last ~100 emails. ; gmailUsersMessagesModify() -> Modify single message. ; gmailUsersMessagesTrash() -> Put email in trash. ; gmailUsersMessagesUntrash() -> Restore email from trash. ; gmailUsersMessagesSend() -> Send email to single or group recipients. ; gmailUsersMessagesAttachmentsGet() -> Download attachment by id. ; ; Author(s): Ascer ;==================================================================================================================================================================================== #Region 1.0, Gmail Profile. ;==================================================================================================================================================================================== ; Function: gmailUsersGetProfile($sEmailAdress, $sAccessToken [, $sTokenType = "Bearer" [, $sFields = Default]]) ; ; Description: Get your profile informations. ; ; Parameter(s): $sEmailAdress - string | Your gmail account email adress. ; $sAccessToken - string | Your "access_token" received calling oAuth2GetAccessToken()[0] or oAuth2RefreshAccessToken()[0] ; $sTokenType - string | (Default = "Bearer") Your "token_type" received calling oAuth2GetAccessToken()[2] or oAuth2RefreshAccessToken()[3] ; $sFields - string | (Default = not used) Return all informations or one get one, two or three of this: "emailAddress,historyId,messagesTotal,threadsTotal" ; ; Return Value(s): On Success Set Error to 0 and Returns 1D array containing: ; [0] - string | Email adress ; [1] - string | Messages Total ; [2] - string | Threads Total ; [3] - string | History Id ; On Failure Set Error to: ; 1 - Failed to create object in $oHttp ("winhttp.winhttprequest.5.1") ; 2 - Invalid parameter in calling HttpRequest. Received status <> 200. ; Author (s): Ascer ;==================================================================================================================================================================================== Func gmailUsersGetProfile($sEmailAdress, $sAccessToken, $sTokenType = "Bearer", $sFields = Default) Local $oHttp = ObjCreate("winhttp.winhttprequest.5.1") If Not IsObj($oHttp) Then Return except("gmailUsersGetProfile()", 'Error durning create object "winhttp.winhttprequest.5.1". Something wrong with your Microsoft lib.', 1, 1) Local $sRequest = "https://www.googleapis.com/gmail/v1/users/" & _URIEncode($sEmailAdress) & "/profile" If $sFields <> Default Then $sRequest &= "?fields=" & $sFields $oHttp.Open("GET", $sRequest , False) $oHTTP.SetRequestHeader("Authorization", $sTokenType & " " & $sAccessToken) $oHTTP.SetRequestHeader("Content-Type", "application/json") $oHttp.Send() Local $iStatus = $oHttp.Status Local $sOutput = $oHttp.ResponseText If $iStatus <> 200 Then Return except("gmailUsersGetProfile()", "Error durning send request to Google. $oHttp.Status = " & $iStatus & @CRLF & "$oHttp.ResponseText = " & $sOutput, 2, $iStatus) Local $aEmailAdress = _JsonValue($sOutput, '"emailAddress": "', '"') Local $aMessagesTotal = _JsonValue($sOutput, '"messagesTotal": ', ',') Local $aThreadsTotal = _JsonValue($sOutput, '"threadsTotal": ', ',') Local $aHistoryId = _JsonValue($sOutput, '"historyId": "', '"') Local $bQuestion Local $aTable $bQuestion = IsArray($aEmailAdress) ? _TableInsert($aTable, $aEmailAdress[0]) : _TableInsert($aTable, "") $bQuestion = IsArray($aMessagesTotal) ? _TableInsert($aTable, $aMessagesTotal[0]) : _TableInsert($aTable, "") $bQuestion = IsArray($aThreadsTotal) ? _TableInsert($aTable, $aThreadsTotal[0]) : _TableInsert($aTable, "") $bQuestion = IsArray($aHistoryId) ? _TableInsert($aTable, $aHistoryId[0]) : _TableInsert($aTable, "") Return SetError(0, 0, $aTable) EndFunc #EndRegion #Region 1.1, Gmail Labels. ;==================================================================================================================================================================================== ; Function: gmailUsersLabelsList($sEmailAdress, $sAccessToken [, $sTokenType = "Bearer"]) ; ; Description: Get ids of all Labels in Gamil Mailbox. ; ; Parameter(s): $sEmailAdress - string | Your gmail account email adress. ; $sAccessToken - string | Your "access_token" received calling oAuth2GetAccessToken()[0] or oAuth2RefreshAccessToken()[0] ; $sTokenType - string | (Default = "Bearer") Your "token_type" received calling oAuth2GetAccessToken()[2] or oAuth2RefreshAccessToken()[3] ; ; Return Value(s): On Success Set Error to 0 and Returns 1D array containing mailbox ids. ; On Failure Set Error to: ; 1 - Failed to create object in $oHttp ("winhttp.winhttprequest.5.1") ; 2 - Invalid parameter in calling HttpRequest. Received status <> 200. ; 3, 4, 5 - Failed to get values from Google respond. ; Author (s): Ascer ;==================================================================================================================================================================================== Func gmailUsersLabelsList($sEmailAdress, $sAccessToken, $sTokenType = "Bearer") Local $oHttp = ObjCreate("winhttp.winhttprequest.5.1") If Not IsObj($oHttp) Then Return except("gmailUsersLabelsList()", 'Error durning create object "winhttp.winhttprequest.5.1". Something wrong with your Microsoft lib.', 1, 1) $oHttp.Open("GET", "https://www.googleapis.com/gmail/v1/users/" & _URIEncode($sEmailAdress) & "/labels" , False) $oHTTP.SetRequestHeader("Authorization", $sTokenType & " " & $sAccessToken) $oHTTP.SetRequestHeader("Content-Type", "application/json") $oHttp.Send() Local $iStatus = $oHttp.Status Local $sOutput = $oHttp.ResponseText If $iStatus <> 200 Then Return except("gmailUsersLabelsList()", "Error durning send request to Google. $oHttp.Status = " & $iStatus & @CRLF & "$oHttp.ResponseText = " & $sOutput, 2, $iStatus) Local $sJson = _JsonValue($sOutput, "[", "]") If Not IsArray($sJson) Then Return except("gmailUsersLabelsList()", "Error durning reading $oHttp.ResponseText. Google must changed *json respond for this request." & @CRLF & "$oHttp.ResponseText = " & $sOutput, 3, 3) Local $aJsonArray = _JsonValue($sJson[0], "{", "}") If Not IsArray($aJsonArray) Then Return except("gmailUsersLabelsList()", "Error durning reading $oHttp.ResponseText. Google must changed *json respond for this request." & @CRLF & "$oHttp.ResponseText = " & $sOutput, 4, 4) Local $aTable For $sJsonElement In $aJsonArray Local $aJsonId = _JsonValue($sJsonElement, '"id": "', '"') If IsArray($aJsonId) Then _TableInsert($aTable, $aJsonId[0]) EndIf Next If Not IsArray($aTable) Then Return except("gmailUsersLabelsList()", "Error durning reading $oHttp.ResponseText. Google must changed *json respond for this request. Ids of Labels not found!" & @CRLF & "$oHttp.ResponseText = " & $sOutput, 5, 5) Return SetError(0, 0, $aTable) EndFunc ;==================================================================================================================================================================================== ; Function: gmailUsersLabelsGet($sLabelId, $sEmailAdress, $sAccessToken [, $sTokenType = "Bearer" [, $sFields = Default]]) ; ; Description: Get informations about specific LabelId in your mailbox. ; ; Parameter(s): $sLabelId - string | "label_id" to check. Call doGmailApiGetLabelsList() to receive all available labels. Example of $sLabelId is: "INBOX", "UNREADED" ; $sEmailAdress - string | Your gmail account email adress. ; $sAccessToken - string | Your "access_token" received calling oAuth2GetAccessToken()[0] or oAuth2RefreshAccessToken()[0] ; $sTokenType - string | (Default = "Bearer") Your "token_type" received calling oAuth2GetAccessToken()[2] or oAuth2RefreshAccessToken()[3] ; $sFields - string | (Default = not used) Return all information or specific: ; "color,id,labelListVisibility,messageListVisibility,messagesTotal,messagesUnread,name,threadsTotal,threadsUnread,type" ; ; Return Value(s): On Success Set Error to 0. and Returns 1D array containing: ; [0] - string | Label Id ; [1] - string | Label Name ; [2] - string | Label Message List Visibility ; [3] - string | Label Label List Visibility ; [4] - string | Label Type ; [5] - integer | Label Messages Total ; [6] - integer | Label Messages Unreaded ; [7] - integer | Label Threads Total ; [8] - integer | Label Threads Unreaded ; On Failure Set Error to: ; 1 - Failed to create object in $oHttp ("winhttp.winhttprequest.5.1") ; 2 - Invalid parameter in calling HttpRequest. Received status <> 200. ; 3 - Failed to get values from Google respond. ; Author (s): Ascer ;==================================================================================================================================================================================== Func gmailUsersLabelsGet($sLabelId, $sEmailAdress, $sAccessToken, $sTokenType = "Bearer", $sFields = Default) Local $oHttp = ObjCreate("winhttp.winhttprequest.5.1") If Not IsObj($oHttp) Then Return except("gmailUsersLabelsGet()", 'Error durning create object "winhttp.winhttprequest.5.1". Something wrong with your Microsoft lib.', 1, 1) Local $sRequest = "https://www.googleapis.com/gmail/v1/users/" & _URIEncode($sEmailAdress) & "/labels/" & $sLabelId If $sFields <> Default Then $sRequest &= "?labels=" & $sFields $oHttp.Open("GET", $sRequest, False) $oHTTP.SetRequestHeader("Authorization", $sTokenType & " " & $sAccessToken) $oHTTP.SetRequestHeader("Content-Type", "application/json") $oHttp.Send() Local $iStatus = $oHttp.Status Local $sOutput = $oHttp.ResponseText If $iStatus <> 200 Then Return except("gmailUsersLabelsGet()", "Error durning send request to Google. $oHttp.Status = " & $iStatus & @CRLF & "$oHttp.ResponseText = " & $sOutput, 2, $iStatus) Local $sId = _JsonValue($sOutput, '"id": "', '"') If Not IsArray($sId) Then Return except("gmailUsersLabelsGet()", "Error durning reading $oHttp.ResponseText. Google must changed *json respond for this request." & @CRLF & "$oHttp.ResponseText = " & $sOutput, 3, 3) Local $sName = _JsonValue($sOutput, '"name": "', '"') Local $sMessageListVisibility = _JsonValue($sOutput, '"messageListVisibility": "', '"') Local $sLabelListVisibility = _JsonValue($sOutput, '"labelListVisibility": "', '"') Local $sType = _JsonValue($sOutput, '"type": "', '"') Local $iMessagesTotal = _JsonValue($sOutput, '"messagesTotal": ', ",") Local $iMessagesUnreaded = _JsonValue($sOutput, '"messagesUnread": ', ",") Local $iThreadsTotal = _JsonValue($sOutput, '"threadsTotal": ', ",") Local $iThreadsUnreaded = _JsonValue($sOutput, '"threadsUnread": ', ",") Local $bQuestion, $aTable _TableInsert($aTable, $sId) $bQuestion = IsArray($sName) ? _TableInsert($aTable, $sName[0]) : _TableInsert($aTable, "") $bQuestion = IsArray($sMessageListVisibility) ? _TableInsert($aTable, $sMessageListVisibility[0]) : _TableInsert($aTable, "") $bQuestion = IsArray($sLabelListVisibility) ? _TableInsert($aTable, $sLabelListVisibility[0]) : _TableInsert($aTable, "") $bQuestion = IsArray($sType) ? _TableInsert($aTable, $sType[0]) : _TableInsert($aTable, "") $bQuestion = IsArray($iMessagesTotal) ? _TableInsert($aTable, Int($iMessagesTotal[0])) : _TableInsert($aTable, 0) $bQuestion = IsArray($iMessagesUnreaded) ? _TableInsert($aTable, Int($iMessagesUnreaded[0])) : _TableInsert($aTable, 0) $bQuestion = IsArray($iThreadsTotal) ? _TableInsert($aTable, Int($iThreadsTotal[0])) : _TableInsert($aTable, 0) $bQuestion = IsArray($iThreadsUnreaded) ? _TableInsert($aTable, Int($iThreadsUnreaded[0])) : _TableInsert($aTable, 0) Return SetError(0, 0, $aTable) EndFunc #EndRegion #Region 1.2, Gmail Messages. ;==================================================================================================================================================================================== ; Function: gmailUsersMessagesBatchDelete($sMessageId, $sEmailAdress, $sAccessToken [, $sTokenType = "Bearer"]) ; ; Description: Remove email (s) from your Gmail account. Max amount is 1000 per one execute session. ; ; Parameter(s): $sMessageId - string or array | Message id to remove, can be an array with ids like this: ["161705b008ab543b", "161700b000df984d"] ; $sEmailAdress - string | Your gmail account email adress. ; $sAccessToken - string | Your "access_token" received calling oAuth2GetAccessToken()[0] or oAuth2RefreshAccessToken()[0] ; $sTokenType - string | (Default = "Bearer") Your "token_type" received calling oAuth2GetAccessToken()[2] or oAuth2RefreshAccessToken()[3] ; ; Return Value(s): On Success Set Error to 0 and Returns empty string "" ; On Failure Set Error to: ; 1 - Failed to create object in $oHttp ("winhttp.winhttprequest.5.1") ; 2 - Invalid parameter in calling HttpRequest. Received status <> 200. ; Author (s): Ascer ;==================================================================================================================================================================================== Func gmailUsersMessagesBatchDelete($sMessageId, $sEmailAdress, $sAccessToken, $sTokenType = "Bearer") Local $oHttp = ObjCreate("winhttp.winhttprequest.5.1") If Not IsObj($oHttp) Then Return except("gmailUsersMessagesBatchDelete()", 'Error durning create object "winhttp.winhttprequest.5.1". Something wrong with your Microsoft lib.', 1, 1) $oHttp.Open("POST", "https://www.googleapis.com/gmail/v1/users/" & _URIEncode($sEmailAdress) & "/messages/batchDelete", False) $oHTTP.SetRequestHeader("Authorization", $sTokenType & " " & $sAccessToken) $oHTTP.SetRequestHeader("Content-Type", "application/json") Local $sBody = "{" $sBody &= '"ids": [' If IsArray($sMessageId) Then Local $iUbound = UBound($sMessageId) For $sItem = 0 To $iUbound - 1 If $sItem = ($iUbound - 1) Then $sBody &= '"' & $sMessageId[$sItem] & '"' Else $sBody &= '"' & $sMessageId[$sItem] & '", ' EndIf Next Else $sBody &= '"' & $sMessageId & '"' EndIf $sBody &= "]}" $oHttp.Send($sBody) Local $iStatus = $oHttp.Status Local $sOutput = $oHttp.ResponseText If Not $iStatus = 200 Or Not $iStatus = 204 Then Return except("gmailUsersMessagesBatchDelete()", "Error durning send request to Google. $oHttp.Status = " & $iStatus & @CRLF & "$oHttp.ResponseText = " & $sOutput, 2, $iStatus) Return SetError(0, 0, "") EndFunc ;==================================================================================================================================================================================== ; Function: gmailUsersMessagesBatchModify($sMessageId, $sAddLabelIds, $sRemoveLabelIds, $sEmailAdress, $sAccessToken [, $sTokenType = "Bearer"]) ; ; Description: Set email(s) labels (status) example to "UNREAD". Limit is 1000 per on execute session. ; ; Parameter(s): $sMessageId - string or array | Message id to remove, can be an array with ids like this: ["161705b008ab543b", "161700b000df984d"] ; $sAddLabelIds - string or array | Add label id for message, can be an array: ["UNREAD", "INBOX"] More labels available by calling gmailUsersLabelsList ; $sRemoveLabelIds - string or array | Remove label id for messages, when nothing to remove put empty string "" ; $sEmailAdress - string | Your gmail account email adress. ; $sAccessToken - string | Your "access_token" received calling oAuth2GetAccessToken()[0] or oAuth2RefreshAccessToken()[0] ; $sTokenType - string | (Default = "Bearer") Your "token_type" received calling oAuth2GetAccessToken()[2] or oAuth2RefreshAccessToken()[3] ; ; Return Value(s): On Success Set Error to 0 and Returns empty string "" ; On Failure Set Error to: ; 1 - Failed to create object in $oHttp ("winhttp.winhttprequest.5.1") ; 2 - Invalid parameter in calling HttpRequest. Received status <> 200. ; Author (s): Ascer ;==================================================================================================================================================================================== Func gmailUsersMessagesBatchModify($sMessageId, $sAddLabelIds, $sRemoveLabelIds, $sEmailAdress, $sAccessToken, $sTokenType = "Bearer") Local $oHttp = ObjCreate("winhttp.winhttprequest.5.1") If Not IsObj($oHttp) Then Return except("gmailUsersMessagesBatchModify()", 'Error durning create object "winhttp.winhttprequest.5.1". Something wrong with your Microsoft lib.', 1, 1) $oHttp.Open("POST", "https://www.googleapis.com/gmail/v1/users/" & _URIEncode($sEmailAdress) & "/messages/batchModify", False) $oHTTP.SetRequestHeader("Authorization", $sTokenType & " " & $sAccessToken) $oHTTP.SetRequestHeader("Content-Type", "application/json") Local $sBody = "{" $sBody &= '"ids": [' If IsArray($sMessageId) Then Local $iUbound = UBound($sMessageId) For $sItem = 0 To $iUbound - 1 If $sItem = ($iUbound - 1) Then $sBody &= '"' & $sMessageId[$sItem] & '"' Else $sBody &= '"' & $sMessageId[$sItem] & '", ' EndIf Next Else $sBody &= '"' & $sMessageId & '"' EndIf $sBody &= "]," $sBody &= '"addLabelIds": [' If IsArray($sAddLabelIds) Then Local $iUbound = UBound($sAddLabelIds) For $sItem = 0 To $iUbound - 1 If $sItem = ($iUbound - 1) Then $sBody &= '"' & $sAddLabelIds[$sItem] & '"' Else $sBody &= '"' & $sAddLabelIds[$sItem] & '", ' EndIf Next Else If $sAddLabelIds <> "" Then $sBody &= '"' & $sAddLabelIds & '"' EndIf $sBody &= "]," $sBody &= '"removeLabelIds": [' If IsArray($sRemoveLabelIds) Then Local $iUbound = UBound($sRemoveLabelIds) For $sItem = 0 To $iUbound - 1 If $sItem = ($iUbound - 1) Then $sBody &= '"' & $sRemoveLabelIds[$sItem] & '"' Else $sBody &= '"' & $sRemoveLabelIds[$sItem] & '", ' EndIf Next Else If $sRemoveLabelIds <> "" Then $sBody &= '"' & $sRemoveLabelIds & '"' EndIf $sBody &= "]}" $oHttp.Send($sBody) Local $iStatus = $oHttp.Status Local $sOutput = $oHttp.ResponseText If Not $iStatus = 200 Or Not $iStatus = 204 Then Return except("gmailUsersMessagesBatchModify()", "Error durning send request to Google. $oHttp.Status = " & $iStatus & @CRLF & "$oHttp.ResponseText = " & $sOutput, 2, $iStatus) Return SetError(0, 0, "") EndFunc ;==================================================================================================================================================================================== ; Function: gmailUsersMessagesDelete($sMessageId, $sEmailAdress, $sAccessToken [, $sTokenType = "Bearer"]) ; ; Description: Permanently remove message from yout Gmail account. Working only with single email. ; ; Parameter(s): $sMessageId - string | Message id to remove. ; $sEmailAdress - string | Your gmail account email adress. ; $sAccessToken - string | Your "access_token" received calling oAuth2GetAccessToken()[0] or oAuth2RefreshAccessToken()[0] ; $sTokenType - string | (Default = "Bearer") Your "token_type" received calling oAuth2GetAccessToken()[2] or oAuth2RefreshAccessToken()[3] ; ; Return Value(s): On Success Set Error to 0 and Returns empty string "" ; On Failure Set Error to: ; 1 - Failed to create object in $oHttp ("winhttp.winhttprequest.5.1") ; 2 - Invalid parameter in calling HttpRequest. Received status <> 200. ; Author (s): Ascer ;==================================================================================================================================================================================== Func gmailUsersMessagesDelete($sMessageId, $sEmailAdress, $sAccessToken, $sTokenType = "Bearer") Local $oHttp = ObjCreate("winhttp.winhttprequest.5.1") If Not IsObj($oHttp) Then Return except("gmailUsersMessagesDelete()", 'Error durning create object "winhttp.winhttprequest.5.1". Something wrong with your Microsoft lib.', 1, 1) $oHttp.Open("DELETE", "https://www.googleapis.com/gmail/v1/users/" & _URIEncode($sEmailAdress) & "/messages/" & $sMessageId, False) $oHTTP.SetRequestHeader("Authorization", $sTokenType & " " & $sAccessToken) $oHTTP.SetRequestHeader("Content-Type", "application/json") $oHttp.Send() Local $iStatus = $oHttp.Status Local $sOutput = $oHttp.ResponseText If Not $iStatus = 200 Or Not $iStatus = 204 Then Return except("gmailUsersMessagesDelete()", "Error durning send request to Google. $oHttp.Status = " & $iStatus & @CRLF & "$oHttp.ResponseText = " & $sOutput, 2, $iStatus) Return SetError(0, 0, "") EndFunc ;==================================================================================================================================================================================== ; Function: gmailUsersMessagesGet($sMessageId, $sEmailAdress, $sAccessToken [, $sTokenType = "Bearer" [, $sFormat = "full" [, $sMetadataHeaders = Default [, $sFields = Default]]]]) ; ; Description: Get informations about specific message in Gmail account. Content is received with *Json data, you need to get values manually. ; ; Parameter(s): $sMessageId - string | Message id can not be array! ; $sEmailAdress - string | Your gmail account email adress. ; $sAccessToken - string | Your "access_token" received calling oAuth2GetAccessToken()[0] or oAuth2RefreshAccessToken()[0] ; $sTokenType - string | (Default = "Bearer") Your "token_type" received calling oAuth2GetAccessToken()[2] or oAuth2RefreshAccessToken()[3] ; $sFormat - string | (Default = "full") Receive all message informations, included metadataHeaders. Can be also "minimal", "raw", "metadata" ; $sMetadataHeaders - string or array | (Default = not used) Catch specific metadataHeaders. Use only when param $sFormat = "metadata". Example of headers are "From", "Subject", "To". Rest of headers you find in $oHttp.ResponseText when $sFormat = "Full" ; $sFields - string | (Default = not used) output *json data will contains: messages, nextPageToken if available and resultSizeEstimate. You can put here: "messages,nextPageToken,resultSizeEstimate" ; ; Return Value(s): On Success Set Error to 0 and Returns 1D array containing: ; [0] - string | Thread Id. ; [1] - array | Array with all message labels. When no labels array will empty. ; [2] - string | Snippet. ; [3] - string | History Id. ; [4] - string | Internal Date. ; [5] - integer | Size Estimate. ; [6] - string | Body of Email. When param $sFormat = "raw": body format is text ealier converetd from base64url, $sFormat = "full" or "metadata": body format is *json ; On Failure Set Error to: ; 1 - Failed to create object in $oHttp ("winhttp.winhttprequest.5.1") ; 2 - Invalid parameter in calling HttpRequest. Received status <> 200. ; Author (s): Ascer ;==================================================================================================================================================================================== Func gmailUsersMessagesGet($sMessageId, $sEmailAdress, $sAccessToken, $sTokenType = "Bearer", $sFormat = Default, $sMetadataHeaders = Default, $sFields = Default) Local $oHttp = ObjCreate("winhttp.winhttprequest.5.1") If Not IsObj($oHttp) Then Return except("gmailUsersMessagesGet()", 'Error durning create object "winhttp.winhttprequest.5.1". Something wrong with your Microsoft lib.', 1, 1) Local $sRequest = "https://www.googleapis.com/gmail/v1/users/" & _URIEncode($sEmailAdress) & "/messages/" & $sMessageId & "?" If $sFormat <> Default Then $sRequest &= "format=" & $sFormat Else $sRequest &= "format=full" EndIf $sFormat = StringLower($sFormat) If $sFormat = "metadata" Then If IsArray($sMetadataHeaders) Then For $sMeta In $sMetadataHeaders $sRequest &= "&metadataHeaders=" & $sMeta Next Else $sRequest &= "&metadataHeaders=" & $sMetadataHeaders EndIf EndIf If $sFields <> Default Then $sRequest &= "&fields=" & $sFields $oHttp.Open("GET", $sRequest, False) $oHTTP.SetRequestHeader("Authorization", $sTokenType & " " & $sAccessToken) $oHTTP.SetRequestHeader("Content-Type", "application/json") $oHttp.Send() Local $iStatus = $oHttp.Status Local $sOutput = $oHttp.ResponseText If $iStatus <> 200 Then Return except("gmailUsersMessagesGet()", "Error durning send request to Google. $oHttp.Status = " & $iStatus & @CRLF & "$oHttp.ResponseText = " & $sOutput, 2, $iStatus) Local $aThreadId = _JsonValue($sOutput, '"threadId": "', '"') Local $aLabelsIds = _JsonValue($sOutput, '"labelIds": [', "]") Dim $aLabels[0] If IsArray($aLabelsIds) Then $aLabelsIds = StringSplit($aLabelsIds[0], @LF) For $sElement = 1 To UBound($aLabelsIds) - 1 Local $aLabel = _JsonValue($aLabelsIds[$sElement], '"', '"') If IsArray($aLabel) Then _TableInsert($aLabels, $aLabel[0]) Next EndIf Local $aSnippet = _JsonValue($sOutput, '"snippet": "', '"') Local $aHistoryId = _JsonValue($sOutput, '"historyId": "', '"') Local $aInternalDate = _JsonValue($sOutput, '"internalDate": "', '"') Local $aSizeEstimate = _JsonValue($sOutput, '"sizeEstimate": ', "}") Local $aRaw = _JsonValue($sOutput, '"raw": "', '"') Local $aTable, $fQuestion $fQuestion = IsArray($aThreadId) ? _TableInsert($aTable, $aThreadId[0]) : _TableInsert($aTable, "") _TableInsert($aTable, $aLabels) $fQuestion = IsArray($aSnippet) ? _TableInsert($aTable, $aSnippet[0]) : _TableInsert($aTable, "") $fQuestion = IsArray($aHistoryId) ? _TableInsert($aTable, $aHistoryId[0]) : _TableInsert($aTable, "") $fQuestion = IsArray($aInternalDate) ? _TableInsert($aTable, $aInternalDate[0]) : _TableInsert($aTable, "") $fQuestion = IsArray($aSizeEstimate) ? _TableInsert($aTable, Int($aSizeEstimate[0])) : _TableInsert($aTable, 0) $fQuestion = IsArray($aRaw) ? _TableInsert($aTable, base64($aRaw[0], False, True)) : _TableInsert($aTable, $sOutput) Return SetError(0, 0, $aTable) EndFunc ;==================================================================================================================================================================================== ; Function: gmailUsersMessagesList($sEmailAdress, $sAccessToken [, $sTokenType = "Bearer" [, $bIncludeSpamTrash = False [, $sLabelIds = Default [, $iMaxResults = Default [, $sPageToken = Default [, $sQ = Default [, $sFields = Default]]]]]]] ) ; ; Description: Get informations about your messages in inbox (additional trash and spam) ; ; Parameter(s): $sEmailAdress - string | Your gmail account email adress. ; $sAccessToken - string | Your "access_token" received calling oAuth2GetAccessToken()[0] or oAuth2RefreshAccessToken()[0] ; $sTokenType - string | (Default = "Bearer") Your "token_type" received calling oAuth2GetAccessToken()[2] or oAuth2RefreshAccessToken()[3] ; $bIncludeSpamTrash - boolean | (Default = False) dont read massages in Trash ans Spam cathegory. ; $sLabelsIds - string | (Default = not used) return messages from specific label id. List all available labels can be checked by calling: gmailUsersLabelsList() ; $iMaxResults - integer | (Default = not used) return selected amount of messages. When amount > 100 then return 100 and nextPageToken to get more, else return all messages. ; $sPageToken - string | (Default = not used) return first messages with last received messages date. Enter pageToken to get next messages. ; $sQ - string | (Default = not used) "from:someuser@example.com rfc822msgid: is:unread". Parameter cannot be used when accessing the api using the gmail.metadata scope. ; $sFields - string | (Default = not used) output *json data will contains: messages, nextPageToken if available and resultSizeEstimate. You can put here: "messages,nextPageToken,resultSizeEstimate" ; ; Return Value(s): On Success Set Error to 0 and Returns 2D array containing: ; [0] - array | An array with recived messages, when no message received then array is empty. ; [0] - string | Message Id ; [1] - string | Thread Id ; [1] - string | Next Page Token ; [2] - integer | Result Size Estimate ; On Failure Set Error to: ; 1 - Failed to create object in $oHttp ("winhttp.winhttprequest.5.1") ; 2 - Invalid parameter in calling HttpRequest. Received status <> 200. ; Author (s): Ascer ;==================================================================================================================================================================================== Func gmailUsersMessagesList($sEmailAdress, $sAccessToken, $sTokenType = "Bearer", $bIncludeSpamTrash = False, $sLabelIds = Default, $iMaxResults = Default, $sPageToken = Default, $sQ = Default, $sFields = Default) Local $oHttp = ObjCreate("winhttp.winhttprequest.5.1") If Not IsObj($oHttp) Then Return except("gmailUsersMessagesList()", 'Error durning create object "winhttp.winhttprequest.5.1". Something wrong with your Microsoft lib.', 1, 1) Local $sRequest = "https://www.googleapis.com/gmail/v1/users/" & _URIEncode($sEmailAdress) & "/messages?" $sRequest &= "includeSpamTrash=" & $bIncludeSpamTrash If $sLabelIds <> Default Then $sRequest &= "&labelIds=" & $sLabelIds If $iMaxResults <> Default Then $sRequest &= "&maxResults=" & Number($iMaxResults) If $sPageToken <> Default Then $sRequest &= "&pageToken=" & $sPageToken If $sQ <> Default Then $sRequest &= "&q=" & $sQ If $sFields <> Default Then $sRequest &= "&fields=" & $sFields $oHttp.Open("GET", $sRequest, False) $oHTTP.SetRequestHeader("Authorization", $sTokenType & " " & $sAccessToken) $oHTTP.SetRequestHeader("Content-Type", "application/json") $oHttp.Send() Local $iStatus = $oHttp.Status Local $sOutput = $oHttp.ResponseText If $iStatus <> 200 Then Return except("gmailUsersMessagesList()", "Error durning send request to Google. $oHttp.Status = " & $iStatus & @CRLF & "$oHttp.ResponseText = " & $sOutput, 2, $iStatus) Local $aMessages = _JsonValue($sOutput, "[", "]") Local $aNextPageToken = _JsonValue($sOutput, '"nextPageToken": "', '"') Local $aResultSizeEstimate = _JsonValue($sOutput, '"resultSizeEstimate": ', "}") Dim $aInbox[0] Local $bQuestion If IsArray($aMessages) Then $aMessages = _JsonValue($aMessages[0], "{", "}") If IsArray($aMessages) Then For $sItem In $aMessages Dim $aSingleMessageArray[0] Local $sId = _JsonValue($sItem, ' "id": "', '"') Local $sThreadId = _JsonValue($sItem, '"threadId": "', '"') $bQuestion = IsArray($sId) ? _TableInsert($aSingleMessageArray, $sId[0]) : _TableInsert($aSingleMessageArray, 0) $bQuestion = IsArray($sThreadId) ? _TableInsert($aSingleMessageArray, $sThreadId[0]) : _TableInsert($aSingleMessageArray, 0) _TableInsert($aInbox, $aSingleMessageArray) Next EndIf EndIf Local $aTable _TableInsert($aTable, $aInbox) $bQuestion = IsArray($aNextPageToken) ? _TableInsert($aTable, $aNextPageToken[0]) : _TableInsert($aTable, 0) $bQuestion = IsArray($aResultSizeEstimate) ? _TableInsert($aTable, Int($aResultSizeEstimate[0])) : _TableInsert($aTable, 0) Return SetError(0, 0, $aTable) EndFunc ;==================================================================================================================================================================================== ; Function: gmailUsersMessagesModify($sMessageId, $sAddLabelIds, $sRemoveLabelIds, $sEmailAdress, $sAccessToken [, $sTokenType = "Bearer"]) ; ; Description: Set status of single email for ex. "UNREAD" or "INBOX" ; ; Parameter(s): $sMessageId - string | Message id to remove. ; $sAddLabelIds - string or array | Add label id for message, can be an array: ["UNREAD", "INBOX"] More labels available by calling gmailUsersLabelsList ; $sRemoveLabelIds- string or array | Remove label id for messages, when nothing to remove put empty string "" ; $sEmailAdress - string | Your gmail account email adress. ; $sAccessToken - string | Your "access_token" received calling oAuth2GetAccessToken()[0] or oAuth2RefreshAccessToken()[0] ; $sTokenType - string | (Default = "Bearer") Your "token_type" received calling oAuth2GetAccessToken()[2] or oAuth2RefreshAccessToken()[3] ; ; Return Value(s): On Success Set Error to 0 and Returns empty string "" ; On Failure Set Error to: ; 1 - Failed to create object in $oHttp ("winhttp.winhttprequest.5.1") ; 2 - Invalid parameter in calling HttpRequest. Received status <> 200. ; Author (s): Ascer ;==================================================================================================================================================================================== Func gmailUsersMessagesModify($sMessageId, $sAddLabelIds, $sRemoveLabelIds, $sEmailAdress, $sAccessToken, $sTokenType = "Bearer") Local $oHttp = ObjCreate("winhttp.winhttprequest.5.1") If Not IsObj($oHttp) Then Return except("gmailUsersMessagesModify()", 'Error durning create object "winhttp.winhttprequest.5.1". Something wrong with your Microsoft lib.', 1, 1) $oHttp.Open("POST", "https://www.googleapis.com/gmail/v1/users/" & _URIEncode($sEmailAdress) & "/messages/" & $sMessageId & "/modify", False) $oHTTP.SetRequestHeader("Authorization", $sTokenType & " " & $sAccessToken) $oHTTP.SetRequestHeader("Content-Type", "application/json") Local $sBody = "{" $sBody &= '"addLabelIds": [' If IsArray($sAddLabelIds) Then Local $iUbound = UBound($sAddLabelIds) For $sItem = 0 To $iUbound - 1 If $sItem = ($iUbound - 1) Then $sBody &= '"' & $sAddLabelIds[$sItem] & '"' Else $sBody &= '"' & $sAddLabelIds[$sItem] & '", ' EndIf Next Else If $sAddLabelIds <> "" Then $sBody &= '"' & $sAddLabelIds & '"' EndIf $sBody &= "]," $sBody &= '"removeLabelIds": [' If IsArray($sRemoveLabelIds) Then Local $iUbound = UBound($sRemoveLabelIds) For $sItem = 0 To $iUbound - 1 If $sItem = ($iUbound - 1) Then $sBody &= '"' & $sRemoveLabelIds[$sItem] & '"' Else $sBody &= '"' & $sRemoveLabelIds[$sItem] & '", ' EndIf Next Else If $sRemoveLabelIds <> "" Then $sBody &= '"' & $sRemoveLabelIds & '"' EndIf $sBody &= "]}" $oHttp.Send($sBody) Local $iStatus = $oHttp.Status Local $sOutput = $oHttp.ResponseText If Not $iStatus = 200 Or Not $iStatus = 204 Then Return except("gmailUsersMessagesModify()", "Error durning send request to Google. $oHttp.Status = " & $iStatus & @CRLF & "$oHttp.ResponseText = " & $sOutput, 2, $iStatus) Return SetError(0, 0, "") EndFunc ;==================================================================================================================================================================================== ; Function: gmailUsersMessagesTrash($sMessageId, $sEmailAdress, $sAccessToken [, $sTokenType = "Bearer"]) ; ; Description: Throw email to trash. You are able to untrash using gmailUsersMessagesUntrash() ; ; Parameter(s): $sMessageId - string | Message id to trash. ; $sEmailAdress - string | Your gmail account email adress. ; $sAccessToken - string | Your "access_token" received calling oAuth2GetAccessToken()[0] or oAuth2RefreshAccessToken()[0] ; $sTokenType - string | (Default = "Bearer") Your "token_type" received calling oAuth2GetAccessToken()[2] or oAuth2RefreshAccessToken()[3] ; ; Return Value(s): On Success Set Error to 0 and Returns 1D array with current message labels ids. ; On Failure Set Error to: ; 1 - Failed to create object in $oHttp ("winhttp.winhttprequest.5.1") ; 2 - Invalid parameter in calling HttpRequest. Received status <> 200. ; Author (s): Ascer ;==================================================================================================================================================================================== Func gmailUsersMessagesTrash($sMessageId, $sEmailAdress, $sAccessToken, $sTokenType = "Bearer") Local $oHttp = ObjCreate("winhttp.winhttprequest.5.1") If Not IsObj($oHttp) Then Return except("gmailUsersMessagesTrash()", 'Error durning create object "winhttp.winhttprequest.5.1". Something wrong with your Microsoft lib.', 1, 1) $oHttp.Open("POST", "https://www.googleapis.com/gmail/v1/users/" & _URIEncode($sEmailAdress) & "/messages/" & $sMessageId & "/trash", False) $oHTTP.SetRequestHeader("Authorization", $sTokenType & " " & $sAccessToken) $oHTTP.SetRequestHeader("Content-Type", "application/json") $oHttp.Send() Local $iStatus = $oHttp.Status Local $sOutput = $oHttp.ResponseText If Not $iStatus = 200 Or Not $iStatus = 204 Then Return except("gmailUsersMessagesTrash()", "Error durning send request to Google. $oHttp.Status = " & $iStatus & @CRLF & "$oHttp.ResponseText = " & $sOutput, 2, $iStatus) Local $aLabelsIds = _JsonValue($sOutput, '"labelIds": [', "]") Dim $aLabels[0] If IsArray($aLabelsIds) Then $aLabelsIds = StringSplit($aLabelsIds[0], @LF) For $sElement = 1 To UBound($aLabelsIds) - 1 Local $aLabel = _JsonValue($aLabelsIds[$sElement], '"', '"') If IsArray($aLabel) Then _TableInsert($aLabels, $aLabel[0]) Next EndIf Return SetError(0, 0, $aLabels) EndFunc ;==================================================================================================================================================================================== ; Function: gmailUsersMessagesUntrash($sMessageId, $sEmailAdress, $sAccessToken [, $sTokenType = "Bearer"]) ; ; Description: Back you message form trash. Destination place depent on your properites it my be "CATEGORY_PERSONAL" you need to verify this. ; ; Parameter(s): $sMessageId - string | Message id back. ; $sEmailAdress - string | Your gmail account email adress. ; $sAccessToken - string | Your "access_token" received calling oAuth2GetAccessToken()[0] or oAuth2RefreshAccessToken()[0] ; $sTokenType - string | (Default = "Bearer") Your "token_type" received calling oAuth2GetAccessToken()[2] or oAuth2RefreshAccessToken()[3] ; ; Return Value(s): On Success Set Error to 0 and Returns 1D array with current message labels ids. ; On Failure Set Error to: ; 1 - Failed to create object in $oHttp ("winhttp.winhttprequest.5.1") ; 2 - Invalid parameter in calling HttpRequest. Received status <> 200. ; Author (s): Ascer ;==================================================================================================================================================================================== Func gmailUsersMessagesUntrash($sMessageId, $sEmailAdress, $sAccessToken, $sTokenType = "Bearer") Local $oHttp = ObjCreate("winhttp.winhttprequest.5.1") If Not IsObj($oHttp) Then Return except("gmailUsersMessagesUntrash()", 'Error durning create object "winhttp.winhttprequest.5.1". Something wrong with your Microsoft lib.', 1, 1) $oHttp.Open("POST", "https://www.googleapis.com/gmail/v1/users/" & _URIEncode($sEmailAdress) & "/messages/" & $sMessageId & "/untrash", False) $oHTTP.SetRequestHeader("Authorization", $sTokenType & " " & $sAccessToken) $oHTTP.SetRequestHeader("Content-Type", "application/json") $oHttp.Send() Local $iStatus = $oHttp.Status Local $sOutput = $oHttp.ResponseText If Not $iStatus = 200 Or Not $iStatus = 204 Then Return except("gmailUsersMessagesUntrash()", "Error durning send request to Google. $oHttp.Status = " & $iStatus & @CRLF & "$oHttp.ResponseText = " & $sOutput, 2, $iStatus) Local $aLabelsIds = _JsonValue($sOutput, '"labelIds": [', "]") Dim $aLabels[0] If IsArray($aLabelsIds) Then $aLabelsIds = StringSplit($aLabelsIds[0], @LF) For $sElement = 1 To UBound($aLabelsIds) - 1 Local $aLabel = _JsonValue($aLabelsIds[$sElement], '"', '"') If IsArray($aLabel) Then _TableInsert($aLabels, $aLabel[0]) Next EndIf Return SetError(0, 0, $aLabels) EndFunc ;==================================================================================================================================================================================== ; Function: gmailUsersMessagesSend($sRecipient, $sSubject, $sBody, $sEmailAdress, $sAccessToken [, $sTokenType = "Bearer" [, $sFrom = Default [, $sAttachment = Default]]) ; ; Description: Send email to single or group of recipients.. ; ; Parameter(s): $sRecipient - string or array | To whom you want to send email. Can by array: ["friend1@gmail.com", "friend2@yahoo.com", "..."] or string: each email separated by comma. ; $sSubject - string | Subject name of email. ; $sBody - string | Email body can be text or html. ; $sEmailAdress - string | Your gmail account email adress. ; $sAccessToken - string | Your "access_token" received calling oAuth2GetAccessToken()[0] or oAuth2RefreshAccessToken()[0] ; $sTokenType - string | (Default = "Bearer") Your "token_type" received calling oAuth2GetAccessToken()[2] or oAuth2RefreshAccessToken()[3] ; $sFrom - string | (Default = not used) It's how receipient see sender name. When default then $sFrom = Your email address. ; $sAttachment - string or array | (Default = not used) Path to your attachment ex. array: [@ScriptDir & "\text.png", @MyDocumentsDir & "\income.pdf"] or single string. ; ; Return Value(s): On Success Set Error to 0 and Returns 1D array: ; [0] - string | Message Id ; [1] - string | Thread Id ; [2] - array | Labels Ids ; On Failure Set Error to: ; 1 - Failed to create object in $oHttp ("winhttp.winhttprequest.5.1") ; 2 - Invalid parameter in calling HttpRequest. Received status <> 200. ; Author (s): Ascer ;==================================================================================================================================================================================== Func gmailUsersMessagesSend($sRecipient, $sSubject, $sBody, $sEmailAdress, $sAccessToken, $sTokenType = "Bearer", $sFrom = Default, $sAttachment = Default) Local $oHttp = ObjCreate("winhttp.winhttprequest.5.1") If Not IsObj($oHttp) Then Return except("gmailUsersMessagesSend()", 'Error durning create object "winhttp.winhttprequest.5.1". Something wrong with your Microsoft lib.', 1, 1) $oHttp.Open("POST", "https://www.googleapis.com/gmail/v1/users/" & _URIEncode($sEmailAdress) & "/messages/send", False) $oHTTP.SetRequestHeader("Authorization", $sTokenType & " " & $sAccessToken) $oHTTP.SetRequestHeader("Content-Type", "application/json") Local $sBoundaryAttachments = "f103045ff2f4d25f810564427c65" Local $To If IsArray($sRecipient) Then For $sEmail In $sRecipient $To &= $sEmail & "," Next $To = StringTrimRight($To, 1) Else $To = $sRecipient EndIf Local $sStruct $sStruct &= "From: " If $sFrom <> Default Then $sStruct &= '"' & $sFrom & '" ' $sStruct &= "<" & $sEmailAdress & ">" & @LF $sStruct &= "Subject: " & $sSubject & @LF $sStruct &= "To: " & $To & @LF $sStruct &= 'Content-Type: multipart/mixed; boundary="' & $sBoundaryAttachments & '"' & @LF & @LF $sStruct &= "--" & $sBoundaryAttachments & @LF $sStruct &= 'Content-Type: text/html; charset="UTF-8"' & @LF $sStruct &= @LF $sStruct &= $sBody If $sAttachment <> Default Then $sStruct &= @LF & @LF $sStruct &= "--" & $sBoundaryAttachments & @LF If Not IsArray($sAttachment) Then Local $aFastArray = [$sAttachment] $sAttachment = $aFastArray EndIf For $sAttatch In $sAttachment Local $sFileName = StringSplit($sAttatch, "\") If IsArray($sFileName) Then $sFileName = $sFileName[$sFileName[0]] Else $sFileName = "Unknown.txt" EndIf $sStruct &= 'Content-Disposition: attachment; filename="' & $sFileName & '"' & @LF $sStruct &= "Content-Transfer-Encoding: base64" & @LF $sStruct &= @LF Local $sFile = FileRead(FileOpen($sAttatch)) ConsoleWrite("++++ gmailUsersMessagesSend() -> converting attachment " & '"' & $sFileName & '"' & " to base64binary." & @CRLF) $sFile = base64($sFile) $sStruct &= $sFile & @LF & @LF $sStruct &= "--" & $sBoundaryAttachments & @LF Next EndIf Local $sJson = "{" $sJson &= '"raw": "' & base64($sStruct, True, True) & '"' $sJson &= "}" $oHttp.Send($sJson) Local $iStatus = $oHttp.Status Local $sOutput = $oHttp.ResponseText If Not $iStatus = 200 Or Not $iStatus = 204 Then Return except("gmailUsersMessagesSend()", "Error durning send request to Google. $oHttp.Status = " & $iStatus & @CRLF & "$oHttp.ResponseText = " & $sOutput, 2, $iStatus) Local $aId = _JsonValue($sOutput, '"id": "', '"') Local $aThreadId = _JsonValue($sOutput, '"threadId": "', '"') Local $aLabelsIds = _JsonValue($sOutput, '"labelIds": [', "]") Dim $aLabels[0] If IsArray($aLabelsIds) Then $aLabelsIds = StringSplit($aLabelsIds[0], @LF) For $sElement = 1 To UBound($aLabelsIds) - 1 Local $aLabel = _JsonValue($aLabelsIds[$sElement], '"', '"') If IsArray($aLabel) Then _TableInsert($aLabels, $aLabel[0]) Next EndIf Local $aTable, $fQuestion $fQuestion = IsArray($aId) ? _TableInsert($aTable, $aId[0]) : _TableInsert($aTable, "") $fQuestion = IsArray($aThreadId) ? _TableInsert($aTable, $aThreadId[0]) : _TableInsert($aTable, "") _TableInsert($aTable, $aLabels) Return SetError(0, 0, $aTable) EndFunc ;==================================================================================================================================================================================== ; Function: gmailUsersMessagesAttachmentsGet($sMessageId, $sAttachmentId, sEmailAdress, $sAccessToken [, $sTokenType = "Bearer"]) ; ; Description: Download attachment id from specific email id. ; ; Parameter(s): $sMessageId - string | Message id to remove. ; $sAttachmentId - string | Attachment id can be get by using gmailUsersMessagesGet()[6] with $sFormat = "full" or "metaheaders" ; $sEmailAdress - string | Your gmail account email adress. ; $sAccessToken - string | Your "access_token" received calling oAuth2GetAccessToken()[0] or oAuth2RefreshAccessToken()[0] ; $sTokenType - string | (Default = "Bearer") Your "token_type" received calling oAuth2GetAccessToken()[2] or oAuth2RefreshAccessToken()[3] ; ; Return Value(s): On Success Set Error to 0 and Returns 1D array: ; [0] - integer | Size of file in bytes ; [1] - string | Output data decoded from base64binary to string. ; On Failure Set Error to: ; 1 - Failed to create object in $oHttp ("winhttp.winhttprequest.5.1") ; 2 - Invalid parameter in calling HttpRequest. Received status <> 200. ; Author (s): Ascer ;==================================================================================================================================================================================== Func gmailUsersMessagesAttachmentsGet($sMessageId, $sAttachmentId, $sEmailAdress, $sAccessToken, $sTokenType = "Bearer") Local $oHttp = ObjCreate("winhttp.winhttprequest.5.1") If Not IsObj($oHttp) Then Return except("gmailUsersMessagesAttachmentsGet()", 'Error durning create object "winhttp.winhttprequest.5.1". Something wrong with your Microsoft lib.', 1, 1) $oHttp.Open("GET", "https://www.googleapis.com/gmail/v1/users/" & _URIEncode($sEmailAdress) & "/messages/" & $sMessageId & "/attachments/" & $sAttachmentId , False) $oHTTP.SetRequestHeader("Authorization", $sTokenType & " " & $sAccessToken) $oHTTP.SetRequestHeader("Content-Type", "application/json") $oHttp.Send() Local $iStatus = $oHttp.Status Local $sOutput = $oHttp.ResponseText If Not $iStatus = 200 Or Not $iStatus = 204 Then Return except("gmailUsersMessagesAttachmentsGet()", "Error durning send request to Google. $oHttp.Status = " & $iStatus & @CRLF & "$oHttp.ResponseText = " & $sOutput, 2, $iStatus) Local $iSize = _JsonValue($sOutput, '"size": ', ",") Local $sData = _JsonValue($sOutput, ' "data": "', '"') Local $aTable, $fQuestion $fQuestion = IsArray($iSize) ? _TableInsert($aTable, Int($iSize[0])) : _TableInsert($aTable, 0) $fQuestion = IsArray($sData) ? _TableInsert($aTable, base64($sData[0], False, True)) : _TableInsert($aTable, "") Return SetError(0, 0, $aTable) EndFunc #EndRegion #Region 1.3, Internal Functions. ;============================================================================================================================== ; Function: _TableInsert($aTable, $vArg [, $iPosition=Null]) ; ; Description: Insert argumnet into array. ; Parameter(s): $aTable - Array to insert. ; $vArg - Item to insert can be example: ; $vArg = 1 ; $vArg = 'Hello World' ; $vArg[5] = [1, 2, 3, 4, 5] ; $vArg[2][2] = [[1, 2], [3, 4]] ; $iPosition - place in array to insert, Default=Null, last ; ; Return Value(s): On Success - Returns Ubound new array ; On Failure - Returns False ; ; Author (s): Ascer ;================================================================================================================================= Func _TableInsert(ByRef $aTable, $vArg, $iPosition = Null) If Not IsArray ($aTable) Then Dim $aTable[0] EndIf Local $iAmt = UBound($aTable) ReDim $aTable[$iAmt + 1] If $iPosition = Null Then $aTable[$iAmt] = $vArg Return $iAmt + 1 EndIf If $iPosition + 1 > $iAmt + 1 Then Return 0 Local $aStore = $aTable $aTable[$iPosition] = $vArg For $i = 0 To $iAmt - 1 If $iPosition <= $i Then $aTable[$i + 1] = $aStore[$i] Else $aTable[$i] = $aStore[$i] EndIf Next Return $iAmt + 1 EndFunc ;============================================================================================================================== ; Function: base64($vCode [, $bEncode = True [, $bUrl = False]]) ; ; Description: Decode or Encode $vData using Microsoft.XMLDOM to Base64Binary or Base64Url. ; IMPORTANT! Encoded base64url is without @LF after 72 lines. Some websites may require this. ; ; Parameter(s): $vData - string or integer | Data to encode or decode. ; $bEncode - boolean | True - encode, False - decode. ; $bUrl - boolean | True - output is will decoded or encoded using base64url shema. ; ; Return Value(s): On Success - Returns output data ; On Failure - Returns 1 - Failed to create object. ; ; Author (s): (Ghads on Wordpress.com), Ascer ;=============================================================================================================================== Func base64($vCode, $bEncode = True, $bUrl = False) Local $oDM = ObjCreate("Microsoft.XMLDOM") If Not IsObj($oDM) Then Return SetError(1, 0, 1) Local $oEL = $oDM.createElement("Tmp") $oEL.DataType = "bin.base64" If $bEncode then $oEL.NodeTypedValue = Binary($vCode) If Not $bUrl Then Return $oEL.Text Return StringReplace(StringReplace(StringReplace($oEL.Text, "+", "-"),"/", "_"), @LF, "") Else If $bUrl Then $vCode = StringReplace(StringReplace($vCode, "-", "+"), "_", "/") $oEL.Text = $vCode Return $oEL.NodeTypedValue EndIf EndFunc ;==>base64 #EndRegion