Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.
Keywords: oAuth Sending API Key WinHttp GET Authorization Header Basic
;---------------------------------------------------------------------------- ; ; Sending an API-Key using WinHttp ; ; API Key Token Header Authentication OAuth ; ;---------------------------------------------------------------------------- cURL = 'https://api.xero.com/api.xro/2.0/Organisation' ; MODIFY TO FIT YOUR NEEDS userid = 'joe' ; MODIFY TO FIT YOUR NEEDS apikey = 'a195a46525c07281c324a4673caa59a1' ; MUST SPECIFY A VALID API KEY TOKEN GoSub UDFs oHTTP = CreateObject("WinHttp.WinHttpRequest.5.1") ;---------------------------------------------------------------------------- ; Initiate the request ;---------------------------------------------------------------------------- a = oHTTP.Open("GET", cURL, @FALSE) ;---------------------------------------------------------------------------- ; Define Headers ;---------------------------------------------------------------------------- ; Authorization ; The format for Authorization header is as follow:; ; Authorization: Basic XXXXXXX ; Where XXXXXX is Base64 encoded string: "UserName:UserPassword." ; 1.Username and password are combined into a string "username:password" ; 2.The resulting string literal is then encoded using Base64 str_64=Base64StringFromClearString(B64GetCookie(),StrCat(userid,":",apikey)) oHTTP.SetRequestHeader("Authorization",StrCat("Basic ", str_64)) ; Content-Type oHTTP.SetRequestHeader("Content-Type", "application/json"); ;---------------------------------------------------------------------------- ; Send the request ;---------------------------------------------------------------------------- oHTTP.Send() oHTTP.WaitForResponse() If oHTTP.Status != 200 If oHTTP.Status == 302 Pause( "Server Attempted Redirect to: ", oHTTP.getResponseHeader("Location")) EndIf Status = oHTTP.Status StatusText = oHTTP.StatusText headers = oHTTP.GetAllResponseHeaders() Pause(oHTTP.Status, headers) EndIf ;---------------------------------------------------------------------------- ; Get Response ;---------------------------------------------------------------------------- JSON_Data = oHTTP.ResponseText ClipPut(JSON_data) Pause("JSON DATA",JSON_Data) oHTTP=0 Exit ;---------------------------------------------------------------------------- ;---------------------------------------------------------------------------- ;---------------------------------------------------------------------------- :UDFs ;---------------------------------------------------------------------------- ; B64GetCookie : Takes no parameters. Returns a value that must be ; passed to all the other B64 functions in these UDFs ; Basically it initializes an array for the other ; functions to use ; ; Retuns B64Cookie used by the other B64 functions. ;---------------------------------------------------------------------------- #DefineFunction B64GetCookie() B64Cookie=ArrDimension(256,2) ; ,0 is to B64 ,1 if from B64 ArrInitialize(B64Cookie,-1) AUPPER=Char2Num("A") alower=Char2Num("a") ZEROChr=Char2Num("0") For xx=0 To 25 B64Cookie[xx,0]=Num2Char(AUPPER+xx) B64Cookie[xx+26,0]=Num2Char(alower+xx) Next For xx=52 To 61 B64Cookie[xx,0]=Num2Char(ZEROChr+xx-52) Next B64Cookie[62,0]="+" B64Cookie[63,0]="/" For xx=0 To 63 val=Char2Num(B64Cookie[xx,0]) B64Cookie[val,1]=xx Next b64Cookie[Char2Num("="),1]=9999 ; flag the = sign Return(B64Cookie) #EndFunction ;---------------------------------------------------------------------------- ; Base64StringFromClearString(B64Cookie,clearstring) ; A useful function that will encode a text string ; ; B64Cookie == Value returned by the B64GetCookie function. ; clearstring == text string to encode ; ; Returns: Base64 encoded string ;---------------------------------------------------------------------------- #DefineFunction Base64StringFromClearString(B64Cookie,clearstring) s=StrLen(clearstring) bb=BinaryAlloc(s) BinaryPokeStr(bb,0,clearstring) b64bb=Base64BBFromClearBB(B64Cookie,bb) s=BinaryPeekStr(b64bb,0,BinaryEodGet(b64bb)) BinaryFree(bb) BinaryFree(b64bb) Return(s) #EndFunction ;---------------------------------------------------------------------------- ; Base64BBFromClearBB(B64Cookie,clearbb) ; A core, low level conversion function not necessarily ; normally used by most users of these UDFs. ; Converts a binary buffer holding data to a new binary ; buffer holding Base64 encoded data. ; ; B64Cookie == Value returned by the B64GetCookiefunction. ; clearbb == A binary buffer holding non-encoded text. (May be binary data) ; ; Returns: A binary buffer containing clear text ;---------------------------------------------------------------------------- #DefineFunction Base64BBFromClearBB(B64Cookie,clearbb) ;clearbb contains bytes to be converted into base64 format ;how many triplets clearbytes=BinaryEodGet(clearbb) cleartriplets= clearbytes/3 clearremnants= clearbytes mod 3 ;if clearremnants !=0 then cleartriplets=cleartriplets+1 b64quads=ClearTriplets If clearremnants!=0 Then b64quads=b64quads+1 b64bytes= b64quads*4 + (b64quads/16)*2 + 6; 4bytes per quad + CRLF every 16 quads plus 6 in case I got the math wrong ;Only 16 quads per line allowed quadlinecount=0 B64BB=BinaryAlloc(b64bytes) For xx= 1 To cleartriplets c1 = BinaryPeek(clearbb,(xx-1)*3+0) c2 = BinaryPeek(clearbb,(xx-1)*3+1) c3 = BinaryPeek(clearbb,(xx-1)*3+2) d1 = c1 >> 2 d2 = ( (c1 & 3) << 4) | (c2 >> 4) d3 = ( (c2 & 15) << 2) | (c3 >> 6) d4 = ( (c3 & 63) ) quad=StrCat(B64Cookie[d1,0],B64Cookie[d2,0],B64Cookie[d3,0],B64Cookie[d4,0]) BinaryPokeStr(b64BB,BinaryEodGet(b64bb),quad) quadlinecount=quadlinecount+1 If (quadlinecount mod 16)==0 Then BinaryPokeStr(b64BB,BinaryEodGet(b64bb),@CRLF) Next xx Switch clearremnants Case 1 c1=BinaryPeek(clearbb,(cleartriplets)*3+0) d1 = c1 >> 2 d2 = ( (c1 & 3) << 4) quad=StrCat(B64Cookie[d1,0],B64Cookie[d2,0],"==") Continue Case 2 c1=BinaryPeek(clearbb,(cleartriplets)*3+0) c2 = BinaryPeek(clearbb,(cleartriplets)*3+1) d1 = c1 >> 2 d2 = ( (c1 & 3) << 4) | (c2 >> 4) d3 = ( (c2 & 15) << 2) quad=StrCat(B64Cookie[d1,0],B64Cookie[d2,0],B64Cookie[d3,0],"=") Continue Case 1 Case 2 BinaryPokeStr(b64BB,BinaryEodGet(b64bb),quad) quadlinecount=quadlinecount+1 If (quadlinecount mod 16)==0 Then BinaryPokeStr(b64BB,BinaryEodGet(b64bb),@CRLF) EndSwitch Return(b64BB) #EndFunction Return
Article ID: W18213
Filename: Sending an API-Key using WinHttp.txt
File Created: 2013:01:08:15:38:56
Last Updated: 2013:01:08:15:38:56