Access Denied Query XML SOAP Sharepoint - vba

The code worked very well, but these last days just stopped to work.
The .responseText is now:
Access is denied. (Exception from HRESULT: 0x80070005
(E_ACCESSDENIED))"
I tried with and without the line: objXMLHTTP.setRequestHeader "Authorization" And got the same Error.
With and without User and Password at line objXMLHTTP.Open, and still the same problem, Access Denied.
Can you guys help me please?
Sub selectSharepoint()
Dim objXMLHTTP As MSXML2.XMLHTTP
Dim strBatchXml As String
Dim strSoapBody As String
UserName = "john#email.com"
Password = "123456"
Set objXMLHTTP = New MSXML2.XMLHTTP
objXMLHTTP.Open "POST", "https://ts.sharepoint.com/sites/2018_teste/_vti_bin/lists.asmx", False, UserName, Password
'objXMLHTTP.Open "POST", "https://ts.sharepoint.com/sites/2018_teste/_vti_bin/lists.asmx", False 'This way worked the same, and got the same error.
objXMLHTTP.setRequestHeader "Content-Type", "text/xml; charset=UTF-8"
objXMLHTTP.setRequestHeader "Content-Length", "length"
'objXMLHTTP.setRequestHeader "Authorization", "Basic " + Base64Encode(UserName & ":" & Password) 'makes no effect, with or without Base64Encode
objXMLHTTP.setRequestHeader "SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/GetListItems"
strSoapBody = "<?xml version='1.0' encoding='utf-8'?>" & _
" <soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" & _
" <soap:Body>" & _
" <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>" & _
" <listName>AllNames</listName>" & _
" <query>" & _
" <Query xmlns=''>" & _
" <Where>" & _
" <Eq>" & _
" <FieldRef Name='Name' />" & _
" <Value Type='Text'>Cloe</Value>" & _
" </Eq>" & _
" </Where>" & _
" <OrderBy>" & _
" <FieldRef Name='ID' />" & _
" </OrderBy>" & _
" </Query>" & _
" </query>"
strSoapBody = strSoapBody & _
" <rowLimit>1</rowLimit>" & _
" <viewFields><ViewFields>" & _
" <FieldRef xsi:type='s:string' Name='Name'></FieldRef>" & _
" </ViewFields></viewFields>" & _
" </GetListItems>" & _
" </soap:Body>" & _
" </soap:Envelope>"
objXMLHTTP.send strSoapBody
If objXMLHTTP.Status <> 200 Then
MsgBox ("Error! " & Chr(10) & objXMLHTTP.responseText)
End
Else
tudo = objXMLHTTP.responseText
End If
End Sub
MsgBox Error:
MsgBox Error

Related

VBA Rest API - DHL Return label - 401

