当给出以下配置时,它的返回总是 WebHook 以错误的 HTTP 状态进行响应。 HTTP 状态为 405。
这是网络钩子(Hook)配置:
var token= access_token;
var _eventFilters = [];
_eventFilters.push('/restapi/v1.0/account/~/extension/' + 232102004 + '/presence?detailedTelephonyState=true&aggregated=true')
rcsdk.platform().post('/subscription',
{
eventFilters: _eventFilters,
deliveryMode: {
"transportType": "WebHook",
"encryption": false,
"address": "https://demo.example.com/backend/country-list/web_hook/?auth_token="+token
}
})
.then(function(subscriptionResponse) {
console.log('Subscription Response: ', subscriptionResponse.json());
})
.catch(function(e) {
console.error(e);
});
这是我的 Django webhook url:
@list_route(methods=['get'], url_path='web_hook')
def create_web_hooks(self, request, **kwargs):
query_params = request.query_params.dict()
from django.http import HttpResponse
response = HttpResponse()
if 'auth_token' in query_params:
response['Validation-Token'] = query_params['auth_token']
response['status'] = 200
response.write('Hello World')
return response
提前致谢
最佳答案
在您的 Webhook 响应中,response['Validation-Token']
的内容需要是 RingCentral 创建 Webhook HTTP 请求的 Validation-Token
header 中存在的值。您的 Webhook 监听器中未使用 RingCentral OAuth 2.0 访问 token 。
您的 webhook 示例是用 Python 编写的,因此这里有一些使用 Django 和 Flask 的示例。您应该检查请求 header 是否存在,如果存在,请将值设置为同名的响应 header 。下面展示如何设置标题。
Django
在 Django 中,请求 header 在 HttpRequest.META
中可用,它使用特定算法重命名 header 。 META
是一个字典,因此您可以通过以下方式访问 header :
response['Validation-Token'] = request.META.get('HTTP_VALIDATION_TOKEN')
或
response['Validation-Token'] = request.META['HTTP_VALIDATION_TOKEN']
有关 Django 处理的更多信息,请参阅 HttpRequest.META
的请求和响应对象文档:
https://docs.djangoproject.com/en/1.11/ref/request-response/#django.http.HttpRequest.META
这是标题重命名的具体文本:
With the exception of CONTENT_LENGTH and CONTENT_TYPE, as given above, any HTTP headers in the request are converted to META keys by converting all characters to uppercase, replacing any hyphens with underscores and adding an HTTP_ prefix to the name. So, for example, a header called X-Bender would be mapped to the META key HTTP_X_BENDER.
flask
使用 Flask,您可以通过以下方式使用 flask.Request
类似字典的对象访问 HTTP 请求 header :
response['Validation-Token'] = request.headers.get('Validation-Token')
或
response['Validation-Token'] = request.headers['Validation-Token']
这在 Flask 传入请求数据文档中进行了讨论:
关于javascript - RingCentral JavaScript SDK、WebHook 以错误的 HTTP 状态进行响应。 HTTP 状态为 405,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47789816/