在MS Access中将CURL请求转换为VBA xml对象
我正在尝试使用VBA代码处理我们学校MS Access数据库中的moodle数据,以发布xml.objects。 我从一个将RESTful API用于VBA代码的示例中实现了以下代码(源代码): json代码:在MS Access中将CURL请求转换为VBA xml对象,vba,rest,ms-access,curl,moodle-api,Vba,Rest,Ms Access,Curl,Moodle Api,我正在尝试使用VBA代码处理我们学校MS Access数据库中的moodle数据,以发布xml.objects。 我从一个将RESTful API用于VBA代码的示例中实现了以下代码(源代码): json代码: curl -X POST \ -H "Content-Type: application/json" \ -H "Accept: application/json" \ -H 'Authorization: {token}' \ -d'{"
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"
这是我目前在ms access db模块中用VBA进行的转换:
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
无论哪种方式,返回的都是一个错误,即请求{“异常”:“moodle_异常”,“错误代码”:“noauthheader”,“消息”:“在发送到moodle的请求中未找到授权头”}。我猜这是一个格式问题,所以我尝试了各种各样的引号,我猜是标题类型声明(“授权”)和标记,但没有任何效果。我还发现了另一个人在CURL源下的这句话:
“我的Apache 2版本删除了授权标头,导致noauthheader错误。添加此指令时,它起作用:
SetEnvIf授权“(*)”HTTP_授权=$1”
但是,我不知道该错误源是否适用于我的案例,以及如何在VBA代码中实现该指令。任何帮助都将不胜感激!顺便说一句,这是locallib.php文件中相应的php代码,我认为curl请求应该指向:
* 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;
}
* 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;
}