I am trying to create a return shipping DHL parcel label with provided DHL api in the sandbox:
https://developer.dhl.com/api-reference/parcel-de-returns-post-parcel-germany#get-started-section/user-guide
With postman it works. But I would like to implement the HTTP Request in VBA.
I always receive status 401 Unauthorized
I guess it is the way how I pass the credentials.
Anyone has an idea how to get it working?
create return labels in the sandbox, with the following user data:
Username: "2222222222_customer"
Password: "uBQbZ62!ZiBiVVbhc"
Sub restAPICall()
Dim objRequest As MSXML2.ServerXMLHTTP60
Dim id_header_name As String, id_key As String, secret_header_name As String, secret_key As String
Dim strUrl As String
Dim blnAsync As Boolean
Dim strResponse As String
Dim json As Object
Dim authKey As String
Set objRequest = New ServerXMLHTTP60
strUrl = "https://api-sandbox.dhl.com/parcel/de/shipping/returns/v1/orders?labelType=BOTH" 'Endpoint Test
blnAsync = False
id_key = "2222222222_customer"
pass = "uBQbZ62!ZiBiVVbhc"
apiKey = "123456789"
body = "{""receiverId"":""deu"", " _
& " ""customerReference"":""Kundenreferenz"", " _
& " ""shipmentReference"":""Sendungsreferenz"", " _
& " ""shipper"": { " _
& " ""name1"":""Absender Retoure Zeile 1"", " _
& " ""name2"":""Absender Retoure Zeile 2"", " _
& " ""name3"":""Absender Retoure Zeile 3"", " _
& " ""addressStreet"":""Charles-de-Gaulle Str."", " _
& " ""addressHouse"":""20"", " _
& " ""city"":""Bonn"", " _
& " ""email"":""Max.Mustermann#dhl.local"", " _
& " ""phone"":""+49 421 987654321"", " _
& " ""postalCode"":""53113"", " _
& " ""state"":""NRW"", " _
& " }, " _
& " ""itemWeight"": { " _
& " ""uom"": ""g"", " _
& " ""value"":""1000"", " _
& " }, " _
& " ""itemValue"": { " _
& " ""currency"": ""EUR"", " _
& " ""value"":""100"", " _
& " }, " _
& "}"
With objRequest
.Open "POST", strUrl, blnAsync ', gkpuser, gkpass
.setRequestHeader "Authorization", "Basic " + EncodeBase64(id_key + ":" + pass)
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Accept", "application/json"
.setRequestHeader "dhl-api-key", "apiKey"
.Send body
While objRequest.readyState <> 4
DoEvents
Wend
strResponseHeaders = .StatusText
strResponse = .responseText
allResponseHeader = .GetAllResponseHeaders
End With
Debug.Print body
Debug.Print allResponseHeader
Debug.Print strResponse
End Sub
Function EncodeBase64(text$)
Dim b
With CreateObject("ADODB.Stream")
.Open: .Type = 2: .Charset = "utf-8"
.WriteText text: .Position = 0: .Type = 1: b = .Read
With CreateObject("Microsoft.XMLDOM").createElement("b64")
.DataType = "bin.base64": .nodeTypedValue = b
EncodeBase64 = Replace(Mid(.text, 5), vbLf, "")
End With
.Close
End With
End Function
Many thanks :)
Alex
If apiKey is a variable, then you should leave the quotes out in:
.setRequestHeader "dhl-api-key", "apiKey"

can I use goole calender API with VBA (to create a calender entry from MS-Excel)

I like to create a new google calender entry from Microsoft Excel 2016, how can I do this?
I tried the following code, but I get a Timeout at "xmlhttp.send ...".
Sub AddGoogleKalenderEntry()
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.Open "POST", "https://www.google.com/accounts/ClientLogin", False
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.send "accountType=HOSTED_OR_GOOGLE&Email=<User>#gmail.com&Passwd=<PWD>" & "&source=Gulp-CalGulp-1.05&service=cl"
Lines = Split(xmlhttp.responseText, vbLf)
nvp = Split(Lines(2), "=")
Set xmlhttp = Nothing
calentry = "<?xml version='1.0' ?><entry xmlns='http://www.w3.org/2005/Atom' " _
& "xmlns:gd='http://schemas.google.com/g/2005'>" _
& "<category scheme='http://schemas.google.com/g/2005#kind' " _
& "term='http://schemas.google.com/g/2005#event'></category>" _
& "<title type='text'>Tennis with Beth</title>" _
& "<content type='text'>Meet for a quick lesson.</content>" _
& "<gd:transparency " _
& "value='http://schemas.google.com/g/2005#event.opaque'>" _
& "</gd:transparency>" _
& "<gd:eventStatus " _
& "value='http://schemas.google.com/g/2005#event.confirmed'>" _
& "</gd:eventStatus>" _
& "<gd:where valueString='Rolling Lawn Courts'></gd:where>" _
& "<gd:when startTime='2010-01-22T15:00:00.000Z' " _
& "endTime='2010-01-22T17:00:00.000Z'></gd:when>" _
& "</entry>"
url = "http://www.google.com/calendar/feeds/default/private/full"
Call postEntry(url, nvp)
End Sub

How to change font color for updated Access data in Outlook mail

