问题描述
我正在尝试使用 VBA 代码处理来自我们学校 MS-Access 数据库的 Moodle 数据以发布 xml.objects。 我从一个示例中实现了以下代码,用于将 RESTful-API 用于我的 VBA 代码(来源 https://moodle.org/plugins/webservice_restful):
json 代码:
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
我不确定我发现的以下功能是否必要:
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
无论哪种方式,返回的都是请求中没有授权头的错误 {"exception":"moodle_exception","errorcode":"noauthheader","message":"No Authorization header found in request sent to Moodle "}。我猜这是一个格式问题,所以我尝试了围绕我猜是标题类型声明(“授权”)和令牌的各种引号,没有任何效果。另外我在 CURL 源下找到了另一个人的评论: “授权标头被我的 Apache 2 版本剥离,导致 noauthheader 错误。添加此指令时它起作用了: SetEnvIf 授权 "(.*)" HTTP_AUTHORIZATION=$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;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)