convert CURL request into VBA xml Object in MS-Access

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-10 18:17:08

问题


I am trying to handle moodle data from our schools MS-Access database using VBA-code to post xml.objects. I implemented the following code from an example for using the RESTful-API into my VBA-code (source https://moodle.org/plugins/webservice_restful):

json code:

curl -X POST \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H 'Authorization: {token}' \
-d'{"options": {"ids":[6]}}' \
"https://localhost/webservice/restful/server.php/core_course_get_courses"

This is my conversion in VBA in a ms-access db module so far:

Option Compare Database
Option Explicit

Private Sub btnTestMoodleApi_Click()

Dim objRequest As Object
Dim strResult As String
Dim strPostData As String
Dim strToken as String
Dim strURL as String

strToken = xxx
strURL = xxx

Set objRequest = CreateObject("MSXML2.XMLHTTP")

strPostData = "'options[ids][0]=8'" 

With objRequest
  .Open "POST", strURL & "/webservice/restful/server.php/core_course_get_courses"
  .setRequestHeader "Authorization", strToken
  '  .setRequestHeader Chr(34) & "Authorization" & Chr(34), Chr(34) & EncodeBase64(strToken) & Chr(34) ' is any of this necessary? I also tried single quotes chr(39)

  .setRequestHeader "Content-Type", " application/x-www-form-urlencoded"
  .setRequestHeader "Accept", "application/json"
  .Send (strPostData)
   While .ReadyState <> 4
    DoEvents
   Wend
  strResult = .responseText
    Debug.Print strResult
End With

End Sub

I am not sure if the following function that I found is necessary:

Function EncodeBase64(text As String) As String

Dim arrData() As Byte
arrData = StrConv(text, vbFromUnicode)

Dim objXML As MSXML2.DOMDocument60
Dim objNode As MSXML2.IXMLDOMElement

Set objXML = New MSXML2.DOMDocument60
Set objNode = objXML.createElement("b64")

objNode.DataType = "bin.base64"
objNode.nodeTypedValue = arrData
EncodeBase64 = objNode.text

Set objNode = Nothing
Set objXML = Nothing
  
End Function

Either way the return is an error that there was no authorization header in the request {"exception":"moodle_exception","errorcode":"noauthheader","message":"No Authorization header found in request sent to Moodle"}. I guessed it is a formatting problem, so I tried various sorts of quotes around what I guess is the header type declaration ("Authorization") and the token, nothing worked. Also I found this remark by another person under the CURL source: "The authorization header was stripped by my version of Apache 2, resulting in a noauthheader error. It worked when added this directive: SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1" However I would not know if that error source applies to my case and how I would implement the directive in my VBA code. Any help would be very much appreciated! By the way this is the corresponding php code in a locallib.php file where I believe the curl request to be directed to:

     * Get the webservice authorization token from the request.
     * Throws error and notifies caller on failure.
     *
     * @param array $headers The extracted HTTP headers.
     * @return string $wstoken The extracted webservice authorization token.
     */
    private function get_wstoken($headers) {
        $wstoken = '';

        if (isset($headers['HTTP_AUTHORIZATION'])) {
            $wstoken = $headers['HTTP_AUTHORIZATION'];
        } else {
            // Raise an error if auth header not supplied.
            $ex = new \moodle_exception('noauthheader', 'webservice_restful', '');
            $this->send_error($ex, 401);
        }

        return $wstoken;
    }

来源:https://stackoverflow.com/questions/65580320/convert-curl-request-into-vba-xml-object-in-ms-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!