In Access 2010 I have tables, e.g. Employee(Pracownicy). I can update the data in the table using the subform and the update button.
Updating the data in the subform automatically generates an Outlook mail containing the data in the updated record.
I need to change font color for updated data in the mail body.
The code to update the data and generate e-mail:
Private Sub cmdUpdate2_Click()
CurrentDb.Execute "update Pracownicy" & _
" SET Identyfikator='" & Me.txtID & "'" & _
", Imie='" & Me.txtImie & "'" & _
", Nazwisko ='" & Me.txtNazwisko & "'" & _
", Wiek ='" & Me.txtWiek & "'" & _
", Data_urodzenia ='" & Me.txtData & "'" & _
", Miejsce_urodzenia ='" & Me.txtMiejsce & "'" & _
", Miejscowosc ='" & Me.txtMiejscowosc & "'" & _
", Plec ='" & Me.txtPlec & "'" & _
" where Identyfikator='" & Me.txtID & "'"
'------------------------------------SEND EMAIL----------------------
'Dim varName As Variant
'Dim strUCC As String
Dim varSubject As Variant
Dim varBody As Variant
Dim Poczta As Object
Dim MojMail As Object
On Error Resume Next
'varName = ""
varSubject = "Employer List "
varBody = "Hello" & _
"<br><br>Employer List: " & _
"<br><br><B>Identyfikator:</B> " & Me.txtID & " " & _
"<br><B>Imie:</B> " & Me.txtImie & " " & _
"<br><B>Nazwisko:</B> " & Me.txtNazwisko & " " & _
"<br><B>Wiek:</B> " & Me.txtWiek & " " & _
"<br><B>Data urodzenia:</B> " & Me.txtData & " " & _
"<br><B>Miejsce urodzenia:</B> " & Me.txtMiejsce & " " & _
"<br><B>Miejscowosc:</B> " & Me.txtMiejscowosc & " " & _
"<br><B>Plec:</B> " & Me.txtPlec & " "
Set Poczta = CreateObject("outlook.application")
Set MojMail = Poczta.createitem(0)
With MojMail
'.To =
'.BCC =
.subject = varSubject
'.ReadReceiptRequested = True
'.originatorDeliveryReportRequested = True
.htmlbody = varBody & "<br>"
.display
'.send
End With
Set Poczta = Nothing
Set MojMail = Nothing
If Err.Number <> 0 Then
MsgBox ("Atention")
End If
On Error GoTo 0
'------------------------------------------------------------------------
DoCmd.Close
MsgBox ("End Update")
End Sub
I think this becomes more of an HTML question rather than VBA. Try adding a FONT tag to the following line and see if that works for you.
"<br><br><B><font color="red">Identyfikator:</font></B> " & Me.txtID & " " & _

Runtime error '1004' General odbc error while refreshing excel sheet to get updated data

