HTTPGet() bearer (token) authentication example

HTTPGet() bearer (token) authentication

'EMAG requires that you perform two HTTPPost requests. The first to get a token

'from Verizon the second to send the actual message using that token. The first

'request must contain a Base64Encoded version of the ClientID and ClientSecret

'from Verizon.

 

Public Batt_volt, PTemp

 

'Base64Encode variables

Public ClientID As String *40

Public ClientSecret As String *50

Public CombinedClientIDandSecret As String *90

Public Base64EncodedAuthHeader As String *140

Public ReturnCode

Public DestSize

Public SourceSwath

 

Public EMAGTokenURL As String *80

Public EMAGTxtURL As String *80

 

Public SenderCode As String *20

 

Public MessageContents As String *200

Public TokenContentsString As String *200

 

Public textsend As Boolean

Public TxtContentsString As String *500

Public TxtHTTPResponse As String *200

Public TxtHTTPHeader As String *200

 

Public GetTokenHeader As String *500

Public HTTPTokenResponse As String *400

 

Public ReceivedToken As String *200

 

Public RecipientNum As String *12

 

'Define Data Tables

DataTable (Test,1,-1) 'Set table size to # of records, or -1 to autoallocate.

DataInterval (0,15,Sec,10)

Minimum (1,Batt_volt,FP2,False,False)

Sample (1,PTemp,FP2)

EndTable

 

'Main Program

BeginProg

'This is the URL the token request has to be sent to

EMAGTokenURL = "https://api.emag.vzw.com:1443/oauth2/token"

'Notice the contents string has to have the %3A added for the URL encoding in

'a few places.

TokenContentsString = "grant_type=client_credentials&scope=sms.text%3Awrite+sms.binary.raw%3Awrite+sms.text%3Aread"

'This is the URL to send the text message send request to

EMAGTxtURL = "https://api-njbb.emag.vzw.com:1443/messaging/sms/text/simple"

'This is the message text that will show up on your phone

MessageContents = "The Water level is now at 10 feet! Also: Be sure to check the battery voltage level to ensure the station is operational after dark."

'Enter your Client Secret and Client ID you received from Verizon

ClientID = ""

ClientSecret = ""

'This is the Sender Code from Verizon AKA the phone number that sends the text

'message. It must start with a +1

SenderCode = "+1XXXXXXXXXX"

'This is the text message recipient phone number. It must be preceeded by +1

RecipientNum = "+1XXXXXXXXXX"

 

'This code concatenates the Client ID and ClientSecret from Verizon as the

'unencoded Base64 part of the Authorization Header. Note: the required colon

'between the two.

CombinedClientIDandSecret=ClientID&":"&ClientSecret

'Detects the string length of the Combined Client ID and Client Secret to pass

'to the Base64Encode instruction

SourceSwath=Len (CombinedClientIDandSecret)

'This encodes the part of the header for the token request that has to be

'Base64 encoded

Base64Encode (ReturnCode,0,CombinedClientIDandSecret,0,SourceSwath,Base64EncodedAuthHeader,DestSize)

 

Scan (1,Sec,0,0)

PanelTemp (PTemp,15000)

Battery (Batt_volt)

 

CallTable Test

NextScan

 

SlowSequence

Scan (60,Sec,3,0)

 

'This trigger enables the text send

If textsend = -1 Then

'NjYwZjdCMzkxODdhQDRkQjliQTI6RjZCQTUxNTUtOTk5NC00QzExLUE4QjUtMjdFRTI0MTRGQkM3

'The string of numbers in the GetTokenHeader after Authorization: Basic is

'the Base64 encoded ClientID and the ClientSecret from Verizon. It can be

'made using the following website: https://base64.guru/converter/encode/text

'or formed automatically in the program using Base64Encode()

GetTokenHeader = "Authorization: Basic "&Base64EncodedAuthHeader&" "&CHR(13)&CHR(10)& "Content-Type: application/x-www-form-urlencoded"

'The HTTPPost() instruction is performed here to get a Token from Verizon's

'server to send text messages. The token is only good for a short period of

'time.

HTTPPost (EMAGTokenURL,TokenContentsString,HTTPTokenResponse,GetTokenHeader)

'These two instructions take the raw HTTPTokenResponse from the server and

'cut out the Token assigning it to the Received Token variable. The first

'SplitString discards the beginning info and the second discards the ending

'info.

SplitStr (ReceivedToken,HTTPTokenResponse,CHR(34)&"access_token"&CHR(34)&":"&CHR(34),1,4)

SplitStr (ReceivedToken,ReceivedToken,CHR(34),1,5)

'Sending an SMS these two instructions setup the Header and Contents for

'the message

TxtHTTPHeader = "Authorization: Bearer " & ReceivedToken &CHR(13)&CHR(10)& "Content-Type: application/json"

TxtContentsString = "{" &CHR(34)& "src"&CHR(34)&":"&CHR(34)& "ern:sms:e164:" &SenderCode & CHR(34)&CHR(44)&CHR(34)& "dest"&CHR(34)&":"&CHR(34)&"ern:sms:e164:"& RecipientNum &CHR(34)&CHR(44)& CHR(34)&"text"&CHR(34)&":"&CHR(34)& MessageContents &CHR(34)&"}"

'This instruction sends the actual text message

HTTPPost (EMAGTxtURL,TxtContentsString,TxtHTTPResponse,TxtHTTPHeader)

'This disables the textsend trigger

textsend = 0

EndIf

 

NextScan

EndProg