我正在使用Django-Tastypie 0.11.1,我正在尝试从第三方网站(常规API调用)在Django(v1.6)的auth_user模型中创建用户。
资源:
class CreateMasterResource(ModelResource):
def hydrate(self, bundle):
# Logic to Validate username and Password
bundle.data["password"] = make_password(bundle.data["password"])
return bundle
class Meta:
queryset = User.objects.using("master").all()
resource_name = 'create_master'
default_format = 'application/json'
list_allowed_methods = ['post', 'get']
serializer = Serializer()
# authentication = MultiAuthentication(ApiKeyAuthentication(), SessionAuthentication())
# authorization = Authorization()
always_return_data = True
问题: 我正在尝试从WP站点发送CURL请求。我确保在标题中以及在" GET"中传递API密钥授权Apikey:参数http://xyz.example.com:8000/master/api/v1/create_master/?username=johndoe&api_key=S4M4L34PIK3Y:
请求命中Django Server并返回401错误代码。没有错误消息。在curl响应上使用var_dump()时,它返回""。
从WP网站发送的CURL请求:
$data = array(
"first_name" => $order->billing_first_name,
"last_name" => $order->billing_last_name,
"email" => $order->billing_email,
"password" => $password,
);
$data_string = json_encode($data);
$ch = curl_init('http://xyz.example.com:8000/master/api/v1/create_master/');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: ApiKey abc@xyz.com:',
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
var_dump($result);
到目前为止调试:
请让我知道我失踪了什么,并且我知道我不应该删除auth类。它已被删除以进行调试。