We are using shared folder in server where we keep all excel sheets based on our business requirement so whoever requires that document he will picked up that document from that shared folder and he will receive all update/manipulated data by clicking on "Refresh" button in "Data" tab in excel 2007,so in my organization everybody pc is working fine and they are getting updated data by refreshing document but in my pc the movement i click on refresh i am getting this error which is in image below please provide me a clear answer.
Sub TT_Out()
' ' Macro2 Macro
Dim RngFromDate, RngToDate
RngFromDate = InputBox("Enter Start Date !", "TT Out", Date - 1)
RngToDate = InputBox("Enter End Date !", "TT Out", RngFromDate)
With Range("Table_Query_from_ALXORCL[TT_OUT_DATE]").ListObject.QueryTable
.Connection = Array(Array( _
"ODBC;DRIVER={Oracle in instantclient_12_1};" & _
"SERVER=ALXORCL;UID=ALXLIVE;PWD=alx123;" & _
"DBQ=ALXORCL;DBA=W;APA=T;EXC=F;XSM=Default;FEN=T;QTO=T;FRC=10;F"), _
Array("DL=10;LOB=T;RST=T;BTD=F;BNF=F;BAM=IfAllSuccessful;NUM=NLS;" & _
"DPM=F;MTS=T;MDI=Me;CSR=F;FWC=F;FBS=60000;TLO=O;" & _
"MLD=0;ODA=F;STE=F;TSZ=8"), Array("192;"))
.CommandText = Array( _
" SELECT ALX_TT_OUT.TT_OUT_CODE, " & _
" ALX_TT_OUT.TT_OUT_DATE, " & _
" ALX_TT_OUT.F_NAME, " & _
" ALX_TT_OUT.B_F_NAME, " & _
" ALX_TT_OUT.SENDING_PRPS, " & _
" ALX_LOOKUP_DET.LOOKUP_DET_NAME||'-'||ALX_TT_OUT.DOC_NO, " & _
" ALX_PRODUCT.PRODUCT_CODE, " & _
" ALX_TT_OUT.QTY*ALX_TT_OUT.SELL_RATE, " & _
" ALX_CORRESPONDENT.CORRESPONDENT_NAME" & Chr(13) & Chr(10) & _
" FROM ALXTEST.ALX_CORRESPONDENT ALX_CORRESPONDENT, ALXTEST2.ALX_LOOKUP_DET ALX_LOOKUP_DET, ALXTEST2.ALX_PRODUCT ALX_PRODUCT, ALXL", _
" IVE.ALX_TT_OUT ALX_TT_OUT" & Chr(13) & "" & Chr(10) & _
" WHERE ALX_PRODUCT.PRODUCT_ID = ALX_TT_OUT.PRODUCT_ID " & _
" AND ALX_TT_OUT.CORRESPONDENT_ID = ALX_CORRESPONDENT.CORRESPONDENT_ID " & _
" AND ALX_LOOKUP_DET.LOOKUP_DET_ID = ALX_TT_OUT.DOC_TYPE_L ", _
" AND ((ALX_TT_OUT.TT_OUT_CODE Not Like '%HOF%') " & _
" AND (to_date(TT_OUT_DATE) Between '" & RngFromDate & "' And '" & RngToDate & "') " & _
" )")
.Refresh BackgroundQuery:=False
End With
End Sub

Converting from GoogleApp Provisioning API to new SDKs

We have been creating and managing our GoogleApps accounts for a couple years by simply generating a block of XML code for the Provisioning API, and then using VBScript to do a POST. Now GoogleApps is requiring that we move to the new Administrative SDKs, and I'm not understanding how, or even if, we can do something similar with the new system.
Here is an example of the code we use to first get an authentication token:
' Create and send XML message to GoogleApps requesting Authentication Token
Set objXMLHTTP = CreateObject("Microsoft.XmlHttp")
objXMLHTTP.open "POST", "https://www.google.com/accounts/ClientLogin", FALSE
objXMLHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objXMLHTTP.send "&Email=Administrator%40company%2Ecom%2Eedu&Passwd=P#ssw0rd&accountType=HOSTED&service=apps"
If Err.Number <> 0 Then
WScript.Echo "Error: send request for GoogleApp Authentication Token failed"
WScript.Quit(1)
End If
' Get response from GoogleApps
strGGAATAuthToken = objXMLHTTP.responseText
If Err.Number <> 0 Then
WScript.Echo "ERROR: Getting GoogleApp Authentication Token (XMLHTTP.responseText) "
WScript.Quit(1)
End If
' Check for known errors in response text
If LCase(Left(strGGAATAuthToken, 6)) = "error=" Then
WScript.Echo "ERROR: GoogleApp replied with Error when asking for Authentication Token"
WScript.Quit(1)
Else
' Extract and return Authentication Token from response text
strGGAATToken = Mid(strGGAATAuthToken, InStr(strGGAATAuthToken, "Auth=") + 5)
GetGAAuthToken = True
End If
Here is a sample of the code we then use to create the account:
' Create XML Record that will be sent to GoogleApps
strXMLRecord = "<?xml version=" & Chr(34) & "1.0" & Chr(34) & " encoding=" & Chr(34) & "UTF-8" & Chr(34) & _
"?>" & vbCRLF
strXMLRecord = strXMLRecord & "<atom:entry xmlns:atom=" & Chr(34) & "http://www.w3.org/2005/Atom" & Chr(34) & _
vbCRLF
strXMLRecord = strXMLRecord & " xmlns:apps=" & Chr(34) & "http://schemas.google.com/apps/2006" & Chr(34) & _
">" & vbCRLF
strXMLRecord = strXMLRecord & " <atom:category scheme=" & Chr(34) & "http://schemas.google.com/g/2005#kind" & _
Chr(34) & vbCRLF
strXMLRecord = strXMLRecord & " term=" & Chr(34) & "http://schemas.google.com/apps/2006#user" & Chr(34) & _
"/>" & vbCRLF
strXMLRecord = strXMLRecord & " <apps:login userName=" & Chr(34) & strCGAAUsername & Chr(34) & vbCRLF
strXMLRecord = strXMLRecord & " password=" & Chr(34) & strCGAAPwd & Chr(34) & vbCRLF
strXMLRecord = strXMLRecord & " changePasswordAtNextLogin=" & Chr(34) & "true" & Chr(34) & vbCRLF
strXMLRecord = strXMLRecord & " suspended=" & Chr(34) & "false" & Chr(34) & "/>" & vbCRLF
' The following line is just so we have the syntax if we need to set quotas
'*****strXMLRecord = strXMLRecord & " <apps:quota limit=" & Chr(34) & "2048" & Chr(34) & "/>" & vbCRLF*****
strXMLRecord = strXMLRecord & " <apps:name familyName=" & Chr(34) & strCGAALastName & Chr(34) & " givenName=" & _
Chr(34) & strCGAAFirstName & Chr(34) & "/>"
strXMLRecord = strXMLRecord & vbCRLF & "</atom:entry>" & vbCRLF
' Create XML object, set headers, and send to GoogleApps
Set objXMLHTTP = CreateObject("Microsoft.XmlHttp")
objXMLHTTP.open "POST", "https://apps-apis.google.com/a/feeds/company.com/user/2.0", FALSE
objXMLHTTP.setRequestHeader "Content-type", "application/atom+xml"
objXMLHTTP.setRequestHeader "Authorization", "GoogleLogin auth=" & strGAAuthToken
objXMLHTTP.send strXMLRecord
If Err.Number <> 0 Then
WScript.Echo "ERROR: unable to XMLHTTP.send for GoogleApps acct creation"
CreateGAAccount = False
WScript.Quit(1)
End If
' Get response from GoogleApps
strResponseText = objXMLHTTP.responseText
If Err.Number <> 0 Then
WScript.Echo "ERROR: unable to get objXMLHTTP.responseText during GoogleApps acct creation"
CreateGAAccount = False
WScript.Quit(1)
End If
' If response reports an error exit function returning False
If InStr(Lcase(strResponseText), "errorcode=") <> 0 Then
WScript.Echo "ERROR: unable to create GoogleApps account"
CreateGAAccount = False
WScript.Quit(1)
End If
' Log GoogleApps account information returned from creation
WScript.Echo "GoogleApp account created for: " & strCGAAUsername
It's probably obvious, but I have a Windows background, not Linux; and I've done scripting, but not real programming. (And I have no experience doing Java and/or other Web programming at all.)
Thanks for any help!!
The steps to use the Admin SDK are very similar.
First you will get authenticated, now google uses Oauth 2 here you can find the documentation on that https://developers.google.com/accounts/docs/OAuth2
here you can test how Oauth works: https://developers.google.com/oauthplayground/
After being authenticated you can now call the Directory API to create a new user. Here is the documentation related to the insert method https://developers.google.com/admin-sdk/directory/v1/reference/users/insert
As you can see in the Doc, you will be sending the same parameters (name, password, etc.) but now it won't be formatted as xml, instead those parameter will be formatted as json (here is a little information on json formatting: http://www.w3schools.com/json/)
I know it's a lot of information, I hope it helps.